现在的位置: 首页 > 综合 > 正文

PropertiesUtils

2017年10月09日 ⁄ 综合 ⁄ 共 999字 ⁄ 字号 评论关闭

或许大家都常从属性文件中获得内容,但每次都写面向过程的那么多代码,这次写个工具类

以便以后可以重用代码,提高代码的效率

package com.xq.util;

import java.util.HashMap;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PropertiesUtils {
    
    
    private static final Logger logger = LoggerFactory.getLogger(PropertiesUtils.class);
    
    private static Properties properties;
    
    private static HashMap<String, String> propertiesKeys = new HashMap<String,String>();

    
    public static String getPropertyValue(String key) {
        if (propertiesKeys.containsKey(key)) {
            return propertiesKeys.get(key);
        } else {
            try {
                properties = new Properties();
                properties.load(Thread.currentThread().getContextClassLoader()
                        .getResourceAsStream("conf/setting.properties"));
                String value = properties.getProperty(key);
                propertiesKeys.put(key, value);
                return value;
            } catch (Exception e) {
                logger.warn("read setting,properties error",e);
            }
        }
        return null;
    }

}

抱歉!评论已关闭.