I have a property file and using Spring property place holder, I set values to the Spring beans. Now, this property file may be modified during the run time. Is there a way to refresh the properties of the Spring beans with this newly modified property value? Especially, I have many singleton beans? How can I refresh them with the new values? Is there already a solution to this or should it be custom coded? If it doesn’t already exist, can someone please give the best approach to achieve this? Thanks!
PS: My application is a batch application. I use Spring based Quartz configuration to schedule the batches.
I’ll leave this in for reference, but the updated answer is below the divider:
Well the
ConfigurableApplicationContextinterface contains a refresh() method, which should be what you want, but the question is: how to access that method. Whichever way you do it, you’ll start with a bean that has a dependency of typeConfigurableApplicationContext:Now the two basic options I’d suggest would be to either
Referring to comments: since it seems impossible to refresh the entire context, an alternative strategy would be to create a properties factory bean and inject that into all other beans.
You could inject this properties bean into all your other beans, however, you would have to be careful to always use prototype scope. This is particularly tricky inside singleton beans, a solution can be found here.
If you don’t want to inject lookup methods all over the place, you could also inject a
PropertyProviderbean like this: