I have written a PropertyUtils class (from internet), which will load the Properties
<bean id="propertiesUtil" class="com.myApp.PropertiesUtil" >
<property name="locations">
<list>
<value>classpath:myApp/myApp.properties</value>
</list>
</property>
</bean>
and a PropertiesUtil class is like below
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);
}
}
Later, I can get the property by calling PropertiesUtil.getProperty() method.
But now I want to modify it slightly such that, If the myApp.properties get modified/changed by user, it should be loaded again
Probably I need FileWatcher class
public abstract class FileWatcher extends TimerTask {
private long timeStamp;
private File file;
public FileWatcher(File file) {
this.file = file;
this.timeStamp = file.lastModified();
}
@Override
public final void run() {
long timeStampNew = this.file.lastModified();
if (this.timeStamp != timeStampNew) {
this.timeStamp = timeStampNew;
onChange(this.file);
}
}
protected abstract void onChange(File newFile);
}
but my doubts are
- How do I create File object using classpath:myApp/myApp.properties (because absolute path is not known)
- How do I invoke/call spring to load/pass the new myApp.properties to PropetisUtil class [in onChange method].