Java spring boot@refreshscope不';更新数据源

Java spring boot@refreshscope不';更新数据源,java,spring,spring-mvc,spring-boot,spring-boot-actuator,Java,Spring,Spring Mvc,Spring Boot,Spring Boot Actuator,如果DB.URL的环境变量更改,我将尝试更新DB datasource。下面是我的班级 @SpringBootApplication @ConfigurationProperties(value="myapp") public class MyApp { @Value("${myapp.db.url}") String databaseURL; @Value("${myapp.db.username}") String databaseUsername; @Value("${myapp.db

如果DB.URL的环境变量更改,我将尝试更新DB datasource。下面是我的班级

@SpringBootApplication
@ConfigurationProperties(value="myapp")
public class MyApp {
@Value("${myapp.db.url}")
String databaseURL;

@Value("${myapp.db.username}")
String databaseUsername;

@Value("${myapp.db.password}")
String databasePassword;

public static void main(String[] args) {
    SpringApplication.run(MyApp.class, args);
}

@Bean
@RefreshScope
@Primary
public DataSource getDataSource() {
    return DataSourceBuilder.create().username(databaseUsername).password(databasePassword).url(databaseURL)
            .driverClassName("org.postgresql.Driver").build();
}
}
但当我更新environment DB.URL时,它不会向新数据库发出请求

我参考了文档,因为可以更新数据源,


我的类中缺少什么?

您需要将此@RefreshScope注释移动到类上下文MyApp

@SpringBootApplication
@ConfigurationProperties(value="myapp")
@RefreshScope
public class MyApp {
  ...
}

另外,确保在服务上发布请求:
http://{your.api.url}/exactor/refresh
,以便在更改属性后进行刷新。

您需要在类上下文MyApp上移动此@RefreshScope注释:

@SpringBootApplication
@ConfigurationProperties(value="myapp")
@RefreshScope
public class MyApp {
  ...
}

另外,确保在服务上执行请求发布:
http://{your.api.url}/exactor/refresh
,以便在更改属性后刷新它。

如果您将@RefreshScope添加到类中而不从方法中删除。然后将刷新@Value变量。数据源也会改变

如果将@RefreshScope添加到类中而不从方法中删除。然后将刷新@Value变量。数据源也会改变

您可以尝试将其移动到它自己的配置类而不是主spring引导应用程序中吗?如果我使用@RefreshScope将数据源配置移动到一个单独的类中,您可能还需要在其自身@DarrenForsyth的配置上使用
RefreshScope
。项目无法生成,因为spring clould配置客户端无法检测数据源。下面是一个例外,在org.springframework.boot.actuate.autoconfigure.healthincatorautoconfiguration$datasourcesehealthincatorconfiguration.(healthincatorautoconfiguration.java:195)的类路径资源[org/springframework/boot/autoconfiguration/jdbc/DataSourceConfiguration$Tomcat.class]中创建名为“dataSource”的bean时出错您是否将其与应用程序放在同一个包中?您好@Darrenforsyth,是的,我将其放在同一个包中。您是否可以尝试将其移动到它自己的配置类而不是主spring boot应用程序中?如果我使用@RefreshScope将数据源配置移动到一个单独的类中,您可能还需要在其自身@DarrenForsyth的配置上使用
RefreshScope
。项目无法生成,因为spring clould配置客户端无法检测数据源。下面是一个例外,在org.springframework.boot.actuate.autoconfigure.healthincatorautoconfiguration$datasourcesehealthincatorconfiguration.(healthincatorautoconfiguration.java:195)的类路径资源[org/springframework/boot/autoconfiguration/jdbc/DataSourceConfiguration$Tomcat.class]中创建名为“dataSource”的bean时出错你把它和应用程序放在同一个包里吗?嗨@DarrenForsythe,是的,我把它放在同一个包里。