`

ommon-configuration读取xml,properties文件

 
阅读更多
我们在实际的开发设计中,通常要读各种各样的配置文件,十分烦琐.然而commons-configuration-1.0.jar为我们提供了非常方便的操作方式实例如下: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);  
    //只有new一个XMLConfiguration的实例就可以了.  
    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对象来访问多个不同的配置资源


    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> 


4.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 {  
     
    /** 
    * @param args 
    * @throws ConfigurationException 
    */  
    public static void main(String[] args) throws ConfigurationException {  
    // TODO Auto-generated method stub  
    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"));  
     
    }  
     
    }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics