最近在做一个基于tomcat的java应用。其中有个要求是手动修改某个配置文件后,不需要重启服务器就可以读取到该配置文件中配置项的最新值。我采用了每次读取都重新load一遍的方法,但是无法获取修改后的配置项的值。配置文件中只有一项:
log.create.time=2011-04-16 06\:44\:52代码:
public class LogConfigs {
final static Log log = LogFactory.getLog(LogConfigs.class);
public static String CONFIG_FILE = "log-config.properties"; public static String get(String tag) {
Properties p = new Properties();
InputStream in = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(CONFIG_FILE);
try {
p.load(in);
return p.getProperty(tag);
catch (IOException ex) {
ex.printStackTrace();
return null;
} finally {
try {
    in.close();
} catch (IOException e) {
    e.printStackTrace();
    log.error(e.getMessage(), e);
}
}
} public static void put(String key, String value) throws IOException {
if (key == null || key.equals(""))
return;
if (value == null || value.equals(""))
return;
Properties p = new Properties();
p.put(key, value);
String path = Thread.currentThread().getContextClassLoader()
.getResource(CONFIG_FILE).getPath();
File file = new File(URLDecoder.decode(path, "UTF-8"));
OutputStream os = new FileOutputStream(file);
p.store(os, null);
os.flush();
os.close();
}
}每次修改配置项使用 LogConfigs.put(key,value); 结果可以为配置项写入新值;
但是之后再用LogConfigs.get("log.create.time")读取,读出的还是第一次启动tomcat后读出的配置项的值,好像该项被缓存了一样。有什么方法能够实时读出更新以后的配置项的值呢? 不胜感激!!!