ResourceBundle定时清除缓冲重新加载配置文件
2017-06-14 本文已影响0人
隔壁付叔叔
package com.net.util;
import java.util.Locale;
import java.util.ResourceBundle;
public class ResourceTester {
private final static MyResourceBundleControl ctl = new MyResourceBundleControl();
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
System.out.println(getBundle().getString("bao"));
Thread.sleep(5000);
System.out.println(getBundle().getString("bao"));
}
public static ResourceBundle getBundle() {
return ResourceBundle.getBundle("config", Locale.getDefault(), ctl);
}
/**
* 重载控制器
*/
private static class MyResourceBundleControl extends ResourceBundle.Control {
@Override
public long getTimeToLive(String baseName, Locale locale) {
return 1000;
}
@Override
public boolean needsReload(String baseName, Locale locale,
String format, ClassLoader loader, ResourceBundle bundle,
long loadTime) {
return true;
}
}
}