java中重新加载指定文件_java-更改后重新加载属性文件
我將屬性文件加載到一個類中,然后在整個應用程序中使用該類來獲取它們.
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
總結
以上是生活随笔為你收集整理的java中重新加载指定文件_java-更改后重新加载属性文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: “长袖暂徘徊”下一句是什么
- 下一篇: 现在70传承还值不值得买