本文介绍了如何清除ResourceBundle缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是使用Guice在Tomcat上运行的webapp。根据文档,我们应该能够调用 ResourceBundle.clearCache();
来清除ResourceBundle缓存,并且可能从bundle属性文件中获取最新信息。
This is a webapp running on Tomcat, using Guice. According to the docs we should be able to call ResourceBundle.clearCache();
to clear the ResourceBundle cache and presumably get the latest from the bundle property files.
我们还尝试了以下方法:
We have also tried the following:
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!
和
ResourceBundle.clearCache(this.class.getClassLoader());
我期待的行为是:
- 启动tomcat并点击页面并显示Hello World
- 将包含Hello World的属性文件更改为Goodbye Earth
- 使用servlet清除缓存
- 点击页面,期望看到'Goodbye Earth'
- Start up tomcat and hit a page and it says 'Hello World'
- Change the properties file containing 'Hello World' to 'Goodbye Earth'
- Clear the cache using a servlet
- Hit the page and expect to see 'Goodbye Earth'
所以问题是,ResourceBundle.clearCache()实际上是如何工作的?还有一些我们需要清除的通用文件缓存吗?
So question is, how is ResourceBundle.clearCache() actually working ? And is there some generic file cache we need to clear also ?
推荐答案
这对我有用:
ResourceBundle.clearCache();
ResourceBundle resourceBundle= ResourceBundle.getBundle("YourBundlePropertiesFile");
String value = resourceBundle.getString("your_resource_bundle_key");
注意:
- 不要使用静态resourceBundle属性,请使用
调用
方法。clearCache()
方法后的ResourceBundle.getBundle()
- ResourceBundle.clearCache() is added at Java 1.6
- Do not use a static resourceBundle property, use
ResourceBundle.getBundle()
method after invokingclearCache()
method.
这篇关于如何清除ResourceBundle缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!