I am loading Properties files to One class and then using the class throughout the application to get them.
public class PropertiesUtil extends PropertyPlaceholderConfigurer {
private static Map<String, String> properties = new HashMap<String, String>();
@Override
protected void loadProperties(final Properties props) throws IOException {
super.loadProperties(props);
for (final Object key : props.keySet()) {
properties.put((String) key, props.getProperty((String) key));
}
}
public String getProperty(final String name) {
return properties.get(name);
}
}
and in ApplicationContext.xml
<bean id="propertiesUtil"
class="com.test.PropertiesUtil">
<property name="locations">
<list>
<value>classpath:test/test.properties</value>
</list>
</property>
</bean>
Now I want to make sure that Properties file is reloaded whenever it’s changed.
I have one listener class which initializes along with tomcat server.
and I have written below logic for file watcher
TimerTask task = new FileWatcher(new File("c:\\temp-reb\\config\\config.properties")) {
/*
* (non-Javadoc)
* @see com.belgacom.rosy.rebecca.utils.FileWatcher#onChange(java.io.File)
*/
@Override
protected void onChange(File file) {
loadServiceProperties(file);
loadMetadata();
}
};
Timer timer = new Timer();
timer.schedule(task, new Date(), Long.valueOf(properties.getProperty("properties.file.timer.schedule"))); // repeat the check every second
problem is
- FileWatcher needs path to run, which I don’t want to hardcode
- How do I tell spring to call properties to reload explicitly!
Just give the relative path like a resource directory in same project folder, which you can get using the
getResource()method. You can also use the system property to access likeuser.dirwhich is the use working directory.The way you are doing it currently seems ok to me. There might be other ways as well, but I don’t see any flaw in the above process.