我写了一个PropertyUtils类(从互联网上),它将加载属性。
<bean id="propertiesUtil" class="com.myApp.PropertiesUtil" >
<property name="locations">
<list>
<value>classpath:myApp/myApp.properties</value>
</list>
</property>
</bean>
而一个 PropertiesUtil 类如下所示:
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);
}
}
稍后,我可以通过调用PropertiesUtil.getProperty()方法来获取属性。
但现在我想稍微修改一下,如果用户修改了myApp.properties文件,它应该重新加载。
可能我需要FileWatcher类。
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);
}
但是我有疑问:
- 如何使用classpath:myApp/myApp.properties创建File对象(因为不知道绝对路径)?
- 如何调用Spring来加载/传递新的myApp.properties给PropetisUtil类 [在onChange方法中]?