1. 程式人生 > >Spring 如何讀取properties檔案內容

Spring 如何讀取properties檔案內容

http://hi.baidu.com/alizv/blog/item/d8cb2af4094662dbf3d38539.html

在現實工作中,我們常常需要儲存一些系統配置資訊,大家一般都會選擇配置檔案來完成,本文根據我工作中用到的讀取properties配置檔案的方法小小總結一下,主要敘述的是spring讀取配置檔案的方法。
   用spring讀取配置檔案,最典型的就是關於資料庫的連線,下面就是一個例子:
   檔案jdbc.properties:
-------------------------------------------------------------------------------------
       driverClassName com.mysql.jdbc.Driver
       url jdbc:mysql://localhost:3306/test
       username root
       password 1234
------------------------------------------------------------------------------------
引入spring的相關jar包,在applicationContext.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="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
           <value>src/jdbc.properties</value>
</property>
</bean>

<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
    <value>${driverClassName}</value>
</property>
<property name="url">
    <value>${url}</value>
</property>
<property name="username">
   <value>${username}</value>
</property>
<property name="password">
    <value>${password}</value>
</property>
</bean>

<bean id="dao" class="com.zh.model.DataDAO">
   <property name="datasource">
     <ref local="datasource"/>
   </property>
</bean>

</beans>
-----------------------------------------------------------------------------------------
DataDAO.java

package com.zh.model;

import javax.sql.DataSource;

public class DataDAO {
private DataSource datasource;

public DataSource getDatasource() {
return datasource;
}

public void setDatasource(DataSource datasource) {
this.datasource = datasource;
}

}
------------------------------------------------------------------------------------
測試連線是否成功,test.java
package com.zh.logic;

import java.sql.Connection;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.zh.model.DataDAO;

public class test {
public static void main(String [] args){
   try{
   String[] path = {"src/applicationContext.xml"};
   ApplicationContext ctx = new FileSystemXmlApplicationContext(path);

   DataDAO dao = (DataDAO)ctx.getBean("dao");
   Connection con = dao.getDatasource().getConnection();
   System.out.println(con.isClosed());
   //System.out.print(dao.getName());
   }catch(Exception ex){
   ex.printStackTrace();
     }
   }
}
-------------------------------------------------------------------------------------
2.用java.util.Properties這個類來讀取
比如,我們構造一個ipConfig.properties來儲存伺服器ip地址和埠,如:
ip=192.168.0.1
port=8080
--------------------------------------------------------------------------------------
則,我們可以用如下程式來獲得伺服器配置資訊:
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ipConfig.properties");
   Properties p = new Properties();
   try{
       p.load(inputStream);
   } catch (IOException e1){
    e1.printStackTrace();
   }
System.out.println("ip:"+p.getProperty("ip")+",port:"+p.getProperty("port"));
--------------------------------------------------------------------------------------
上面介紹了讀取properties的內容,現實中我們還有可能要修改檔案的內容,下面就看下怎麼修改properties的內容,檔案還是上面那個:
package com.zh.logic;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties;

public class TestRead {
    
public void read(){
   try {
    InputStream in = this.getClass().getClassLoader().getResourceAsStream("config/host.properties");
    Properties p = new Properties();
    p.load(in);
    //p.list(System.out);
      
    System.out.println(p.getProperty("ip")+","+p.getProperty("username")+","+p.getProperty("pwd"));
   } catch (Exception e) {
    e.printStackTrace();
   }
}

public void update(String path){
    try{
     Properties p = new Properties();
     FileInputStream in = new FileInputStream(path);
     p.load(in);
     FileOutputStream out = new FileOutputStream(path);
    
     p.setProperty("ip","1234567");
     p.store(out,"ip update");
     //p.save(out,"ip updated");
    }catch(Exception ex){
     ex.printStackTrace();
    }
}
public static void main(String[] args){
   TestRead td = new TestRead();
   td.read();
   td.update("config/host.properties");
   td.read();
}
}
可以看見修改之前的和修改之後的內容有改變;在上面有點要注意的:
        FileInputStream in = new FileInputStream(path);
        p.load(in);
        FileOutputStream out = new FileOutputStream(path);
就是p.load(in);要寫在FileOutputStream out = new FileOutputStream(path);之前,不然的話,修改後的檔案內容就成了ip=1234567,而port=8080這句被覆蓋了;什麼願意大家自己想想吧;

上面介紹了兩中讀取properties檔案的方法,希望對大家有幫助........