5

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.

Il-Bhima
  • 10,744
  • 1
  • 47
  • 51

5 Answers5

3

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.

jor
  • 711
  • 3
  • 7
  • It seems this is the most straightforward approach, and is what we'll end up doing. Thanks – Il-Bhima Mar 05 '09 at 08:38
  • I don't see the difference between PropertyOverrideConfigurer and PropertyPlaceholderConfigurer in that respect. There are differences how the properties are specified, but both are applied only once as a BeanFactoryPostProcessor before the actual beans are created. How chould they be reapplied? – Dr. Hans-Peter Störr Apr 02 '12 at 11:18
  • @hstoerr: With a PropertyPlaceholderConfigurer the settings are 'hidden' inside the spring (xml) configuration (with {..} expressions). With PropertyOverrideConfigurer, the configuration is in the format of beanName.property=value. So this can be re-applied on exising beans (e.g. PropertyOverrideConfigurer.processProperties – jor Apr 03 '12 at 13:42
  • @Il-Bhima Did this approach really work? In my experiments trying to reapply the configurer did nothing. – Dr. Hans-Peter Störr Apr 23 '12 at 14:22
  • @hstoerr I'm terribly sorry but I honestly don't remember. It was ages ago. I know that we did solve this problem but I don't remember how. Also I no longer have access to those code-bases. Sorry again :-( – Il-Bhima Apr 24 '12 at 21:31
2

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

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

You might try to use a custom scope for the bean that recreates beans on changes of the properties file. See my more extensive answer here.

Community
  • 1
  • 1
Dr. Hans-Peter Störr
  • 25,298
  • 30
  • 102
  • 139
0

Spring Cloud Config has facilities to change configuration properties at runtime via the Spring Cloud Bus and using a Cloud Config Server. The configuration or .properties or .yml files are "externalized" from the Spring app and instead retrieved from a Spring Cloud Config Server that the app connects to on startup. That Cloud Config Server retrieves the appropriate configuration .properties or .yml files from a GIT repo (there are other storage solutions, but GIT is the most common). You can then change configuration at runtime by changing the contents of the GIT repo's configuration files--The Cloud Config Server broadcasts the changes to any Client Spring applications via the Spring Cloud Bus, and those applications' configuration is updated without needing a restart of the app. You can find a working simple example here: https://github.com/ldojo/spring-cloud-config-examples

Lev
  • 317
  • 2
  • 9
0

One way to do this is to embed a groovy console in your application. Here's some instructions. They were very simple to do, btw - took me very little time even though I'm not that familiar with groovy.

Once you do that you can simply go into the console and change values inside the live application on the fly.

Steve B.
  • 55,454
  • 12
  • 93
  • 132