这是一个运行在Tomcat上的webapp,使用Guice。根据文档,我们应该能够调用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!和
ResourceBundle.clearCache(this.class.getClassLoader());我期待的行为是:
所以问题是,ResourceBundle.clearCache()实际上是如何工作的?我们还需要清除一些通用文件缓存吗?
发布于 2014-11-03 11:38:41
这对我起了作用:
ResourceBundle.clearCache();
ResourceBundle resourceBundle= ResourceBundle.getBundle("YourBundlePropertiesFile");
String value = resourceBundle.getString("your_resource_bundle_key");备注:
clearCache()方法后使用ResourceBundle.getBundle()方法。发布于 2012-03-22 10:32:30
我认为您无法重新加载已经创建的ResourceBundle实例,因为它的内部控制类已经创建。您可以尝试将此作为初始化包的替代方法:
ResourceBundle.getBundle("my.bundle", new ResourceBundle.Control() {
@Override
public long getTimeToLive(String arg0, Locale arg1) {
return TTL_DONT_CACHE;
}
});发布于 2013-07-02 14:11:12
我找到了这个解决方案(与tomcat一起工作):
如何打电话:
ResourceBundle bundle = ResourceBundle.getBundle("yourfile", new UTF8Control());自定义类:
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;
}
}https://stackoverflow.com/questions/9819999
复制相似问题