key words : 热部署 动态读取配置文件 动态读取properties文件
come from
herepackage com.javaeye.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
/**
* @author Robbin Fan
*
*/
public class ConfigUtil {
private static Properties props = null;
private static File configFile = null;
private static long fileLastModified = 0L;
private static void init() {
URL url = ConfigUtil.class.getClassLoader().getResource("global.properties");
configFile = new File(url.getFile());
fileLastModified = configFile.lastModified();
props = new Properties();
load();
}
private static void load() {
try {
props.load(new FileInputStream(configFile));
fileLastModified = configFile.lastModified();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String getConfig(String key) {
if ((configFile == null) || (props == null)) init();
if (configFile.lastModified() > fileLastModified) load();
return props.getProperty(key);
}
}