@RefreshScope 不起作用 - Spring Boot [英] @RefreshScope not working - Spring Boot

查看:244
本文介绍了@RefreshScope 不起作用 - Spring Boot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

@Configuration@RefreshScope公共类 CloudConfig {静态 volatile 整数计数;//20 秒@Value("${config.count}")public void setCount(int count) {this.count = 计数;}公共静态 int getCount() {返回计数;}}

并且我在其他类中使用变量 count,例如 CloudConfig.getCount().我能够在启动时加载属性就好了,但我无法动态更新它们.谁能告诉我我做错了什么?如果不是制作这个配置类,我完全按照教程描述的那样做,一切正常,但我无法将它适应我的用例.谁能告诉我我错过了什么?

解决方案

尝试使用 @ConfigurationProperties 代替.例如

@ConfigurationProperties(prefix="config")公共类 CloudConfig {私有整数计数;公共整数计数(){返回 this.count;}public void setCount(整数计数){this.count = 计数;}}

来自 spring cloud 的参考文档 指出:><块引用>

@RefreshScope 在 @Configuration 类上工作(技术上),但它可能会导致令人惊讶的行为:例如这并不意味着所有的该类中定义的@Beans 本身就是@RefreshScope.具体来说,任何依赖于这些 bean 的东西都不能依赖它们在启动刷新时更新,除非它本身在@RefreshScope(它将在刷新时重建,并且它的重新注入依赖项,此时它们将被重新初始化来自刷新的@Configuration).

I am following the approach described here: https://github.com/jeroenbellen/blog-manage-and-reload-spring-properties, the only difference is that in my case, the properties are being used in multiple classes so I have put them all in one utility class CloudConfig and I refer to its variables using the getters. This is what the class looks like:

@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;
    }

}

and I use the variable count in other classes like CloudConfig.getCount(). I am able to load the properties on bootup just fine but I am not able to dynamically update them on the fly. Can anyone tell what I am doing wrong? If instead of making this config class, I do exactly what the tutorial describes everything works fine but I am having trouble adapting it to my usecase. Can anybody tell what I am missing?

解决方案

Try using @ConfigurationProperties instead. e.g.

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

    private Integer count;

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

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

}

The reference doc from spring cloud states:

@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).

这篇关于@RefreshScope 不起作用 - Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆