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

写一个类读配置文件可以在网上搜一下

2018年04月28日 ⁄ 综合 ⁄ 共 980字 ⁄ 字号 评论关闭

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

import org.apache.log4j.Logger;

public class PropertyUtil
{
    private static final Logger logger = Logger.getLogger(PropertyUtil.class);
   
    private static  Properties properties;

    public static Properties getProperties()
    {
        return properties;
    }

    public static void setProperties(Properties properties)
    {
        PropertyUtil.properties = properties;
    }
   
    public synchronized static String getPropertiesResource(String key)
    {
        if(null == properties)
        {
            properties = new Properties();
           
            try
            {
                InputStream is = PropertyUtil.class.getClassLoader().getResourceAsStream("systemConfig.properties");
                properties.load(is);
            }
            catch (Exception e)
            {
                logger.error("--------PropertyUtil-------",e);
            }
        }
        return properties.getProperty(key);
    }
}

 

 

配置文件

key=value

value不加引号

 

在action中使用

PropertyUtil.getPropertiesResource("key");

【上篇】
【下篇】

抱歉!评论已关闭.