使用common-configuration读取xml,properties文件

 

转帖出处: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文件中内容

 

  1. import java.util.List;  
  2. import org.apache.commons.configuration.Configuration;  
  3. import org.apache.commons.configuration.XMLConfiguration;  
  4. public class XmlConfigurationExample {  
  5.  public static void main(String[] args) throws Exception {  
  6.   String resource = "com/discursive/jccook/configuration/global.xml";  
  7.   Configuration config = new XMLConfiguration(resource);  
  8.   //只有new一个XMLConfiguration的实例就可以了.  
  9.   List startCriteria = config.getList("start-criteria.criteria");  
  10.   int horsepower = config.getInt("horsepower");  
  11.   System.out.println( "Start Criteria: " + startCriteria );  
  12.   System.out.println(horsepower);  
  13.  }  
  14. }  

 

global.xml:

 

  1. <?xml version="1.0" encoding="ISO-8859-1" ?>  
  2. <engine-config>  
  3.  <start-criteria>  
  4.   <criteria>  
  5.    Temperature Above -10 Celsius  
  6.   </criteria>  
  7.   <criteria>  
  8.    Fuel tank is not empty  
  9.   </criteria>  
  10.  </start-criteria>  
  11.  <volume>4 Liters</volume>  
  12.  <horsepower>42</horsepower>  
  13. </engine-config>  

 

2.读取properties文件

 

 

  1. import org.apache.commons.configuration.Configuration;  
  2. import org.apache.commons.configuration.PropertiesConfiguration;  
  3. public class PropertiesConfigurationExample {  
  4.  public static void main(String[] args) throws Exception  {  
  5.   Configuration config = new PropertiesConfiguration(   
  6. "com/discursive/jccook/configuration/global.properties" );  
  7.     
  8.   System.out.println( "Speed: " + config.getFloat("speed"));  
  9.   System.out.println( "Names: " + config.getString("name"));  
  10.   }  
  11. }  

 

global.properties:

 

 

  1. threads.maximum=50  
  2. threads.minimum=20  
  3. timeout=15.52  
  4. interactive=true  
  5. color=red  
  6. speed=50  
  7. name=Default User  
  8. email=default@email.com  
  9. region=Earth  

 

 

3.当有多个配置文件时,就利用ConfigurationFactory对象来访问多个不同的配置资源

ConfigurationFactory可以组合多个配置资源。然后我们就可以像访问单个资源文件一样来访问他们中的属性。首

先,我们需要创建一个xml文件来告诉工厂哪些文件将要被加载。下面是一个例:
 <?xml version="1.0" encoding="ISO-8859-1" ?> 
<configuration>
  <properties fileName="usergui.properties"/>
</configuration>
ConfigurationFactory的定义文件是一个普通的xml文件.根元素是configuration.他饱含的子元素制定了需要装载

的配置资源.properties是元素之一,他用来包含属性文件

 

 

  1. import java.net.URL;  
  2. import java.util.List;  
  3. import org.apache.commons.configuration.Configuration;  
  4. import org.apache.commons.configuration.ConfigurationFactory;  
  5. public class PropertiesXmlConfigurationExample {  
  6.  public static void main(String[] args) throws Exception {  
  7.   PropertiesXmlConfigurationExample example = new PropertiesXmlConfigurationExample();  
  8.   ConfigurationFactory factory = new ConfigurationFactory();  
  9.   URL configURL = example.getClass().getResource("additional-xml-configuration.xml");  
  10.   factory.setConfigurationURL( configURL );  
  11.     
  12.   Configuration config = factory.getConfiguration();  
  13.     
  14.   List startCriteria = config.getList("start-criteria.criteria");  
  15.   System.out.println( "Start Criteria: " + startCriteria );  
  16.     
  17.   int horsepower = config.getInt("horsepower");  
  18.   System.out.println( "Horsepower: " + horsepower );  
  19.  }  
  20. }  

 

 

additional-xml-configuration.xml:

 

 

  1. <?xml version="1.0" encoding="ISO-8859-1" ?>  
  2. <configuration>  
  3.   <properties fileName="global.properties"/>  
  4.   <xml fileName="global.xml"/>  
  5. </configuration>  
  6.    

 


或者采用另外一种方法:  用到了:CompositeConfiguration,手动加上两个配置文件

  1. import org.apache.commons.configuration.CompositeConfiguration;  
  2. import org.apache.commons.configuration.ConfigurationException;  
  3. import org.apache.commons.configuration.PropertiesConfiguration;  
  4. import org.apache.commons.configuration.XMLConfiguration;  
  5. public class Test {  
  6.  /** 
  7.   * @param args 
  8.   * @throws ConfigurationException  
  9.   */  
  10.  public static void main(String[] args) throws ConfigurationException {  
  11.   // TODO Auto-generated method stub  
  12.   CompositeConfiguration config = new CompositeConfiguration();  
  13.   config.addConfiguration(new PropertiesConfiguration(   
  14. "com/discursive/jccook/configuration/global.properties" ));  
  15.   config.addConfiguration( new XMLConfiguration  
  16. ("com/discursive/jccook/configuration/global.xml"));  
  17.   List startCriteria = config.getList("start-criteria.criteria");  
  18.   int horsepower = config.getInt("horsepower");  
  19.   System.out.println( "Start Criteria: " + startCriteria );  
  20.   System.out.println(horsepower);  
  21.     
  22.   System.out.println( "Speed: " + config.getFloat("speed"));  
  23.   System.out.println( "Names: " + config.getString("name"));  
  24.  }  
  25. }  

 

 

你可能感兴趣的:(configuration)