spring入門(二)【載入properties檔案】,springproperties
在開發過程當中需要用到配置資訊,這些資訊不能進行寫入程式碼,這時設定檔是一個比較好的方式,java提供了properties格式的檔案,以索引值對的方式儲存資訊,在讀取的時候通過鍵獲得鍵對應的值,spring提供了讀取properties檔案的支援,下面看具體的配置,
一、<context:property-placeholder location=""/>標籤
在項目中經常用到資料庫連接,串連資料庫需要一些參數:driver、url、username、password,這些資訊通常儲存在properties檔案中,如,在我的項目src目錄下有db.properties檔案,檔案內容,
db.driver=com.mysql.jdbc.Driverdb.url=jdbc:mysql://192.168.8.32:3306/testdb.username=rootdb.passwd=root
現在要使用spring載入此檔案,我們看spring如何配置,在spring的設定檔中有<context:property-placeholder location="classpath:db.properties"/>標籤,此標籤支援單個檔案的載入,不過可以使用萬用字元,如,classpath:db*.properties,這裡指定了src下的db.properties檔案,那麼如何使用載入的檔案呢,可以使用${db.driver},下面看一個在資料庫連接池中使用的例子,
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${db.driver}"></property> <property name="url" value="${db.url}"></property> <property name="username" value="${db.username}"></property> <property name="password" value="${db.passwd}"></property> </bean>
在spring的設定檔中可以使用上面的方式獲得db.properties檔案中的值,
那麼,現在我要在java類中使用db.properties檔案中的值呢,可以使用@Value("${db.driver}"),使用這樣一個註解別可以給類中的屬性賦值,如下
package com.cn.study.day2;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;@Componentpublic class Configuration { @Value("${db.driver}") private String dbDriver; @Value("${db.url}") private String dbUrl; @Value("${db.username}") private String dbUsername; @Value("${db.passwd}") private String dbPasswd; public String getDbDriver() { return dbDriver; } public String getDbUrl() { return dbUrl; } public String getDbUsername() { return dbUsername; } public String getDbPasswd() { return dbPasswd; } }
這裡把註解用在了屬性上面,也可以用在setXXX方法上,下面是測試代碼,
//cpac是ClassPathXmlApplicationContext的一個執行個體對象 Configuration con=(Configuration)cpac.getBean("configuration"); System.out.println(con.getDbDriver()); System.out.println(con.getDbUrl()); System.out.println(con.getDbUsername()); System.out.println(con.getDbPasswd());
列印結果如下,
com.mysql.jdbc.Driverjdbc:mysql://192.168.8.32:3306/testrootroot
以上是在java類中使用properties檔案的例子。
二、PropertyPlaceholderConfigurer類
配置PropertyPlaceholderConfigurer類的bean
<bean id="appProperty" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <array> <value>classpath:quartz.properties</value> <value>classpath:db.properties</value>
</array> </property></bean>
使用這種方式可以指定多個檔案,只要配置多個<value>標籤即可,這種方式和<context:property-placeholder location="classpath:db.properties"/>方式是一樣的。使用方式和第一種是一致的。
<context:property-placeholder location="classpath:db.properties"/>和配置PropertyPlaceholderConfigurer類的方式在spring的設定檔中只有一個起作用,即,假如都配置了,只有最先配置的才會被設定檔載入到,因為spring容器採用反射掃描機制,如果已經有一個PropertyPlaceholderConfigurer類的執行個體,那麼就不會再建立了,因此使用其中一種方式即可
三、PropertiesFactoryBean類
可以配置此類,如下,
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>classpath:quartz.properties</value>
<value>classpath:db.properties</value>
</list> </property> </bean>
要使用這種方式載入的properties檔案,需要使用#{beanId['鍵']},如上面的,#{configProperties['db.url']}使用這種方式便可以得到db.url的值,
如,資料庫連接中,
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>${db.driver}</value> </property> <property name="url"> <value>#{configProperties['db.url']}</value> </property> <property name="username"> <value>#{configProperties['db.username']}</value> </property> <property name="password"> <value>123456</value> </property> </bean>
上面的方式混合使用了${db.driver}和#{configProperties['db.url']}的方式,這兩種方式都是可以的,
在java代碼中的使用方式也是使用註解@Value,但是值是#{configProperties['db.url']},Z@Value("#{configProperties['db.url']}"),如下,
@Value("#{configProperties['db.username']}") private String dbUsername;
此註解用在setXXX方法上也是可以的。
上面是三種spring載入properties檔案的方式,有人會問,為什麼在配置時使用了locations、location這樣的屬性呢,下面是一個有關properties的UML類圖,
這個類圖說明了各個類之間的繼承關係,其中locations、location都是在共同的父類PropertiesLoaderSupport中定義的,這是一個抽象類別。
好了,關於spring載入properties的內容就說那麼多;其中源碼還得繼續研究呢。
有不正之處歡迎指出,謝謝!