PropertyUtils

package com.kuanrf.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * 读取,properties
 * 
 * @author chenlujun
 * @version [版本号, 2014年11月8日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class PropertyUtils {

    private static final String[] PATHS = { "/config/config.properties", "/config/redis.properties" };

    private static Logger logger = LogManager.getLogger(PropertyUtils.class);

    private static Properties props = null;

    private static long start = 0L;

    /**
     * <一句话功能简述> <功能详细描述> [参数说明]
     * 
     * @return void [返回类型说明]
     * @exception throws
     *                [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    private static void init() {
        long end = System.currentTimeMillis();
        // 为空时或加载超过1小时
        if (props == null || end - start >= 3600000) {
            start = end;

            InputStream in = null;
            props = new Properties();
            try {
                for (String PATH : PATHS) {
                    in = PropertyUtils.class.getResourceAsStream(PATH);
                    props.load(in);
                }
            } catch (IOException e) {
                logger.error("读取出错配置文件出错", e);
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                        logger.error("关闭IO异常", ex);
                    }
                }
            }
        }
    }

    /**
     * 获取属性文件值
     * 
     * @param key
     * @return
     */
    public static String getValue(String key) {
        init();
        return props.getProperty(key);
    }
}

你可能感兴趣的:(properties)