1、spring中context:property-placeholder/元素
1.有些參數在某些階段中是常量
比如 :a、在開發階段我們連接數據庫時的連接url,username,password,driverClass等
b、分佈式應用中client端訪問server端所用的server地址,port,service等
c、配置文件的位置
2.而這些參數在不同階段之間又往往需要改變
比如:在項目開發階段和交付階段數據庫的連接信息往往是不同的,分佈式應用也是同樣的情況。
期望:能不能有一種解決方案可以方便我們在一個階段內不需要頻繁書寫一個參數的值,而在不同階段間又可以方便的切換參數配置信息
解決:spring3中提供了一種簡便的方式就是context:property-placeholder/元素
只需要在spring的配置文件裏添加一句:<context:property-placeholder location="classpath:jdbc.properties"/> 即可,這裏location值爲參數配置文件的位置,參數配置文件通常放在src目錄下,而參數配置文件的格式跟java通用的參數配置文件相同,即鍵值對的形式,例如:
#jdbc配置
test.jdbc.driverClassName=com.mysql.jdbc.Driver
test.jdbc.url=jdbc:mysql://localhost:3306/test
test.jdbc.username=root
test.jdbc.password=root
行內#號後面部分爲註釋
應用:
1.這樣一來就可以爲spring配置的bean的屬性設置值了,比如spring有一個jdbc數據源的類DriverManagerDataSource
在配置文件裏這麼定義bean:
<bean id="testDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${test.jdbc.driverClassName}"/>
<property name="url" value="${test.jdbc.url}"/>
<property name="username" value="${test.jdbc.username}"/>
<property name="password" value="${test.jdbc.password}"/>
</bean>
2.甚至可以將${ }這種形式的變量用在spring提供的註解當中,爲註解的屬性提供值
---------------------------------------------------------
外在化應用參數的配置
在開發企業應用期間,或者在將企業應用部署到生產環境時,應用依賴的很多參數信息往往需要調整,比如LDAP連接、RDBMS JDBC連接信息。對這類信息進行外在化管理顯得格外重要。PropertyPlaceholderConfigurer和PropertyOverrideConfigurer對象,它們正是擔負着外在化配置應用參數的重任。
<context:property-placeholder/>元素
PropertyPlaceholderConfigurer實現了BeanFactoryPostProcessor接口,它能夠對<bean/>中的屬性值進行外在化管理。開發者可以提供單獨的屬性文件來管理相關屬性。比如,存在如下屬性文件,摘自userinfo.properties。
db.username=scott
db.password=tiger
如下內容摘自propertyplaceholderconfigurer.xml。正常情況下,在userInfo的定義中不會出現${db.username}、${db.password}等類似信息,這裏採用PropertyPlaceholderConfigurer管理username和password屬性的取值。DI容器實例化userInfo前,PropertyPlaceholderConfigurer會修改userInfo的元數據信息(<bean/>定義),它會用userinfo.properties中db.username對應的scott值替換${db.username}、db.password對應的tiger值替換${db.password}。最終,DI容器在實例化userInfo時,UserInfo便會得到新的屬性值,而不是${db.username}、${db.password}等類似信息。
- <bean id="propertyPlaceholderConfigurer"
- class="org.springframework.beans.factory.config.
- PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>userinfo.properties</value>
- </list>
- </property>
- </bean>
- <bean name="userInfo" class="test.UserInfo">
- <property name="username" value="${db.username}"/>
- <property name="password" value="${db.password}"/>
- </bean>
通過運行並分析PropertyPlaceholderConfigurerDemo示例應用,開發者能夠深入理解PropertyPlaceholderConfigurer。爲簡化PropertyPlaceholderConfigurer的使用,Spring提供了<context:property-placeholder/>元素。下面給出了配置示例,啓用它後,開發者便不用配置PropertyPlaceholderConfigurer對象了。
- <context:property-placeholder location="userinfo.properties"/>
PropertyPlaceholderConfigurer內置的功能非常豐富,如果它未找到${xxx}中定義的xxx鍵,它還會去JVM系統屬性(System.getProperty())和環境變量(System.getenv())中尋找。通過啓用systemPropertiesMode和searchSystemEnvironment屬性,開發者能夠控制這一行爲。
****注意*****:如果配置發現報"Could not resolve placeholder" 錯,那麼 下面就是解決方案
除去properites文件路徑錯誤、拼寫錯誤外,出現"Could not resolve placeholder"很有可能是使用了多個PropertyPlaceholderConfigurer或者多個<context:property-placeholder>的原因。
比如我有一個dao.xml讀取dbConnect.properties,還有一個dfs.xml讀取dfsManager.properties,然後web.xml統一load這兩個xml文件
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- WEB-INF/config/spring/dao.xml,
- WEB-INF/config/spring/dfs.xml
- </param-value>
- </context-param>
如果這兩個xml文件中分別有
- <!-- dao.xml -->
- <context:property-placeholder location="WEB-INF/config/db/dbConnect.properties" />
- <!-- dfs.xml -->
- <context:property-placeholder location="WEB-INF/config/dfs/dfsManager.properties" />
那麼,一定會出"Could not resolve placeholder"。
一定要記住,不管是在一個Spring文件還是在多個Spring文件被統一load的情況下,直接寫
- <context:property-placeholder location="" />
- <context:property-placeholder location="" />
是不允許的。
解決方案:
(1) 在Spring 3.0中,可以寫:
- <context:property-placeholder location="xxx.properties" ignore-unresolvable="true"
- />
- <context:property-placeholder location="yyy.properties" ignore-unresolvable="true"
- />
注意兩個都要加上ignore-unresolvable="true",一個加另一個不加也是不行的
(2) 在Spring 2.5中,<context:property-placeholder>沒有ignore-unresolvable屬性,此時可以改用PropertyPlaceholderConfigurer。其實<context:property-placeholder location="xxx.properties" ignore-unresolvable="true" />與下面的配置是等價的
- <bean id="隨便" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="location" value="xxx.properties" />
- <property name="ignoreUnresolvablePlaceholders" value="true" />
- </bean>
正因爲如此,寫多個PropertyPlaceholderConfigurer不加ignoreUnresolvablePlaceholders屬性也是一樣會出"Could not resolve placeholder"。
雖然兩者是的等價的,但估計大家還是喜歡寫<context:property-placeholder>多一些,畢竟簡單一些嘛。所以如果是Spring 3.0,直接用解決方案(1)再簡單不過了;如果是Spring 2.5,需要費點力氣改寫成PropertyPlaceholderConfigurer