Java Web - 多個屬性文件加載

    在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  - 中國共產黨萬歲

在項目啓動時加載多個配置文件 可能會影響服務啓動時間 和 內存

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章