首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何清除ResourceBundle缓存

如何清除ResourceBundle缓存
EN

Stack Overflow用户
提问于 2012-03-22 10:02:27
回答 6查看 15K关注 0票数 4

这是一个运行在Tomcat上的webapp,使用Guice。根据文档,我们应该能够调用ResourceBundle.clearCache();来清除ResourceBundle缓存,并可能从包属性文件中获得最新的信息。

我们还尝试了以下几点:

代码语言:javascript
复制
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!

代码语言:javascript
复制
ResourceBundle.clearCache(this.class.getClassLoader());

我期待的行为是:

  1. 启动tomcat,点击一个页面,上面写着“Hello”
  2. 将包含“Hello”的属性文件更改为“再见地球”
  3. 使用servlet清除缓存
  4. 点击页面,期待看到“再见地球”

所以问题是,ResourceBundle.clearCache()实际上是如何工作的?我们还需要清除一些通用文件缓存吗?

EN

回答 6

Stack Overflow用户

发布于 2014-11-03 11:38:41

这对我起了作用:

代码语言:javascript
复制
ResourceBundle.clearCache();
ResourceBundle resourceBundle= ResourceBundle.getBundle("YourBundlePropertiesFile");
String value = resourceBundle.getString("your_resource_bundle_key");

备注:

  1. ResourceBundle.clearCache()是在Java1.6添加的
  2. 不要使用静态resourceBundle属性,在调用clearCache()方法后使用ResourceBundle.getBundle()方法。
票数 6
EN

Stack Overflow用户

发布于 2012-03-22 10:32:30

我认为您无法重新加载已经创建的ResourceBundle实例,因为它的内部控制类已经创建。您可以尝试将此作为初始化包的替代方法:

代码语言:javascript
复制
ResourceBundle.getBundle("my.bundle", new ResourceBundle.Control() {
    @Override
    public long getTimeToLive(String arg0, Locale arg1) {
        return TTL_DONT_CACHE;
    }
});
票数 5
EN

Stack Overflow用户

发布于 2013-07-02 14:11:12

我找到了这个解决方案(与tomcat一起工作):

  • 使用自定义ResourceBundle.Control (因为我需要UTF8)
  • 添加getTimeToLive作为“感知”描述
  • 强制重新加载标志
  • "ResourceBundle.clearCache“不工作

如何打电话:

代码语言:javascript
复制
ResourceBundle bundle = ResourceBundle.getBundle("yourfile", new UTF8Control());

自定义类:

代码语言:javascript
复制
public class UTF8Control extends Control
{
    public ResourceBundle newBundle(
        String baseName,
        Locale locale,
        String format,
        ClassLoader loader,
        boolean reload)
    throws IllegalAccessException, InstantiationException, IOException
    {
        // The below is a copy of the default implementation.
        String bundleName = toBundleName(baseName, locale);
        String resourceName = toResourceName(bundleName, "properties");
        ResourceBundle bundle = null;
        InputStream stream = null;

        // FORCE RELOAD because needsReload doesn't work and reload is always false
        reload = true;

        if (reload) {
            URL url = loader.getResource(resourceName);
            if (url != null) {
                URLConnection connection = url.openConnection();
                if (connection != null) {
                    connection.setUseCaches(false);
                    stream = connection.getInputStream();
                }
            }
        }
        else {
            stream = loader.getResourceAsStream(resourceName);
        }

        if (stream != null) {
            try {
                // Only this line is changed to make it to read properties files as UTF-8.
                bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
            }
            finally {
                stream.close();
            }
        }
        return bundle;
    }

    // ASK NOT TO CACHE
    public long getTimeToLive(String arg0, Locale arg1) {
        return TTL_DONT_CACHE;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9819999

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档