PropertyPlaceholderConfigurer
注意: Spring容器僅容許最多定義一個PropertyPlaceholderConfigurer(或<context:property-placeholder/>),其他的會被Spring忽略掉。
Spring容器採用反射掃描的發現機制,在探測到Spring容器中有一個org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean
就會中止對剩餘PropertyPlaceholderConfigurer的掃描(Spring 3.1已經使用PropertySourcesPlaceholderConfigurer替代PropertyPlaceholderConfigurer了)。java
一:配置單個Properties文件spring
<bean id="deployProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:config/sysconfig/sysconfig.properties</value> </property> </bean>
<bean id="deployProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:config/sysconfig/sysconfig.properties"/> </bean>
二:配置多個Properties文件網絡
<bean id="deployProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:config/dbconfig/database.properties</value> <value>classpath:config/sysconfig/sysconfig.properties</value> <value>file:/opt/demo/config/demo-mq.properties</value> </list> </property> </bean>
三:簡化操做
爲簡化PropertyPlaceholderConfigurer的使用,Spring提供了<context:property-placeholder/>元素。下面給出了配置示例,啓用它後,開發者便不用配置PropertyPlaceholderConfigurer對象了。app
<context:property-placeholder location="classpath:jdbc.properties"/>
四:參考用例ide
1.spa
1. applicationContext.xml <bean id="deployProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations" > <list> <value>classpath:config/dbconfig/database.properties</value> <value>classpath:config/sysconfig/sysconfig.properties</value> </list> </property> </bean> // 其中classpath是引用src目錄下的文件寫法 <import resource="classpath:config/spring/spring-data.xml" /> spring-data.xml <context:property-placeholder properties-ref="deployProperties" />
2.3d
applicationContext.xml <context:property-placeholder location="classpath:config/*config/*.properties"/>
3.code
applicationContext.xml <bean id="deployProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:config/dbconfig/database.properties</value> <value>classpath:config/sysconfig/sysconfig.properties</value> <value>file:/opt/demo/config/demo-mq.properties</value> </list> </property> </bean>
4.xml
applicationContext.xml <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="order" value="1" /> <property name="ignoreUnresolvablePlaceholders" value="true" /> <property name="location" value="classpath:config/dbconfig/database.properties"/> </bean> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="order" value="2" /> <property name="ignoreUnresolvablePlaceholders" value="true" /> <property name="location" value="classpath:config/sysconfig/sysconfig.properties"/> </bean>
其中order屬性表明其加載順序,而ignoreUnresolvablePlaceholders爲是否忽略不可解析的 Placeholder,如配置了多個PropertyPlaceholderConfigurer,則需設置爲true對象
5.
applicationContext.xml <bean id="dbResources" class="java.util.ArrayList"> <constructor-arg> <list> <value>classpath:config/dbconfig/database.properties</value> <value>classpath:config/sysconfig/sysconfig.properties</value> </list> </constructor-arg> </bean> <bean id="deployProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" ref="dbResources"> </property> </bean>
6.
applicationContext.xml <bean id="configproperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations" > <list> <value>classpath:config/dbconfig/database.properties</value> <value>classpath:config/sysconfig/sysconfig.properties</value> </list> </property> </bean> <bean id="deployProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="properties" ref="configproperties"> </property> </bean>
7.context 標籤和PropertyPlaceholderConfigurer 混合在一塊兒要加ignoreUnresolvablePlaceholders
<context:property-placeholder location="classpath:config/*config/database.properties"/> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="order" value="2" /> <property name="ignoreUnresolvablePlaceholders" value="true" /> <property name="location" value="classpath:config/sysconfig/sysconfig.properties"/> </bean>
純屬我的總結和網絡搜索的資料