Java Web - 多个属性文件加载

来源:互联网 发布:淘宝潮阳三角梅园艺 编辑:程序博客网 时间:2024/05/16 17:03

    在Java Web项目中,我们常常会遇到在项目启动时需要加载多个属性配置文件的情况,我的一种解决方法是将要加载的属性文件配置在根目录的一个配置文件中,然后在项目启动时将配置文件中配置的属性文件都装载至内存,以配置文件的key做标识,当我们需要获取属性文件中的值时,需要传两个key ,一个是配置文件的key,一个是要获取的属性文件的key。实现如下:

1、我们先在跟目录下建立一个配置文件needloads.properties ,配置需要加载的属性文件。内容如下

a=com/xx/xx/configs/a.properties
b=com/xx/xx/configs/b.properties

2、在对应的com/xx/xx/configs/目录下建立a.properties和b.properties两个属性文件。

a.properties内容:

sdds=\u4E2D\u56FD\u5171\u4EA7\u515A\u4E07\u5C81
sds=\u4E60\u8FD1\u5E73\u4E3B\u5E2D\u4E07\u5C81

b.properties内容:

xcc=\u793E\u4F1A\u4E3B\u4E49\u4E07\u5C81
xsd=\u4E2D\u534E\u4EBA\u6C11\u5171\u548C\u56FD\u4E07\u5C81

3、首先,我们需要一个装载和读取配置文件的工具类:

/** *  */import java.io.IOException;import java.io.InputStream;import java.util.Enumeration;import java.util.Iterator;import java.util.Map;import java.util.Properties;import org.apache.log4j.Logger;/** * @author pengl * 2013-11-18 */public class PropertiesExtra {private Logger _logger = Log4jUtil.getLogger();/**属性对象*/private Properties _properties = null;/**文件名,如果不传入文件路径默认为根*/private String fileName = "";/**文件路径,如:xx/xx/xx/*/private String path = "";public PropertiesExtra(String fileName)throws IOException{this.fileName = fileName;createProperties();}public PropertiesExtra(String fileName,String path)throws IOException{this.fileName = fileName;this.path     = path;createProperties();}/** * <code>读取配制文件</code> * @throws IOException */private void createProperties()throws IOException{try{_properties = new Properties();ClassLoader cl = Thread.currentThread().getContextClassLoader();InputStream is = cl.getResourceAsStream(this.path+this.fileName);_properties.load(is);is.close();}catch(IOException iox){_logger.warn("读取配制文件:"+this.path+this.fileName+"失败。"+iox.getMessage());throw iox;}catch(RuntimeException rux){_logger.warn("生成配制文件引用异常:"+rux.getMessage()+",配制文件路径:"+this.path+this.fileName);throw rux;}}public Properties getProperties(){return this._properties;}public Object clone(){return this._properties.clone();}public Enumeration elements(){return this._properties.elements();}public void setProperty(String key,String value){this._properties.setProperty(key,value);}public String getProperty(String key){return this._properties.getProperty(key);}public String toString(){try{    StringBuffer sb = new StringBuffer();    Iterator _iterator = _properties.entrySet().iterator();    while(_iterator.hasNext()){        Map.Entry  _entry = (Map.Entry)_iterator.next();        sb.append(_entry.getKey());        sb.append("=");        sb.append(_entry.getValue());        sb.append("\n");    }return sb.toString();}catch(Exception ex){_logger.warn(ex.getMessage());return ex.getMessage();}}}

4、读取根目录下的配置文件,加载配置文件中配置的多个属性文件至内存,并以配置文件中的key做为各个属性文件的标识。

/** *  */import java.io.IOException;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Properties;import org.apache.log4j.Logger;/** * @author pengl * 2013-11-18 * @版本 V1.0 * 描述:加载项目指定配置文件至内存中 */public class ReadPropertiesUtil {private static Properties properties = null;private static Map<String, Properties> proMap = new HashMap<String, Properties>();private static Logger _loger = Log4jUtil.getLogger();static {try {PropertiesExtra extra = new PropertiesExtra("needloads.properties");properties = extra.getProperties();Iterator itor = properties.entrySet().iterator();while(itor.hasNext()){    Map.Entry entry=(Map.Entry)itor.next();    String key = (String)entry.getKey();    String value = (String)entry.getValue();    proMap.put(key, new PropertiesExtra(value).getProperties());    _loger.info("配置文件:【"+value+"】成功加载至内存中...");} }catch (Exception e) {Log4jUtil.getLogger().warn("读取属性配置文件出错,请检查 【needloads.properties】文件 " + e.getMessage(), e);e.printStackTrace();}}/** * 根据数据文件名和KEY值读取相应的VALUE值* @Title: getProperty * @Description: TODO* @param prokey:needloads.properties文件中的key * @param key:属性文件中的key * @return void    返回类型 * @throws */public static String getProperty(String prokey,String key){String result = "";try {Properties pros = proMap.get(prokey);result = pros.getProperty(key);}catch (Exception e) {e.printStackTrace();Log4jUtil.getLogger().warn("配置文件读取失败,请查询原因: " + e.getMessage(), e);}return result;}}

3.测试

import java.io.IOException;/** *  * @author pengl * 2014-6-3 * 描述: */public class Main {public static void main(String[] args) throws IOException {Log4jUtil.getLogger().warn(ReadPropertiesUtil.getProperty("b", "xsd"));Log4jUtil.getLogger().warn(ReadPropertiesUtil.getProperty("a", "sdds"));}}

控制台打印:

[ReadPropertiesUtil.java:34] 2014-06-03 00:36:23 INFO  com.doone.util.ReadPropertiesUtil  - 配置文件:【com/doone/util/configs/b.properties】成功加载至内存中...[ReadPropertiesUtil.java:34] 2014-06-03 00:36:23 INFO  com.doone.util.ReadPropertiesUtil  - 配置文件:【com/doone/util/configs/a.properties】成功加载至内存中...[Main.java:16] 2014-06-03 00:36:23 WARN  com.doone.util.Main  - 中华人民共和国万岁[Main.java:17] 2014-06-03 00:36:23 WARN  com.doone.util.Main  - 中国共产党万岁

在项目启动时加载多个配置文件 可能会影响服务启动时间 和 内存

0 0
原创粉丝点击