轉自:http://blog.csdn.net/stypace/article/details/38414871html
1、使用org.apache.commons.configurationjava
須要使用的是jar包:commons-collections-3.2.1.jar、commons-configuration-1.10.jar、commons-lang-2.6.jar和commons-logging-1.2.jar。web
能夠讀取的配置文件:xml和propertiesspring
一、讀取xml文件apache
- <span style="font-family:Microsoft YaHei;font-size:12px;">package com.styspace;
-
- import org.apache.commons.configuration.Configuration;
- import org.apache.commons.configuration.ConfigurationException;
- import org.apache.commons.configuration.XMLConfiguration;
- public class xmlLoaderTest {
-
- public static void main(String[] args) throws ConfigurationException{
- Configuration config = new XMLConfiguration("com/styspace/config.xml");
- String name = config.getString("Account.name");
- System.out.println("name:" + name);
- }
- }
- </span>
須要注意的是config.getString(「Account.name」)中的參數是Account.name,這個參數是XPath格式的,並且不能包含xml中的根元素。app
使用到的config.xml內容以下:工具
- <span style="font-family:Microsoft YaHei;font-size:12px;"><?xml version="1.0" encoding="gbk"?>
- <Accounts>
- <Account type="by0003">
- <code>100001</code>
- <pass>123</pass>
- <name>李四</name>
- <money>1000000.00</money>
- </Account>
- </Accounts></span>
二、讀取properties文件ui
- <span style="font-family:Microsoft YaHei;font-size:12px;">package com.styspace;
-
- import org.apache.commons.configuration.Configuration;
- import org.apache.commons.configuration.ConfigurationException;
- import org.apache.commons.configuration.PropertiesConfiguration;
-
- public class peropertiesLoaderTest {
-
- public static void main(String[] args) throws ConfigurationException{
- Configuration config = new PropertiesConfiguration("com/styspace/config.properties");
- String name = config.getString("name");
- System.out.println("name:" + name);
- }
- }
- </span>
使用到的config.properties文件內容以下:this
- <span style="font-family:Microsoft YaHei;font-size:12px;">threads.max=50threas.min=2
- timout=15.52
- interactive=true
- color=red
- speed=50
- name=Default User</span>
2、使用java.util.Properties讀取spa
- <span style="font-family:Microsoft YaHei;font-size:12px;">package com.styspace;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
-
- public class PropertiesTest {
- public static void main(String[] args){
- PropertiesTest pt = new PropertiesTest();
- try {
- pt.getProperties();
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- }
-
- private void getProperties() throws IOException {
- InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("com/styspace/config.properties");
- System.out.println("begin!!!");
- Properties properties = new Properties();
- try{
- properties.load(inputStream);
- }catch (IOException ioE){
- ioE.printStackTrace();
- }finally{
- inputStream.close();
- }
- System.out.println("name:"+properties.getProperty("name"));
- }
- }
- </span>
須要注意的是hetClassLoader().getResourceAsStream()的參數是項目根目錄下的路徑,儘管config.properties是該該類文件在相同的目錄下,可是不能寫成getClassLoader().getResourceAsStream("config.properties"),這樣程序會報錯,獲得的InputStream是null值。
ClassLoader()和URLClassLoader()區別:ClassLoader()只能查找src目錄下的文件,而URLClassLoader()則能查找任意目錄下的文件。
3、spring中配置文件的讀取
一、ClassPathXmlApplicationContext:從類路徑中加載。
二、FileSystemXmlApplicationContext:從文件系統加載。
三、XmlWebApplicationContext:從web系統中加載。
一、使用bean工廠獲取bean
- <span style="font-family:Microsoft YaHei;font-size:12px;"> BeanFactory factory = null;
-
- ClassPathResource resource = new ClassPathResource("spring.xml");
- factory= new XmlBeanFactory(resource);
-
- FileSystemResource fileSystemResource = new FileSystemResource("D:\\Ncode\\mcode\\sday02\\src\\spring.xml");
- factory= new XmlBeanFactory(fileSystemResource);
-
-
-
-
-
-
- HelloService helloService = factory.getBean("helloServiceImpl", HelloServiceImpl.class);
- helloService.sayHello();</span>
二、使用上下文(Context)
上下文更加高級:提供文本信息解析工具,包括對國際化支持;提供載入文件資源的通用方法,如圖片;能夠向註冊爲監聽器的bean發送事件。
在不多的狀況下,使用BeanFactory。
- <span style="font-family:Microsoft YaHei;font-size:12px;">
- ApplicationContext context = new FileSystemXmlApplicationContext("file:D:\\Ncode\\mcode\\sday02\\src\\spring.xml");
-
- ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring.xml");
-
- HelloService helloService = context.getBean("helloServiceImpl", HelloServiceImpl.class);
- helloService.sayHello();</span>
三、在web應用中使用
3.一、使用XmlWebApplicationContext
- XmlWebApplicationContext context = new XmlWebApplicationContext();
- context.setServletContext(getServletContext());
- context.refresh();
- HelloDao helloDao = context.getBean("helloDaoImpl", HelloDaoImpl.class);
- helloDao.sayHello();
3.二、使用WebApplicationContextUtils工具類
- WebApplicationContext context=
- WebApplicationContextUtils.getWebApplicationContext(getServletContext());
- System.out.println(context);
- HelloDao helloDao = context.getBean("helloDaoImpl", HelloDaoImpl.class);
- helloDao.sayHello()
二者的區別是:
一、當採用getWebApplicationContext(getServletContext())獲取context對象的時候,輸出的context對象爲null 因此在使用
context.getBean("helloDaoImpl", HelloDaoImpl.class);會出現空指針的異常
二、當採用getRequiredWebApplicationContext(getServletContext());獲取context對象的時候 會出現以下bug
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered