首页 > 代码库 > 工程启动加载.properties/.xml配置文件

工程启动加载.properties/.xml配置文件

工程目录:

技术分享

 

 

demo.properties

today=2017-06-10

 

PropertiesMap.java

package com.dzpykj.common.utils;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 将用户授权demo.properties文件读取到集合Map中
 */
@Component
public class PropertiesMap {
    
    private static final Logger logger = LoggerFactory.getLogger(PropertiesMap.class);
    public Map<String,String> getMap(){
        Properties props = new Properties();
        Map<String,String> map = new HashMap<String,String>();
        try {
            final String propertiesName = "demo";//必须与.properties文件路径一致
            InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(propertiesName+".properties");
            props.load(in);
            
            Iterator<String> it=props.stringPropertyNames().iterator();
            while(it.hasNext()){
                String key=it.next();
                map.put(key, props.getProperty(key));
                //System.out.println(key+":"+props.getProperty(key));
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            e.printStackTrace();
        }
        return map;
    }
}

 

工程启动加载.properties/.xml配置文件