zoukankan      html  css  js  c++  java
  • java-读properties配置文件

    package com.test;
    
    import java.util.HashMap;
    import java.util.Map;
    import org.apache.commons.configuration.ConfigurationException;
    import org.apache.commons.configuration.PropertiesConfiguration;
    import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
    import org.apache.commons.lang.StringUtils;
    import org.apache.commons.lang.exception.*;
    
    
    import org.apache.logging.log4j.Logger;
    import org.apache.logging.log4j.LogManager;
    
    public class PropertyUtil {
        
        private static final String DEFAULT_PROPERTY = "conf/conf";
    //    private static final String DEFAULT_PROPERTY = "conf";
    
        private static final String PROPERTY_SUFIX = ".properties";
    
        private static final Map<String, PropertiesConfiguration> CONFIGURATIONS = new HashMap<String, PropertiesConfiguration>();
    
        static private Logger logger = LogManager.getLogger(PropertyUtil.class);
    
    
        private static PropertiesConfiguration getInstance(String name)
        {
            String fileName = getPropertyName(name);
    //        logger.debug("filename=" + fileName);
            PropertiesConfiguration config = CONFIGURATIONS.get(fileName);
            if (config == null)
            {
                try
                {    
    //                logger.debug("congfig is null");
                
                    config = new PropertiesConfiguration(fileName);
                    config.setReloadingStrategy(new FileChangedReloadingStrategy());
                    CONFIGURATIONS.put(fileName, config);
                }
                catch (ConfigurationException e)
                {
                    logger.error("cannot find property file for : " + name);
                    throw new RuntimeException("cannot find property file for : " + name);
                }
            }
            return config;
        }
    
        private static String getPropertyName(String name)
        {
            if (StringUtils.isBlank(name))
            {
                name = DEFAULT_PROPERTY;
            }
            return name.endsWith(PROPERTY_SUFIX) ? name : (name += PROPERTY_SUFIX);
        }
    
        public static PropertiesConfiguration getInstance()
        {
            return getInstance(null);
        }
    
        public static String getString(String key)
        {
            return getString(key, DEFAULT_PROPERTY);
        }
    
        public static String getString(String key, String propertyInstance)
        {
            return getInstance(propertyInstance).getString(key);
        }
    
        public static int getInt(String key)
        {
            return getInt(key, DEFAULT_PROPERTY);
        }
    
        public static int getInt(String key, String propertyInstance)
        {
            String value = getInstance(propertyInstance).getString(key);
            return Integer.parseInt(value);
        }
        public static  void main(String[] args)
        {
            PropertyUtil test = new PropertyUtil();
            System.out.print(test.getString("key"));
        }
    
    }
  • 相关阅读:
    ES集群性能调优链接汇总
    【转】dmesg 时间转换
    广师大笔记汉诺塔
    广师大python学习笔记求派的值
    155. 最小栈(c++)
    160. 相交链表(c++)
    论文 数据集总结
    论文阅读 总结 复习
    121. 买卖股票的最佳时机(c++)
    9. 回文数(c++)
  • 原文地址:https://www.cnblogs.com/Pierre-de-Ronsard/p/3986544.html
Copyright © 2011-2022 走看看