转帖出处:http://blog.csdn.net/pengchua/archive/2007/12/21/1957069.aspx
http://commons.apache.org/configuration/apidocs/index.html
Commons Configuration provides a generic configuration interface which enables a Java application to
read configuration data from a variety of sources.
PropertiesConfiguration Loads configuration values from a properties file.
XMLConfiguration Takes values from an XML document.
INIConfiguration Loads the values from a .ini file as used by Windows.
PropertyListConfiguration Loads values from an OpenStep .plist file. XMLPropertyListConfiguration is
also available to read the XML variant used by Mac OS X.
JNDIConfiguration Using a key in the JNDI tree, can retrieve values as configuration properties.
BaseConfiguration An in-memory method of populating a Configuration object.
HierarchicalConfiguration An in-memory Configuration object that is able to deal with complex
structured data.
SystemConfiguration A configuration using the system properties
ConfigurationConverter Takes a java.util.Properties or an
org.apache.commons.collections.ExtendedProperties and converts it to a Configuration object.
1.读取XML文件中内容
- import java.util.List;
- import org.apache.commons.configuration.Configuration;
- import org.apache.commons.configuration.XMLConfiguration;
- public class XmlConfigurationExample {
- public static void main(String[] args) throws Exception {
- String resource = "com/discursive/jccook/configuration/global.xml";
- Configuration config = new XMLConfiguration(resource);
-
- List startCriteria = config.getList("start-criteria.criteria");
- int horsepower = config.getInt("horsepower");
- System.out.println( "Start Criteria: " + startCriteria );
- System.out.println(horsepower);
- }
- }
global.xml:
- <?xml version="1.0" encoding="ISO-8859-1" ?>
- <engine-config>
- <start-criteria>
- <criteria>
- Temperature Above -10 Celsius
- </criteria>
- <criteria>
- Fuel tank is not empty
- </criteria>
- </start-criteria>
- <volume>4 Liters</volume>
- <horsepower>42</horsepower>
- </engine-config>
2.读取properties文件
- import org.apache.commons.configuration.Configuration;
- import org.apache.commons.configuration.PropertiesConfiguration;
- public class PropertiesConfigurationExample {
- public static void main(String[] args) throws Exception {
- Configuration config = new PropertiesConfiguration(
- "com/discursive/jccook/configuration/global.properties" );
-
- System.out.println( "Speed: " + config.getFloat("speed"));
- System.out.println( "Names: " + config.getString("name"));
- }
- }
global.properties:
- threads.maximum=50
- threads.minimum=20
- timeout=15.52
- interactive=true
- color=red
- speed=50
- name=Default User
- email=default@email.com
- region=Earth
3.当有多个配置文件时,就利用ConfigurationFactory对象来访问多个不同的配置资源
ConfigurationFactory可以组合多个配置资源。然后我们就可以像访问单个资源文件一样来访问他们中的属性。首
先,我们需要创建一个xml文件来告诉工厂哪些文件将要被加载。下面是一个例:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<configuration>
<properties fileName="usergui.properties"/>
</configuration>
ConfigurationFactory的定义文件是一个普通的xml文件.根元素是configuration.他饱含的子元素制定了需要装载
的配置资源.properties是元素之一,他用来包含属性文件
- import java.net.URL;
- import java.util.List;
- import org.apache.commons.configuration.Configuration;
- import org.apache.commons.configuration.ConfigurationFactory;
- public class PropertiesXmlConfigurationExample {
- public static void main(String[] args) throws Exception {
- PropertiesXmlConfigurationExample example = new PropertiesXmlConfigurationExample();
- ConfigurationFactory factory = new ConfigurationFactory();
- URL configURL = example.getClass().getResource("additional-xml-configuration.xml");
- factory.setConfigurationURL( configURL );
-
- Configuration config = factory.getConfiguration();
-
- List startCriteria = config.getList("start-criteria.criteria");
- System.out.println( "Start Criteria: " + startCriteria );
-
- int horsepower = config.getInt("horsepower");
- System.out.println( "Horsepower: " + horsepower );
- }
- }
additional-xml-configuration.xml:
- <?xml version="1.0" encoding="ISO-8859-1" ?>
- <configuration>
- <properties fileName="global.properties"/>
- <xml fileName="global.xml"/>
- </configuration>
-
或者采用另外一种方法: 用到了:CompositeConfiguration,手动加上两个配置文件
- import org.apache.commons.configuration.CompositeConfiguration;
- import org.apache.commons.configuration.ConfigurationException;
- import org.apache.commons.configuration.PropertiesConfiguration;
- import org.apache.commons.configuration.XMLConfiguration;
- public class Test {
-
-
-
-
- public static void main(String[] args) throws ConfigurationException {
-
- CompositeConfiguration config = new CompositeConfiguration();
- config.addConfiguration(new PropertiesConfiguration(
- "com/discursive/jccook/configuration/global.properties" ));
- config.addConfiguration( new XMLConfiguration
- ("com/discursive/jccook/configuration/global.xml"));
- List startCriteria = config.getList("start-criteria.criteria");
- int horsepower = config.getInt("horsepower");
- System.out.println( "Start Criteria: " + startCriteria );
- System.out.println(horsepower);
-
- System.out.println( "Speed: " + config.getFloat("speed"));
- System.out.println( "Names: " + config.getString("name"));
- }
- }