令人耳目一新的弹簧靴属性 [英] refreshing spring boot properties

查看:31
本文介绍了令人耳目一新的弹簧靴属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 spring 集成 web 应用程序,它导入了一堆属性文件,如下所示:

I have a spring integration web application that imports a bunch of properties file like so:

<context:property-placeholder location="classpath*:*.properties" />

现在我正在将其转换为 Spring Boot 应用程序,并且正在使用配置类中的 @PropertySource 导入属性.我也在查看用于未来状态项目的 spring 云配置服务器.所以我的问题是,当属性文件更改时需要刷新的所有 bean 都需要 @RefreshScope 但此时我知道 xml 中没有等效的属性.我想我可以使用 scope="refresh" 应用程序抛出一个错误,说刷新"不是一个有效的范围.那么如何在属性文件更改时管理刷新 xml 定义的 bean.

Now I am converting it to a spring boot application and I am importing the the properties in using the @PropertySource in the config class. I am also looking at the spring cloud config server for a future state project. So my question is this, all of the beans that need to be refreshed when the properties file changes need @RefreshScope but there is no equivalent attribute in the xml at this point that I am aware of. I thought I could use scope="refresh" the application throw an error saying "refresh" is not a valid scope. So how do I manage refreshing the xml defined beans when the properties file changes.

我想我可以尝试捕获事件,然后执行 context.refresh();但它们是否是一种更好的方法来做到这一点,已经内置到 spring 中?

I'm thinking I could try and capture the event and then do a context.refresh(); but is their a better way to do this that is already built into spring?

推荐答案

要注册自定义Scope,您需要声明一个 bean 定义.如果您使用 @EnableAutoConfiguration,Spring Cloud Config 会为您执行此操作.如果不这样做,则需要手动创建它(XML 或非 XML).例如

To register a custom Scope you need to declare a bean definition. Spring Cloud Config does that for you if you use @EnableAutoConfiguration. If you don't then you need to create it manually (XML or not XML). E.g.

@Bean
public static RefreshScope refreshScope() {
    return new RefreshScope();
}

(链接:https://github.com/spring-cloud/spring-cloud-config/blob/1.0.0.M2/spring-cloud-config-client/src/main/java/org/springframework/cloud/autoconfigure/RefreshAutoConfiguration.java#L63)

这篇关于令人耳目一新的弹簧靴属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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