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

读取配置文件有很多种方法,现就最常用的方法作以下总结:(其他会慢慢更新)

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 " ));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值