简体   繁体   English

@RefreshScope无法正常工作 - Spring Boot

[英]@RefreshScope not working - Spring Boot

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. 我遵循这里描述的方法: https//github.com/jeroenbellen/blog-manage-and-reload-spring-properties ,唯一的区别是在我的情况下,属性被用于多个类,所以我有将它们全部放在一个实用程序类CloudConfig并使用getter引用它的变量。 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() . 我在其他类中使用变量count ,如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. 请尝试使用@ConfigurationProperties eg 例如

@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: 来自spring cloud参考文档指出:

@RefreshScope works (technically) on an @Configuration class, but it might lead to surprising behaviour: eg it does not mean that all the @Beans defined in that class are themselves @RefreshScope. @RefreshScope(在技术上)在@Configuration类上工作,但它可能会导致令人惊讶的行为:例如,它并不意味着该类中定义的所有@Beans本身都是@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). 具体来说,依赖于那些bean的任何东西都不能依赖于它们在启动刷新时被更新,除非它本身位于@RefreshScope中(在刷新时会重建它并重新注入其依赖项,此时它们将被重新注入)从刷新的@Configuration重新初始化。

Anyone else facing this issue, please make sure the followings: 遇到此问题的任何其他人,请确保以下内容:

  1. Your controller is annotated with @RefreshScope 您的控制器使用@RefreshScope注释
  2. Spring boot actuator is added into your dependency, as it is the module which actually provides these endpoints: Spring启动器执行器被添加到您的依赖项中,因为它是实际提供这些端点的模块:

    org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-actuator

  3. Refresh endpoint has been updated to: 刷新端点已更新为:

    http://{ip_address}:{port}/actuator/refresh HTTP:// {IP_ADDRESS}:{端口} /致动器/刷新

  4. Refresh endpoint isn't enabled by default. 默认情况下不启用刷新端点。 You have to enable it explicitly in the bootstrap.properties file by adding the following line: 您必须通过添加以下行在bootstrap.properties文件中显式启用它:

    management.endpoints.web.exposure.include=*

I have enabled all the endpoints, while you can just enable the specific endpoints as well. 我已启用所有端点,同时您也可以启用特定端点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 引导 2.4.x - @RefreshScope 不工作 - Spring Boot 2.4.x - @RefreshScope is not working Spring @RefreshScope 不适用于 @Component - Spring @RefreshScope is not working with @Component 使用@RefreshScope @PostConstruct @PreDestroy进行Spring启动 - Spring boot with @RefreshScope @PostConstruct @PreDestroy spring boot @refreshscope 不更新数据源 - spring boot @refreshscope doesn't update datasource Spring Cloud Config Server并在整个Spring Boot应用程序上应用@RefreshScope - Spring Cloud Config Server and applying @RefreshScope on an entire Spring Boot application 如何在 spring boot + cloud 中禁用 refreshScope 运行状况指示器? - How can I disable the refreshScope health indicator in spring boot + cloud? 使用refreshscope在运行时刷新spring.active.profile-Spring Boot和Cloud - Refresh spring.active.profile at runtime using refreshscope - spring boot and cloud spring 启动,无法在运行时使用 spring 云配置更改属性值,@RefreshScope 不起作用 - spring boot, Not able to change the properties value at runtime using spring cloud config , @RefreshScope doesn't work 如何通过Spring Boot Integration测试中的@RefreshScope初始化bean? - How can I initialize a bean with @RefreshScope from Spring Boot Integration tests? Spring RefreshScope 与 SpEL 延迟评估 - Spring RefreshScope with SpEL deferred Evaluation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM