1. 程式人生 > >工程啟動加載.properties/.xml配置文件

工程啟動加載.properties/.xml配置文件

asstream pri back 文件 extc factory 工程 type loader

工程目錄:

技術分享

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配置文件