Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having spring bean properties refreshed automatically from properties file

Tags:

I'm using Spring 2.5.6. I have a bean whose properties are being assign from a property file via a PropertyPlaceholderConfigurer. I'm wondering whether its possible to have the property of the bean updated when the property file is modified. There would be for example some periodic process which checks the last modified date of the property file, and if it has changed, reload the bean.

I'm wondering if there is already something that satisfies my requirements. If not, what would be the best approach to solving this problem?

Thanks for your help.

like image 459
Il-Bhima Avatar asked Feb 27 '09 15:02

Il-Bhima


People also ask

How do I refresh properties in spring boot?

For Reloading properties, spring cloud has introduced @RefreshScope annotation which can be used for refreshing beans. Spring Actuator provides different endpoints for health, metrics. but spring cloud will add extra end point /refresh to reload all the properties.

How reload properties file in java?

If you want to be updated you need to check the resource from tome to time and reload the properties. It is a problem that you are loading properties from classpath. If you use file system you can check the last modified attribute of file and decide whether to reload it.


2 Answers

Might also look into useing Spring's PropertyOverrideConfigurer. Could re-read the properties and re-apply it in some polling/schedular bean.

It does depend on how the actual configured beans use these properties. They might, for example, indirectly cache them somewhere themself.

like image 190
jor Avatar answered Oct 16 '22 16:10

jor


If you want dynamic properties at runtime, perhaps another way to do it is JMX.

like image 29
duffymo Avatar answered Oct 16 '22 16:10

duffymo