gpt4 book ai didi

spring-boot - @RefreshScope 不起作用 - Spring Boot

转载 作者:行者123 更新时间:2023-12-04 22:28:25 25 4
gpt4 key购买 nike

我正在遵循此处描述的方法:https://github.com/jeroenbellen/blog-manage-and-reload-spring-properties ,唯一的区别是,在我的例子中,属性被用于多个类,所以我把它们都放在一个实用程序类中 CloudConfig我使用 getter 来引用它的变量。这就是类的样子:

@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() .我能够在启动时加载属性就好了,但我无法动态更新它们。谁能告诉我我做错了什么?如果不是制作这个配置类,我完全按照教程描述的那样做,一切正常,但我无法将它适应我的用例。谁能告诉我我错过了什么?

最佳答案

尝试使用 @ConfigurationProperties反而。
例如

@ConfigurationProperties(prefix="config")
public class CloudConfig {

private Integer count;

public Integer count() {
return this.count;
}

public void setCount(Integer count) {
this.count = count;
}

}

reference doc from spring cloud状态:

@RefreshScope works (technically) on an @Configuration class, but it might lead to surprising behaviour: e.g. it does not mean that all the @Beans defined in that class are themselves @RefreshScope. Specifically, anything that depends on those beans cannot rely on them being updated when a refresh is initiated, unless it is itself in @RefreshScope (in which it will be rebuilt on a refresh and its dependencies re-injected, at which point they will be re-initialized from the refreshed @Configuration).

关于spring-boot - @RefreshScope 不起作用 - Spring Boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45137555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com