如何清除ResourceBundle缓存 [英] How to clear ResourceBundle cache

查看:218
本文介绍了如何清除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());

我期待的行为是:


  1. 启动tomcat并点击页面并显示Hello World

  2. 将包含Hello World的属性文件更改为Goodbye Earth

  3. 使用servlet清除缓存

  4. 点击页面,期望看到'Goodbye Earth'

  1. Start up tomcat and hit a page and it says 'Hello World'
  2. Change the properties file containing 'Hello World' to 'Goodbye Earth'
  3. Clear the cache using a servlet
  4. 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");

注意:


  1. ResourceBundle.clearCache()

  2. 不要使用静态resourceBundle属性,请使用
    调用 clearCache()
    方法后的ResourceBundle.getBundle()
    方法。

  1. ResourceBundle.clearCache() is added at Java 1.6
  2. Do not use a static resourceBundle property, use ResourceBundle.getBundle() method after invoking clearCache() method.

这篇关于如何清除ResourceBundle缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆