<context:property-placeholder>标签
<context:property-placeholder>标签提供了一种优雅的外在化参数配置的方式,不过该标签在Spring配置文件中只能存在一份!!!
众所周知,Spring容器是采用反射扫描的发现机制,通过标签的命名空间实例化实例,当Spring探测到容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描,即:在多个spring的xml配置文件中只能存在一个PropertyPlaceholderConfiurer实例!
1、示例:
<context:property-placeholder
location=""
file-encoding=""
ignore-resource-not-found=""
ignore-unresolvable=""
properties-ref=""
local-override=""
system-properties-mode=""
order=""
/>
- location:表示属性文件位置,多个之间通过如逗号/分号等分隔;
- file-encoding:文件编码;
- ignore-resource-not-found:如果属性文件找不到,是否忽略,默认false,即不忽略,找不到将抛出异常
- ignore-unresolvable:是否忽略解析不到的属性,如果不忽略,找不到将抛出异常
- properties-ref:本地java.util.Properties配置
- local-override:是否本地覆盖模式,即如果true,那么properties-ref的属性将覆盖location加载的属性
- system-properties-mode:系统属性模式,ENVIRONMENT(默认),NEVER,OVERRIDE
ENVIRONMENT:将使用Spring 3.1提供的PropertySourcesPlaceholderConfigurer,其他情况使用Spring 3.1之前的PropertyPlaceholderConfigurer
OVERRIDE: PropertyPlaceholderConfigurer使用,因为在spring 3.1之前版本是没有Enviroment的,所以OVERRIDE是spring 3.1之前版本的Environment
NEVER:只查找properties-ref、location;
- order:当配置多个<context:property-placeholder/>时的查找顺序(如果配置了多个property-placeholder标签,那么在每个properties文件中都要有对应的key、value对)
2、说明:
1)location中指定多个properties文件的顺序:
location中可以指定多个properties文件,也可以使用*通配符来指定多个文件。例如:location=“classpath:db.properties,classpath:default.properties”
值得注意的是如果后一个文件中有和前面某一个文件中属性名是相同的,最终取的值是后加载的值。
2)local-override属性:
当local-override属性为true时,properties-ref属性文件中属性值将覆盖location属性文件属性值(同名属性)
<bean id="refProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:default2.properties</value>
</list>
</property>
</bean>
3、多个spring配置文件中加载不同的properties:
在spring项目中,假设有多个spring的配置文件,如:application.xml、application-redis.xml、application-thrift.xml等,在不同的spring配置文件中又需要从不同的properties文件中加载值,该如何做呢?
上面说了,spring中采用反射加载方式,最终只会实例化一个PropertyPlaceholderConfigurer,如果我们在spring配置文件中配置了多个<context:property-placeholder>,每个property-placeholder指定了一个properties文件,并且不同的properties文件中key、value对不一样,那么就会导致部分key-value值找不到,报类似:Could not resolve placeholder 'redis.maxTotal' in string value "${redis.maxTotal}" 的错误。
正确的解决方法是,配置一个<context:property-placeholder>,指定多个properties文件(使用逗号分隔,或者通配符),然后使用import标签引入其他spring配置文件,例如:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:conf/*.properties" />
<import resource="activemq/spring-activemq.xml" />
<import resource="redis/spring-redis.xml" />
</beans>