2017-07-17 81 views
1

我遵循这里描述的方法:https://github.com/jeroenbellen/blog-manage-and-reload-spring-properties,唯一的区别是在我的情况下,属性被用于多个类,所以我已经把它们全部放在一个工具类CloudConfig和我请参阅使用getter的变量。这是类的样子:@RefreshScope不工作 - 春季启动

@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()。我可以在启动时加载属性,但我无法动态更新它们。任何人都可以告诉我做错了什么?如果不是制作这个配置类,我完全按照本教程描述的一切正常工作,但是我无法适应它到我的用例。任何人都可以告诉我缺少什么?

+0

如果其它类是单身,只有在启动时加载这些值,就不会在刷新后得到新鲜值。 –

+0

啊,但那些CloudConfig会的,我会用干将(CloudConfig.getCount()),其他类,因此他们会得到正确的价值观也。正确? – ion20

+0

如果你只使用getter方法来设置一次没有他们不会,他们也应该不是静态的,而是常规方法其他代理创建将失败的值。 –

回答

3

尝试使用@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工程(技术)上的@Configuration类,但它 可能导致令人惊讶的行为:例如,这并不意味着所有在该类中定义的@Beans都是@RefreshScope。 具体来说,除非它本身在 @RefreshScope(其中它将在刷新时重建并且它的 依赖项被重新注入,在这种情况下,依赖于这些bean的任何东西都不能依赖于它们在更新开始时被更新的 点他们将从刷新@Configuration重新初始化 )。