Spring集成多个资源文件加载问题

Spring集成多个资源文件加载问题

异常代码

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'redis.port' in value "${redis.port}"


 

配置文件结构

 


Spring-redis.xml 文件

<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:redis="http://www.springframework.org/schema/redis"
	xsi:schemaLocation=""
	default-autowire="byName">
	<!-- 数据源 -->
	<context:property-placeholder location="classpath*:properties/redis.properties" /> 
		
	<!-- redis服务器中心 -->
	<bean id="redisConnectionFactory"
	class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<property name="port" value="${redis.port}" />
		<property name="hostName" value="${redis.host}" />
		<property name="password" value="${redis.password}" />
	</bean>
</beans>


 

Spring-jdbc.xml 配置文件

<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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="">
	<!-- 数据源 -->
	<context:property-placeholder location="classpath*:properties/jdbc.properties" /> 
	<!--数据库连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClass}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
       </bean>
</beans>


 

问题原因

Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceholderConfigurer替代PropertyPlaceholderConfigurer了)。
而<context:property-placeholder/>这个基于命名空间的配置,其实内部就是创建一个PropertyPlaceholderConfigurer Bean而已。换句话说,即Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或<context:property-placeholder/>),其余的会被Spring忽略掉(其实Spring如果提供一个警告就好了)。

 

修改配置文件,问题解决

<context:property-placeholder location="classpath:properties/jdbc.properties" ignore-unresolvable="true"/> 
<context:property-placeholder location="classpath:properties/redis.properties" ignore-unresolvable="true"/>


 

或者

    
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
            <property name="locations">  
                <list>                    <value>classpath*:properties/jdbc.properties</value>  
                </list>  
            </property>  
            <property name="ignoreUnresolvablePlaceholders" value="true" />   
        </bean>     
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
            <property name="locations">  
                <list>                <value>classpath*:properties/redis.properties</value>  
                </list>  
            </property>  
            <property name="ignoreUnresolvablePlaceholders" value="true" />   
        </bean>  


 

 

问题补充:

1、PropertyPlaceholderBeanDefinitionParser的父类中shouldGenerateId返回true,即默认会为每一个bean生成一个唯一的名字; 如果使用了两个<context:property-placeholder则注册了两个PropertySourcesPlaceholderConfigurer Bean;所以不是覆盖(而且bean如果同名是后边的bean定义覆盖前边的);

2、PropertySourcesPlaceholderConfigurer本质是一个BeanFactoryPostProcessor,spring实施时如果发现这个bean实现了Ordered,则按照顺序执行;默认无序;

3、此时如果给<context:property-placeholder加order属性,则会反应出顺序,值越小优先级越高即越早执行;
比如
   <context:property-placeholder order="2" location="classpath*:conf/conf_a.properties"/> 
   <context:property-placeholder order="1" location="classpath*:conf/conf_b.properties"/>

此时会先扫描order='1' 的,如果没有扫描order='2'的

4、默认情况下ignore-unresolvable;即如果没找到的情况是否抛出异常。默认false:即抛出异常;
<context:property-placeholder location="classpath*:conf/conf_a.properties" ignore-unresolvable="false"/>

 

参考地址:http://www.iteye.com/topic/1131688

http://xingguangsixian.iteye.com/blog/2088618

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值