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 >
读取:
< 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);
}
}
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 protected]
region=Earth
读取:
threads.minimum=20
timeout=15.52
interactive=true
color=red
speed=50
name=Default User
[email protected]
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"));
}
}
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 >
< 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 );
}
}
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"));
}
}
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)
public class HelloBean {
private String helloWorld;
public String getHelloWorld(){
return helloWorld;
}
public void setHelloWorld(String helloWorld){
this.helloWorld = helloWorld;
}
}
<?
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文件
<! 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 >
<1>利用ClassPathXmlApplicationContext
ApplicationContext context
=
new
ClassPathXmlApplicationContext(
"
beanConfig.xml
"
);
HelloBean helloBean = (HelloBean)context.getBean( " helloBean " );
System.out.println(helloBean.getHelloWorld());
<2>利用FileSystemResource读取
HelloBean helloBean = (HelloBean)context.getBean( " helloBean " );
System.out.println(helloBean.getHelloWorld());
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());
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文件
helloBean.helloWorld=Hello!chb!
<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读取属性文件
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());
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 " ));
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 " ));