标签:
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; /** * 加载配置文件 * */ public class PropertyFactory { private static Log logger = LogFactory.getLog(PropertyFactory.class); private static java.util.Properties pros = new java.util.Properties(); public PropertyFactory(List<String> filePaths) { if(filePaths!=null){ for(int i=0;i<filePaths.size();i++){ String filePath = filePaths.get(i); InputStream in = null; URL url = Thread.currentThread().getContextClassLoader().getResource(filePath); if (null == url) { url = this.getClass().getClassLoader().getResource(filePath); } if(null == url){ url = this.getClass().getResource(filePath); } //解决文件路径可能出现空格的问题 String path = url.getFile(); if(!"".equals(path)){ path = path.replace("%20", " "); } try { in = new BufferedInputStream(new FileInputStream(path)); pros.load(in); } catch (Exception e) { logger.error("加载配置文件出错 :\n", e); } finally { if(in != null) { try { in.close(); } catch (IOException e) { logger.error("关闭输入流出错 :\n", e); } } } } } } public static String getProperty(String key) { return pros.getProperty(key); } public void setProperty(String key, String value) { pros.setProperty(key, value); } }
在配置文件中加入key,value
需要在spring配置文件注入 applicationContext.xml
<!-- 读取配置文件类 -->
<bean id="propertyFactory" class="com.creditease.autoloan.common.PropertyFactory">
<constructor-arg>
<list>
<value>/config.properties</value>
<value>/global.properties</value>
</list>
</constructor-arg>
</bean>
用的时候直接调用 PropertyFactory.getProperty("xxx");
标签:
原文地址:http://www.cnblogs.com/xiaoxiaozhu/p/4262325.html