spring boot @refreshscope 不更新数据源 [英] spring boot @refreshscope doesn't update datasource
问题描述
如果 DB.URL 的环境变量发生变化,我将尝试更新 DB 数据源.下面是我的课,
I am trying to update DB datasource if the environment variable for DB.URL changes. Below is my class,
@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();
}
}
但是当我更新环境 DB.URL 时,它不会向新数据库发出请求.
But when I update environment DB.URL it doesn't make request to new DB.
我参考了文档,因为可以更新数据源,http://projects.spring.io/spring-cloud/spring-cloud.html#_refresh_scope
I have referred the documentation as it is possible to update datasource, http://projects.spring.io/spring-cloud/spring-cloud.html#_refresh_scope
我的班级缺少什么?
推荐答案
如果将 @RefreshScope 添加到类而不从方法中删除.然后 @Value 变量将被刷新.并且数据源会改变.
If you add @RefreshScope to class without removing from method. Then @Value variables will be refreshed. And datasource will change.
这篇关于spring boot @refreshscope 不更新数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!