Spring bean properties are automatically updated from the properties file

I am using Spring 2.5.6. I have a bean whose properties are assigned from a properties file through PropertyPlaceholderConfigurer . I am wondering if the bean property can be updated when the property file changes. For example, there would be some batch process that checks the last modified date of the property file, and if it has changed, reload the bean.

I am wondering if there is anything that satisfies my requirements. If not, what is the best approach to solving this problem?

Thanks for your help.

+5
source share
5 answers

You can also explore the use of Spring PropertyOverrideConfigurer. You can reread the properties and reapply them in some polls / schedular bean.

It depends on how these configured beans options use. They can, for example, indirectly cache them somewhere themselves.

+3
source

If you need dynamic properties at runtime, perhaps another way is JMX.

+2
source

One way to do this is to embed the groovy console in the application. Here are some instructions. They were very easy to use, by the way, I have very little time, although I am not familiar with groovy.

Once you do this, you can just go into the console and change the values ​​in the real application on the fly.

0
source

You can try using a custom bean scope that recreates beans when the properties file changes. See my more extensive answer here .

0
source

Spring Cloud Config has the ability to change configuration properties at run time through the Spring Cloud Bus and use Cloud Config Server. A configuration of either .properties or .yml files is “rallied” from the Spring application and is instead retrieved from the Spring Cloud Config configuration server to which the application connects at startup. This Cloud Config Server retrieves the appropriate configuration .properties or .yml files from the GIT repository (there are other storage solutions, but GIT is the most common). You can then change the configuration at run time by changing the contents of the GIT repo configuration files. The cloud configuration server transfers changes to any Client Spring applications using the Spring Cloud Bus, and the configuration of these applications is updated without having to restart the application. Here you can find a working simple example: https://github.com/ldojo/spring-cloud-config-examples

0
source

Source: https://habr.com/ru/post/912221/


All Articles