系統開發的時候,都會涉及到一些設定檔的讀取,為方便從設定檔中讀取我們預先設定的值,我做了一個簡單設定檔讀取工具類。這個類主要基於apache開源項目commons-configuration,讓我們看下這個簡單工具類的實現。
/** * Copyright(c) 2005 zjhcsoft Techonologies, Ltd. * * History: * 2010-3-4 14:10:33 Created by Tiwen */package com.zjhcsoft.modules.configuration; import java.util.Iterator;import java.util.List; import org.apache.commons.configuration.*;import org.slf4j.Logger;import org.slf4j.LoggerFactory; import com.zjhcsoft.common.util.StringUtil; /** * 系統設定檔參擷取類 * * @author <a href="mailto:Tiwen@qq.com">Tiwen</a> * @version 1.0 2010-3-4 14:10:33 */public class SystemConfiguration{ private static Logger log = LoggerFactory.getLogger(SystemConfiguration.class); private static SystemConfiguration systemConfiguration = new SystemConfiguration(); private static List<String> locations; private SystemConfiguration() { } public static SystemConfiguration getInstance() { if (!isRead) { reload(); isRead = true; } return systemConfiguration; } /** * 重新載入設定檔 */ public static void reload() { if (locations == null) { log.warn("沒有設定檔,無法讀取"); return; } //預先清空原先的配置集合 compositeConfiguration.clear(); Configuration conf = null; int index = 0; int count = 0; for (String location : locations) { if (StringUtil.isValid(location)) { try { log.debug("開始讀取系統設定檔[{}]", location); if (location.toLowerCase().endsWith(".xml")) { conf = new XMLConfiguration(location); } else { conf = new PropertiesConfiguration(location); } compositeConfiguration.addConfiguration(conf); index = 0; for (Iterator<String> it = conf.getKeys(); it.hasNext(); ) { index++; count++; String key = it.next(); log.debug("設定檔資訊:[key = {} , value = {} ]", key, conf.getString(key)); } log.info("【{}】設定檔完畢,SystemConfig總共載入了 {} 個配置資訊", location, index); } catch (ConfigurationException e) { log.error("SystemConfiguration讀取系統配置出現錯誤:", e); } } } log.info("所有設定檔完畢,systemConfiguration總共載入了 {} 個配置資訊", count); } private static CompositeConfiguration compositeConfiguration = new CompositeConfiguration(); private static boolean isRead = false; /** * 讀取key,返回boolean結果,如果沒有找到對應索引值,則返回false * @param key 鍵 * @return boolean結果 */ public static boolean getBoolean(String key) { return getBoolean(key, false); } /** * 讀取key,返回boolean結果,如果沒有找到對應索引值,則返回預設值 * @param key 鍵 * @param defaultValue 預設值 * @return boolean結果 */ public static boolean getBoolean(String key, boolean defaultValue) { return compositeConfiguration.getBoolean(key, defaultValue); } /** * 讀取key,返回字串結果,如果沒有找到對應索引值,則返回null * @param key 鍵 * @return 索引值字串 */ public static String getString(String key) { return getString(key, null); } /** * 讀取key,返回字串結果,如果沒有找到對應索引值,則返回預設值 * @param key 鍵 * @param defaultValue 預設值 * @return 索引值字串 */ public static String getString(String key, String defaultValue) { return compositeConfiguration.getString(key, defaultValue); } /** * 讀取key,返回int結果,如果沒有找到對應索引值,則返回0 * @param key 鍵 * @return 索引值 */ public static int getInt(String key) { return getInt(key, 0); } /** * 讀取key,返回int結果,如果沒有找到對應索引值,則返回預設值 * @param key 鍵 * @param defaultValue 預設值 * @return 索引值 */ public static int getInt(String key, int defaultValue) { return compositeConfiguration.getInt(key, defaultValue); } /** * 讀取key,返回double結果,如果沒有找到對應索引值,則返回0f * @param key 鍵 * @return 索引值 */ public static double getDouble(String key) { return getDouble(key, 0d); } /** * 讀取key,返回double結果,如果沒有找到對應索引值,則返回預設值 * @param key 鍵 * @param defaultValue 預設值 * @return 索引值 */ public static double getDouble(String key, double defaultValue) { return compositeConfiguration.getDouble(key, defaultValue); } /** * 讀取key,返回key結果,如果沒有找到對應索引值,則返回0 * @param key 鍵 * @return 索引值 */ public static byte getByte(String key) { return getByte(key, (byte) 0); } /** * 讀取key,返回key結果,如果沒有找到對應索引值,則返回預設值 * @param key 鍵 * @param defaultValue 預設值 * @return 索引值 */ public static byte getByte(String key, byte defaultValue) { return compositeConfiguration.getByte(key, defaultValue); } /** * 讀取key,返回key結果,如果沒有找到對應索引值,則返回0f * @param key 鍵 * @return 索引值 */ public static float getFloat(String key) { return getFloat(key, 0f); } /** * 讀取key,返回key結果,如果沒有找到對應索引值,則返回預設值 * @param key 鍵 * @param defaultValue 預設值 * @return 索引值 */ public static float getFloat(String key, float defaultValue) { return compositeConfiguration.getFloat(key, defaultValue); } /** * 讀取key,返回key結果,如果沒有找到對應索引值,則返回0l * @param key 鍵 * @return 索引值 */ public static long getLong(String key) { return getLong(key, 0l); } /** * 讀取key,返回key結果,如果沒有找到對應索引值,則返回預設值 * @param key 鍵 * @param defaultValue 預設值 * @return 索引值 */ public static long getLong(String key, long defaultValue) { return compositeConfiguration.getLong(key, defaultValue); } /** * 讀取key,返回key結果,如果沒有找到對應索引值,則返回0 * @param key 鍵 * @return 索引值 */ public static short getShort(String key) { return getShort(key, (short) 0); } /** * 讀取key,返回key結果,如果沒有找到對應索引值,則返回預設值 * @param key 鍵 * @param defaultValue 預設值 * @return 索引值 */ public static short getShort(String key, short defaultValue) { return compositeConfiguration.getShort(key, defaultValue); } public List<String> getLocations() { return locations; } public void setLocations(List<String> locations) { this.locations = locations; }}
我根據commons-configuration組件特性,將設定檔讀取格式可以讀properties之外,也可以同時讀取xml格式。這個工具類屬於單例模式,也就是可以隨時共用給jvm任何對象使用,在spring容器下,只需將設定檔指定好就可以完成設定檔自動讀取,spring配置如下:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="systemConfiguration" class="com.zjhcsoft.modules.configuration.SystemConfiguration" init-method="getInstance"> <property name="locations"> <list> <value>test.properties</value> <value>test.xml</value> </list> </property> </bean></beans>
容器物件只需要在初始化時調用SystemConfiguration類的getInstance來擷取單例對象即可。然後將設定檔注入到locations成員屬性即可。我們在使用時無需在初始化該對象,即可立即取出設定檔中預設值。下面我舉個例子,準備test.properies設定檔和test.xml設定檔。
test.properies
abc=123tiwen=hello!tiwen
test.xml
<?xml version="1.0" encoding="UTF-8"?><conf> <node1>true</node1> <node2>10000</node2></conf>
java測試例子如下:
/** * Copyright(c) 2005 zjhcsoft Techonologies, Ltd. * * History: * 2010-3-4 14:10:33 Created by Tiwen */package com.zjhcsoft.modules.configuration; import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext; /** * 系統設定檔參擷取類 * * @author <a href="mailto:Tiwen@qq.com">Tiwen</a> * @version 1.0 2010-3-4 14:10:33 */public class SystemConfigurationTest{ public static void main(String []args){ ApplicationContext ctx = new ClassPathXmlApplicationContext("modules.xml"); System.out.println("properties config. key tiwen=" + SystemConfiguration.getString("tiwen")); System.out.println("properties config. key abc=" + SystemConfiguration.getString("abc")); System.out.println("xml config. key node1=" + SystemConfiguration.getBoolean("node1")); System.out.println("xml config. key node2=" + SystemConfiguration.getInt("node2")); }}
列印結果如下:
properties config. key tiwen=hello!tiwenproperties config. key abc=123xml config. key node1=truexml config. key node2=10000
很簡單吧,SystemConfiguration類還提供多種資料類型轉換介面,大家根據自己需要進行選擇,但記住,轉換類一定要對應正確的配置值,如“123”不可以使用getBoolean方法,否則會報出類型轉換錯誤運行時異常。
工具類完整代碼:http://download.csdn.net/detail/tiwen818/3873255
原創文章,如果轉載,請標註作者:田文 CSDN地址:http://blog.csdn.net/tiwen818