【发布时间】:2017-12-21 14:18:58
【问题描述】:
我遵循这里描述的方法:https://github.com/jeroenbellen/blog-manage-and-reload-spring-properties,唯一的区别是在我的例子中,属性被用于多个类,所以我将它们全部放在一个实用程序类CloudConfig 中,我参考它使用吸气剂的变量。这是类的样子:
@Configuration
@RefreshScope
public class CloudConfig {
static volatile int count; // 20 sec
@Value("${config.count}")
public void setCount(int count) {
this.count = count;
}
public static int getCount() {
return count;
}
}
我在其他类中使用变量count,例如CloudConfig.getCount()。我可以在启动时加载属性,但我无法动态更新它们。谁能告诉我做错了什么?如果我没有制作这个配置类,而是完全按照教程描述的方式进行操作,一切正常,但我无法将其适应我的用例。谁能告诉我我错过了什么?
【问题讨论】:
-
如果其他类是单例并且只在启动时加载这些值,刷新后它们将不会获得新值。
-
是的,但是 CloudConfig 中的那些会,我将在其他类中使用 getter (CloudConfig.getCount()),这样它们也会获得正确的值。对吗?
-
如果你只使用 getter 来设置值,它们不会,它们也不应该是静态的,而是常规方法,否则代理创建将失败。
标签: spring-boot spring-cloud spring-cloud-config