配置起来很是方便简单,但是还不及注解使用的方便,但是可以了解一下,拓宽一下思路
public class PropertiesUtil {
private static Properties prop = null;
static {
prop = new Properties();
loadData();
Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(new Runnable() {
public void run() {
loadData();
}
}, 5, 5, TimeUnit.SECONDS);
}
private static void loadData() {
InputStream in = null;
try {
in = Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties");
prop.load(in);
} catch (IOException e) {
System.out.println("读取application.properties出错");
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
System.out.println("Fail to close inputStream" + e);
}
}
}
}
public static String getPropertyByKey(String key) {
String value = prop.getProperty(key);
return (value== null || value =="") ? "" : value;
}
}