PropertyPlaceholderConfigurer
注意: Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或<context:property-placeholder/>),其余的会被Spring忽略掉。
Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean
就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceholderConfigurer替代PropertyPlaceholderConfigurer了)。
一:配置单个Properties文件
<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对象了。
<context:property-placeholder location="classpath:jdbc.properties"/>
四:参考用例
1.
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.
applicationContext.xml
<context:property-placeholder location="classpath:config/*config/*.properties"/>
3.
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.
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>
纯属个人总结和网络搜索的资料