一、pom依賴添加java
<!-- 配置文件讀取 --> <dependency> <groupId>commons-configuration</groupId> <artifactId>commons-configuration</artifactId> <version>1.10</version> </dependency>
二、讀取.properties文件git
使用PropertiesConfiguration配置類,主要示例代碼以下:github
public static final String fileName = "test.properties"; public static PropertiesConfiguration cfg = null; static { try { cfg = new PropertiesConfiguration(fileName); } catch (ConfigurationException e) { e.printStackTrace(); } // 當文件的內容發生改變時,配置對象也會刷新 cfg.setReloadingStrategy(new FileChangedReloadingStrategy()); } /** * 讀String * @param key * @return */ public static String getStringValue(String key) { return cfg.getString(key); } /** * 讀int * @param key * @return */ public static int getIntValue(String key) { return cfg.getInt(key); } /** * 讀boolean * @param key * @return */ public static boolean getBooleanValue(String key) { return cfg.getBoolean(key); } /** * 讀List */ public static List<?> getListValue(String key) { return cfg.getList(key); } /** * 讀數組 */ public static String[] getArrayValue(String key) { return cfg.getStringArray(key); }
test.properties能夠以下定義:數組
name=king port=21 flag=true interest=guitar,piano
以後就能夠用給定的一些讀取方法操做了緩存
String name = CGPropetiesUtil.getStringValue("name");
System.out.println("String:" + name);
三、讀取.xml文件工具
使用XMLConfiguration配置類測試
主要代碼ui
public static final String fileName = "test.xml"; public static XMLConfiguration cfg = null; static { try { cfg = new XMLConfiguration(fileName); } catch (Exception e) { e.printStackTrace(); } // 配置文件 發生變化就從新加載 cfg.setReloadingStrategy(new FileChangedReloadingStrategy()); } public static String getStringValue(String key) { return cfg.getString(key); } public static int getIntValue(String key) { return cfg.getInt(key); }
test.xml定義以下:url
<?xml version="1.0" encoding="UTF-8"?> <config> <database> <url>127.0.0.1</url> <port>1521</port> <login>admin</login> <password>admin</password> </database> </config>
測試操做同上。spa
四、對於一個文件來講,他的操做就對應一個配置類,而不能使用同一個配置類操做多個文件,不然它只以讀取的第一個爲操做對象。針對這種狀況,能夠寫成一個通用的讀取工具,簡單示例以下:
public class CGCommonUtil { /** * 一個文件對應一個Configuration */ public static Map<String, Configuration> configMap = new ConcurrentHashMap<String, Configuration>(); /** * 文件後綴 */ private static final String SUFFIX_PROPERTIES = ".properties"; private static final String SUFFIX_XML = ".xml"; public static Configuration getConfig(String fileName) { if (!configMap.containsKey(fileName)) { CGCommonUtil.initConfig(fileName); } Configuration cfg = configMap.get(fileName); if (null == cfg) { throw new IllegalArgumentException("cfg is null"); } return cfg; } private static void initConfig(String fileName) { Configuration cfg = null; try { if (fileName.endsWith(SUFFIX_XML)) { cfg = new XMLConfiguration(fileName); } else if (fileName.endsWith(SUFFIX_PROPERTIES)) { cfg = new PropertiesConfiguration(fileName); } } catch (ConfigurationException e) { e.printStackTrace(); } if (null != cfg) { configMap.put(fileName, cfg); } else { System.out.println("cfg is null"); } } /** * 讀String * @param key * @return */ public static String getStringValue(Configuration cfg, String key) { return cfg.getString(key); } /** * 讀int * @param key * @return */ public static int getIntValue(Configuration cfg, String key) { return cfg.getInt(key); } /** * 讀boolean * @param key * @return */ public static boolean getBooleanValue(Configuration cfg, String key) { return cfg.getBoolean(key); } /** * 讀List */ public static List<?> getListValue(Configuration cfg, String key) { return cfg.getList(key); } /** * 讀數組 */ public static String[] getArrayValue(Configuration cfg, String key) { return cfg.getStringArray(key); } public static void main(String[] args) { Configuration config = getConfig("test.properties"); String name1 = getStringValue(config, "name"); } }
以上能夠做爲一個通用的讀取工具,爲每個文件都設置了一個相應的配置操做類,若是考慮本地緩存影響會影響內存的話能夠考慮定時刪除緩存數據操做
五、Configuration讀取文件源原
入口類ConfigurationUtils
主要涉及的方法以下:
locateFromClasspath方法:
static URL locateFromClasspath(String resourceName) { URL url = null; ClassLoader loader = Thread.currentThread().getContextClassLoader(); if (loader != null) { url = loader.getResource(resourceName); if (url != null) { LOG.debug("Loading configuration from the context classpath (" + resourceName + ")"); } } if (url == null) { url = ClassLoader.getSystemResource(resourceName); if (url != null) { LOG.debug("Loading configuration from the system classpath (" + resourceName + ")"); } } return url; }
首先它經過獲取了當前線程的一個類加載器,經過加載器的getResouce方法去類加載器找到resourceName這個文件
getResouce方法:
public URL getResource(String name) { URL url; if (parent != null) { url = parent.getResource(name); } else { url = getBootstrapResource(name); } if (url == null) { url = findResource(name); } return url; }
先去父節點的loader去加載資源文件,若是找不到,則會去BootstrapLoader中去找,若是仍是找不到,才調用當前類的classLoader去找。這也就是JDK類加載的雙親委派模型。
getInputStream方法:
public InputStream getInputStream(URL url) throws ConfigurationException { File file = ConfigurationUtils.fileFromURL(url); if (file != null && file.isDirectory()) { throw new ConfigurationException("Cannot load a configuration from a directory"); } else { try { return url.openStream(); } catch (Exception var4) { throw new ConfigurationException("Unable to load the configuration from the URL " + url, var4); } } }
調用url的openStream()方法去得到此文件的輸入流