aspose file tools*
The moose likes Spring and the fly likes Can we Reload the property file when get modified in spring? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Can we Reload the property file when get modified in spring?" Watch "Can we Reload the property file when get modified in spring?" New topic
Author

Can we Reload the property file when get modified in spring?

kiran nyala
Ranch Hand

Joined: Sep 21, 2009
Posts: 47

Hi
Using a standard spring PropertyPlaceholderConfigurer, properties will be read from a file. If the property file modified we need to shut down the application context and launch a new one to get new values from property file.
Can we do this with out relaunch application context(or restart server).
For now my code and configuration is like below

//PropertyUtils.java


Spring.xml


C:/properties/conf.properties


//TestService.java



Output: Alpha

If I change the value of USER_NAME to Beta in property file i need to restart the context to get the new value assigned. But can I do this with out restarting the server or context in spring?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

kiran nyala wrote: But can I do this with out restarting the server or context in spring?


Not easily, and even harder with a PropertyPlaceHolderConfigurer since these replacements are done on the BeanDefinition before the bean is even instantiated. Making changes then to the singleton beans into which these properties were injected at runtime is tricky.

There are people out there that have made this sort of work you would have to google around. What kind of properties are actually changing at runtime? Perhaps it makes more sense to store these in a database instead and make use of the @Cacheable annotation to expire it every so often so often or on some sort of event (like updating the db)


[How To Ask Questions][Read before you PM me]
rakesh kumar nampally
Ranch Hand

Joined: Feb 03, 2007
Posts: 46
Hi,

I have found this, probably it might be useful:

http://www.smilingleo.net/post/2010-11-25/3740365

Thanks
Rakesh.


thanks & regards, Rakesh
rakesh kumar nampally
Ranch Hand

Joined: Feb 03, 2007
Posts: 46
Hi,

Alternatively spring is providing the facility to reload the property files using
ReloadableResourceBundleMessageSource class.

An alternative implementation of a MessageSource is class ReloadableResourceBundleMessageSource. This class automatically reloads messages from resource bundles, according to a setup policy. The property cacheSeconds is used to specify the time (in seconds) a loaded resource bundle is kept before checking for a new reload. A value of -1 keeps resource bundle cached forever. A value of 1 or more is recommended for good performance in message resolution. This class also accepts file base prefixes in basenames (e.g. classpath:, file.

example:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages"/>
<property name="cacheSeconds" value="1"/>
</bean>

http://docs.spring.io/spring/docs/3.2.6.RELEASE/javadoc-api/org/springframework/context/support/ReloadableResourceBundleMessageSource.html
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

The ReloadableMessageSource has been used for this purpose. As has Java 7's WatchService or pre-Java 7 the Apache commons-configuration library, to watch a file to see if it's changed. However the ReloadableMessageSource was designed more for resolving messages, with support for the parameterization and internationalization of those messages, although it could be used here as well.

The original question though to do this with a PropertyPlaceholderConfigurer remains same. You would have to get a little more creative, if you are set on that path, here is one guys solution http://www.morgan-design.com/2012/08/reloadable-application-properties-with.html.

The @Cacheable abstraction I mentioned before can wrap any method even one that just reads a properties file from the file system (or a db look-up). If put into a helper bean I think it achieves the goal much more easily. Alternatively a static utility class with WatchService on the properties file would also work, and would be less complex. Either of the last 2 solutions allow you to be a little more flexible with your refresh policy as well.

kiran nyala
Ranch Hand

Joined: Sep 21, 2009
Posts: 47

Thank you all !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we Reload the property file when get modified in spring?