commons configuration管理项目的配置文件
Posted 滴水之光
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了commons configuration管理项目的配置文件相关的知识,希望对你有一定的参考价值。
Commons Configutation
commons configuration可以很方便的访问配置文件和xml文件中的的内容。Commons Configuration 是为了提供对属性文件、XML文件、JNDI资源、来自JDBC Datasource数据的访问。
官方文档:http://commons.apache.org/proper/commons-configuration/
1、Maven中引入相关的jar
<!-- 配置文件读取 --> <dependency> <groupId>commons-configuration</groupId> <artifactId>commons-configuration</artifactId> <version>1.5</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.10</version> </dependency>
2、读取properties文件的内容
(1)新建一个icp.properties
name=parry port=21 flag=true users=Tom,parry
(2)工具类读取
package icp; import java.util.List; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy; public class Test { public static final String fileName = "icp.properties"; public static PropertiesConfiguration cfg = null; static { try { cfg = new PropertiesConfiguration(fileName); } catch (ConfigurationException e) { e.printStackTrace(); } // 当文件的内容发生改变时,配置对象也会刷新 cfg.setReloadingStrategy(new FileChangedReloadingStrategy()); } // 读String public static String getStringValue(String key) { return cfg.getString(key); } // 读int public static int getIntValue(String key) { return cfg.getInt(key); } // 读boolean 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); } }
(3)测试
public static void main(String[] args) { String name = Test.getStringValue("name"); System.out.println("name:" + name); int port = Test.getIntValue("port"); System.out.println("port:" + port); boolean flag = Test.getBooleanValue("flag"); System.out.println("flag:" + flag); List<String> users = (List<String>) Test.getListValue("users"); for (String user : users) { System.out.println("user:" + user); } }
(4)测试结果
3、读取XML配置文件
(1)新建一个XMl文件
<?xml version="1.0" encoding="UTF-8"?> <config> <database> <url>127.0.0.1</url> <port>1521</port> <login>admin</login> <password>pass</password> </database> </config>
(2)读取XML配置的工具文件
package icp; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.XMLConfiguration; import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy; public class XmlTest { public static final String fileName = "XMLProperties.xml"; public static XMLConfiguration cfg = null; static { try { cfg = new XMLConfiguration(fileName); } catch (ConfigurationException 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); } }
这里只是添加读取String 和读取Int的方法,其他的方法类似。
(3)测试
public static void main(String[] args) { String url = XmlTest.getStringValue("database.url"); System.out.println("url:" + url); int port =XmlTest.getIntValue("database.port"); System.out.println("port:"+port); }
(4)测试结果
由于项目中,经常有多个配置文件,最后提供一个工具类:
public class PropertiesUtil { public static Map<String, Object> configMap = new ConcurrentHashMap<String, Object>(); public static String getStringValue(String fileName, String key) { if (!configMap.containsKey(key)) { PropertiesUtil.initConfig(fileName); } if (fileName.endsWith(".properties")) { PropertiesConfiguration cfg = (PropertiesConfiguration) configMap.get(fileName); return cfg.getString(key); } else if (fileName.endsWith(".xml")) { XMLConfiguration cfg = (XMLConfiguration) configMap.get(fileName); return cfg.getString(key); } return null; } private static void initConfig(String fileName) { try { if (fileName.endsWith(".xml")) { XMLConfiguration cfg = new XMLConfiguration(fileName); configMap.put(fileName, cfg); } else if (fileName.endsWith(".properties")) { PropertiesConfiguration cfg = new PropertiesConfiguration(fileName); configMap.put(fileName, cfg); } } catch (ConfigurationException e) { e.printStackTrace(); } } }
测试:
public static void main(String[] args) { String name= PropertiesUtil.getStringValue("icp.properties", "name"); System.out.println(name); String url = PropertiesUtil.getStringValue("icp.xml", "database.url"); System.out.println(url); }
以上是关于commons configuration管理项目的配置文件的主要内容,如果未能解决你的问题,请参考以下文章
java:commons-configuration2与spring的集成
Failed obtaining configuration for Common.Logging from configuration section 'common/logging'
Commons Configuration2 - Quick start guide
已解决:java.lang.NoClassDefFoundError: org/apache/commons/configuration/Configuration