加载配置信息

package com.ad.web.common;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.Logger;


@SuppressWarnings("serial")
public class PropertiesUtil extends HttpServlet {

     private static final Logger log = Logger.getLogger(PropertiesUtil.class);
    private static Map<String, String> propertiesMap = new HashMap<String, String>();;

    public void init(ServletConfig config) throws ServletException {
        String localpath=config.getInitParameter("localpath");
        String prefix = config.getServletContext().getRealPath("/");
        //String file = prefix+"local.properties";
        String filePath = prefix + localpath;
        Properties props = new Properties();
        try {
            FileInputStream istream = new FileInputStream(filePath);
            props.load(istream);
            istream.close();        
            for (Object key : props.keySet()) {
                String keyStr = key.toString();
                String value = props.getProperty(keyStr);
                propertiesMap.put(keyStr, value);
            }
            String path=propertiesMap.get("configdir").toString();
            LoadAll(path);
        
        } catch (IOException e) {

            return;
        }
    }

    public void LoadAll(String path) {
        Properties props = new Properties();
        try {
            
            File file = new File(path);//��ȡ�ļ���ַ
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isFile()) {
                    FileInputStream istream = new FileInputStream(path+"/"+files[i].getName());
                    props.load(istream);
                }
            }
            for (Object key : props.keySet()) {
                String keyStr = key.toString();
                String value = props.getProperty(keyStr);
                propertiesMap.put(keyStr, value);

            }
            
        } catch (Exception e) {
           log.error("load propertiesMap failure",e);
        } finally {
            props.clone();
        }

    }
    
    public static String getProperty(String key)
    {
        return propertiesMap.get(key);
    }

}


<!--web.xml  初始化PropertiesUtil方法 -->
    <servlet>
        <servlet-name>PropertiesUtil-init</servlet-name>
        <servlet-class>com.ad.web.common.PropertiesUtil</servlet-class>
        <init-param>
            <param-name>localpath</param-name>
            <param-value>WEB-INF/classes/local.properties</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
    </servlet>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值