1. 程式人生 > >Java中讀取配置檔案的方式

Java中讀取配置檔案的方式



一、使用org.apache.commons.configuration

需要使用的jar包:commons-collections-3.2.1.jar、commons-configuration-1.10.jar、commons-lang-2.6.jar和commons-logging-1.2.jar。可以讀取的配置檔案:xml和properties。

        1、讀取.xml檔案

Configuration config = new XMLConfiguration("com/styspace/config.xml");  
String name = config.getString("Account.name"); 

        需要注意的是config.getString("Account.name")中的引數是Account.name,這個引數是XPath格式的,而且不能包含xml中的根元素。xml檔案格式形如:

<Accounts>   
    <Account type="by0003">    
        <code>100001</code>   
        <pass>123</pass>   
        <name>李四</name>    
        <money>1000000.00</money>    
    </Account>    
</Accounts>

       2、讀取.properties檔案

Configuration config = new PropertiesConfiguration("com/styspace/config.properties");  
String name = config.getString("name");  

        properties檔案格式形如:

interactive=true  
color=red  
speed=50  
name=Default User

二、使用Java.util.Properties讀取

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ipConfig.properties");
Properties p = new Properties();
p.load(inputStream);
String ip = p.getProperty("ip");

        需要注意的是getClassLoader().getResourceAsStream()的引數是專案根目錄下的路徑,即使config.properties在相同的目錄下,也不能寫成getClassLoader().getResourceAsStream("config.properties"),這樣程式會報錯,得到的InputStream是null值。

ClassLoader()和URLClassLoader()區別:ClassLoader()只能查詢src目錄下的檔案,而URLClassLoader()則能查詢任意目錄下的檔案。

三、spring讀取配置檔案

        1、XmlBeanFactory:BeanFactory實現,提供基本的IoC容器功能,可以從classpath或檔案系統等獲取資源;

            1>用法1

File file = new File(“ApplicationContext.xml”); 
Resource resource = new FileSystemResource(file); 
BeanFactory beanFactory = new XmlBeanFactory(resource);

            2>用法2

Resource resource = new ClassPathResource(“ApplicationContext.xml”); 
BeanFactory beanFactory = new XmlBeanFactory(resource);

        2、ClassPathXmlApplicationContext:ApplicationContext實現,從classpath獲取配置檔案;

BeanFactory beanFactory = new ClassPathXmlApplicationContext(“ApplicationContext.xml”);

        3、FileSystemXmlApplicationContext:ApplicationContext實現,從檔案系統獲取配置檔案。

BeanFactory beanFactory = new FileSystemXmlApplicationContext(“ApplicationContext.xml”);

四、利用spring中org.springframework.beans.factory.support.PropertiesBeanDefinitionReader讀取properties 檔案

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");

五、在Web應用中讀取配置檔案

        1、使用XmlWebApplicationContext:

XmlWebApplicationContext context = new XmlWebApplicationContext(); 
//預設的路徑/WEB-INF/applicationContext.xml
//applicationContext.xml檔名稱 可以任意起
//重新設定路徑
//context.setConfigLocations(new String[] {"/WEB-INF/classes/applicationContext.xml"}); 
//設定ServletContext上下文為web應用程式的上下文
context.setServletContext(getServletContext());
//重新整理
context.refresh();
//根據id名稱獲取
HelloDao helloDao = context.getBean("helloDaoImpl", HelloDaoImpl.class);
//執行helloDao物件的方法
helloDao.sayHello();

        2、使用WebApplicationContextUtils工具類:

//直接採用getWebApplicationContext(getServletContext()) 獲取context物件
WebApplicationContext  context= WebApplicationContextUtils.getWebApplicationContext(getServletContext());
//或者context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
HelloDao helloDao = context.getBean("helloDaoImpl", HelloDaoImpl.class);
helloDao.sayHello()

注意:

        當採用getWebApplicationContext(getServletContext())獲取context物件的時候,輸出的context物件為null,所以在使用context.getBean("helloDaoImpl", HelloDaoImpl.class);會出現空指標的異常。而當採用getRequiredWebApplicationContext(getServletContext());獲取context物件時會出現如下異常:java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered。