@RefreshScope 不起作用

编程入门 行业动态 更新时间:2024-10-12 05:52:29
本文介绍了@RefreshScope 不起作用 - Spring Boot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我遵循此处描述的方法:github/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: github/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 不起作用

本文发布于:2023-10-26 07:08:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529429.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   RefreshScope

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!