我将属性文件加载到一个类,然后在整个应用程序中使用该类来获取它们。
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);
}
}
并在 ApplicationContext.xml
<bean id="propertiesUtil"
class="com.test.PropertiesUtil">
<property name="locations">
<list>
<value>classpath:test/test.properties</value>
</list>
</property>
</bean>
现在我想确保属性文件在更改时重新加载。
我有一个与 tomcat 服务器一起初始化的侦听器类。我已经为文件观察器编写了以下逻辑
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
问题是
- FileWatcher 需要运行路径,我不想硬编码
- 我如何告诉 spring 调用属性以显式重新加载!