<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-124  評論-49  文章-56  trackbacks-0

    讀取配置文件有很多種方法,現就最常用的方法作以下總結:(其他會慢慢更新)

    configuration/ConfigurationFactory用法(pache.commons包下)

    本文:org.apache.commons.configuration.configuration
    其它:com.opensymphony.xwork2.config.configuration
                javax.security,auth.login.configuration
                net.sf.ehcache.config.configuration
                org.hibernate.cfg.configuration 等等,用方類同。


    1.讀取XML文件中內容
    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> 
    讀取:
    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);   
     }
       
    }

    2.讀取properties文件
    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  
    讀取:
    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"));   
      }
       
    }

    3.當有多個配置文件時,就利用ConfigurationFactory對象來訪問多個不同的配置資源

    ConfigurationFactory可以組合多個配置資源。然后我們就可以像訪問單個資源文件一樣來訪問他們中的屬性。

    首先,我們需要創建一個xml文件來告訴工廠哪些文件將要被加載。

    additional-xml-configuration.xml:

    <?xml version="1.0" encoding="ISO-8859-1" ?>   
    <configuration>   
      
    <properties fileName="global.properties"/>   
      
    <xml fileName="global.xml"/>   
    </configuration>

    方法1: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 );   
     }
       
    }

    方法2:或者采用另外一種方法:  用到了: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"));   
     }
       
    }


    Spring的ClassPathXmlApplicationContext類(ApplicationContext接口)  JDK中的Properties 類(Java.util

    1.讀取XML文件中內容

    (1)java bean (HelloBean.java)
    package com.test;
    public class HelloBean{
    private String helloWorld;
    public String getHelloWorld(){
    return helloWorld;
     }

    public void setHelloWorld(String helloWorld){
    this.helloWorld = helloWorld;
     }

    }
    (2)xml配置文件(beanConfig.xml)
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
    <beans>
     
    <bean id="helloBean" class="chb.demo.vo.HelloBean">
      
    <property name="helloWorld">
       
    <value>Hello!chb!</value>
      
    </property>
     
    </bean>
    </beans>
    (3)讀取xml文件
    <1>利用ClassPathXmlApplicationContext
    ApplicationContext context = new ClassPathXmlApplicationContext("beanConfig.xml");
     HelloBean helloBean 
    = (HelloBean)context.getBean("helloBean");
     System.out.println(helloBean.getHelloWorld());
    <2>利用FileSystemResource讀取
    Resource rs = new FileSystemResource("D:/software/tomcat/webapps/springWebDemo/WEB-INF/classes/beanConfig.xml");
      BeanFactory factory 
    = new XmlBeanFactory(rs);
      HelloBean helloBean 
    = (HelloBean)factory.getBean("helloBean");
      System.out.println(helloBean.getHelloWorld());

    2.讀取properties文件

    (1)HelloBean.java同上
    (2).properties配置文件(beanConfig.properties)
    helloBean.class=com.test.HelloBean
    helloBean.helloWorld=Hello!chb!
    (3)讀取.properties文件
    <1>利用spring讀取properties 文件
    屬性文件中的"helloBean"名稱即是Bean的別名設定,.class用于指定類來源。
    然后利用org.springframework.beans.factory.support.PropertiesBeanDefinitionReader來讀取屬性文件
    BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
      PropertiesBeanDefinitionReader reader 
    = new PropertiesBeanDefinitionReader(reg);
      reader.loadBeanDefinitions(
    new ClassPathResource("beanConfig.properties"));
      BeanFactory factory 
    = (BeanFactory)reg;
      HelloBean helloBean 
    = (HelloBean)factory.getBean("helloBean");
      System.out.println(helloBean.getHelloWorld());
    <2>利用java.util.Properties讀取屬性文件
      InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("beanConfig.properties");
      Properties p 
    = new Properties();
      
    try {
       p.load(inputStream);
      }
     catch (IOException e1) {
       e1.printStackTrace();
      }

    System.out.println(
    "class:"+p.getProperty("helloBean.class")+",value:"+p.getProperty("helloBean.helloWorld"));
    posted on 2009-11-04 17:14 junly 閱讀(2501) 評論(0)  編輯  收藏 所屬分類: tomcat/jboss/weblogic
    主站蜘蛛池模板: 永久免费av无码网站大全| 亚洲中文字幕第一页在线| 青娱乐在线免费观看视频| 亚洲精品乱码久久久久久蜜桃不卡| h视频在线观看免费网站| 边摸边吃奶边做爽免费视频网站| 国产亚洲人成网站观看| 一个人看www在线高清免费看| www在线观看免费视频| 亚洲午夜精品在线| 亚洲一区二区精品视频| 成人黄色免费网址| 成av免费大片黄在线观看| 亚洲无码一区二区三区| 亚洲AV综合色区无码一区 | 999国内精品永久免费观看| 日本系列1页亚洲系列| 78成人精品电影在线播放日韩精品电影一区亚洲 | 亚洲AV无码专区在线厂| 久久久久亚洲Av无码专| 亚洲国产人成精品| 在线免费视频一区二区| 99久久久国产精品免费牛牛四川| 看Aⅴ免费毛片手机播放| 亚洲免费视频观看| 久久久久久亚洲精品| 亚洲av片一区二区三区| 成全高清视频免费观看| 6080午夜一级毛片免费看6080夜福利| 尤物视频在线免费观看| 亚洲欧美日韩中文二区| 亚洲欧洲日本精品| 亚洲AV无码成人专区片在线观看| 免费一看一级毛片| 四虎在线视频免费观看| 真人做人试看60分钟免费视频| 免费毛片a线观看| 中文字幕乱码免费看电影| 伊人久久国产免费观看视频| 精品久久久久亚洲| 亚洲精品无码专区|