这是一个使用 Guice 在 Tomcat 上运行的 Web 应用程序。根据文档我们应该能够调用ResourceBundle.clearCache();
清除 ResourceBundle 缓存并大概从包属性文件中获取最新的信息。
我们还尝试了以下方法:
Class klass = ResourceBundle.getBundle("my.bundle").getClass().getSuperclass();
Field field = klass.getDeclaredField("cacheList");
field.setAccessible(true);
ConcurrentHashMap cache = (ConcurrentHashMap) field.get(null);
cache.clear(); // If i debug here I can see the cache is now empty!
and
ResourceBundle.clearCache(this.class.getClassLoader());
我期望的行为是:
- 启动 tomcat 并点击一个页面,显示“Hello World”
- 将包含“Hello World”的属性文件更改为“Goodbye Earth”
- 使用 servlet 清除缓存
- 点击该页面并期待看到“再见地球”
所以问题是,ResourceBundle.clearCache() 实际上是如何工作的?是否还需要清除一些通用文件缓存?
这对我有用:
ResourceBundle.clearCache();
ResourceBundle resourceBundle= ResourceBundle.getBundle("YourBundlePropertiesFile");
String value = resourceBundle.getString("your_resource_bundle_key");
Notes:
-
ResourceBundle.clearCache() http://docs.oracle.com/javase/7/docs/api/java/util/ResourceBundle.html#clearCache(java.lang.ClassLoader)在Java 1.6中添加
- 不要使用静态resourceBundle属性,使用
ResourceBundle.getBundle()
调用后的方法clearCache()
method.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)