Java使用线程读取配置文件内容

配置起来很是方便简单,但是还不及注解使用的方便,但是可以了解一下,拓宽一下思路

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;
}

}

善于发现别人代码的优势,为我所用,技术的经验,积累总是很快的;

关注博客,分享实用技术

发布了412 篇原创文章 · 获赞 110 · 访问量 47万+

猜你喜欢

转载自blog.csdn.net/qq_37022150/article/details/105556736