我将属性文件加载到一个类中,然后在整个应用程序中使用该类来获取它们.
public class PropertiesUtil extends PropertyPlaceholderConfigurer {
private static Map properties = new HashMap();
@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中
class="com.test.PropertiesUtil">
classpath:test/test.properties
现在,我想确保每当更改属性文件时都会重新加载.
我有一个侦听器类,它与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 needs path to run, which I don’t want to hardcode
How do I tell spring to call properties to reload explicitly!
解决方法:
FileWatcher needs path to run, which I don’t want to hardcode
只需提供相对路径,例如在同一项目文件夹中的资源目录,就可以使用getResource()方法获得该相对路径.您还可以使用system property来访问user.dir这样的用户目录,这是使用工作目录.
File f = new File(System.getProperty("user.dir")+ "/test.properties");
System.out.println(f.getAbsolutePath());
2. How do I tell spring to call properties to reload explicitly!
目前,您的操作方式对我来说似乎还可以.可能还有其他方法,但是我看不到上述过程有任何缺陷.
标签:properties,file,spring,java
来源: https://codeday.me/bug/20191031/1976429.html