refreshing spring boot properties

醉酒当歌 提交于 2019-12-11 04:01:36

问题


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

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

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.

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?


回答1:


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();
}

(link: 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)



来源:https://stackoverflow.com/questions/26717187/refreshing-spring-boot-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!