我有一个经常更新的属性文件。为了自动重新加载文件,我使用了公共(public)配置,但更改没有立即反射(reflect)在属性文件中。只有重新启动服务器后我才能看到更改,这意味着自动重新加载不起作用。我已经包含了所有需要的 jar 文件。
PropertiesConfiguration property = null;
try {
property = new PropertiesConfiguration(PROPERTY_FILENAME);
property.setReloadingStrategy(new FileChangedReloadingStrategy());
} catch (ConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
最佳答案
您必须提供文件的完整路径,否则它将无法检测到任何更改。如果该文件存在于您的类路径中并且您仅提供文件名,则会加载该文件,但不会检测到任何更改;您甚至可以删除该文件,应用程序将继续运行,就像什么都没发生一样。
不检测更改。删除文件而不停止应用程序
public class PropStandaloneConfigurationTest { public static void main(String[] args) throws ConfigurationException, InterruptedException { final PropertiesConfiguration config = new PropertiesConfiguration("app-project.properties"); config.setListDelimiter(','); config.setAutoSave(true); FileChangedReloadingStrategy reload = new FileChangedReloadingStrategy(); reload.setRefreshDelay(2000L); config.setReloadingStrategy(reload); Provider<Integer> cacheRetriesProvider = new Provider<Integer>() { @Override public Integer get() { return config.getInt("cache.retries"); } }; while (true) { System.out.println(cacheRetriesProvider.get()); Thread.sleep(3000L); } } }
检测更改并重新加载文件
... final PropertiesConfiguration config = new PropertiesConfiguration("/usr/local/ws/app-project/src/test/resources/app-project.properties"); ...
关于java - 使用公共(public)配置自动重新加载属性文件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28870379/