Spring多个Xml配置文件中PropertySourcesPlaceholderConfigurer的加载问题?

撸码的iJIAJIA 发布于 2015/11/09 17:01
阅读 3K+
收藏 0

在Spring框架写Junit测试类时,引用了两个Spring配置文件:spring-mail.xml和spring-mybatis.xml,两个配置文件中分别引用了Properties:QQMail.properties和estore.properties,在debug Junit时发生了占位符无法识别的错误。
spring-mail相关配置信息如下:

<!-- 加载配置文件 --> <bean id="qqMailPropConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> <property name="order" value="1"/> <property name="location" value="classpath:properties/QQMail.properties" /> </bean> <bean name="QQMailAuthenticator" class="com.rayootech.mail.ServerMailAuthenticator" > <property name="username" value="${Mail.username}"/> <property name="password" value="${Mail.password}"/> </bean>

spring-mybatis相关配置信息如下:

<!-- 加载配置文件 --> <bean id="estorePropConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> <property name="order" value="1"/> <property name="location" value="classpath:properties/estore.properties" /> </bean> <!-- 创建数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${estore.driver}"/> <property name="jdbcUrl" value="${estore.url}"/> <property name="user" value="${estore.username}"/> <property name="password" value="${estore.password}"/> </bean>

测试日志如下:

2015-11-9 14:10:19 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [config/spring-mybatis.xml] 2015-11-9 14:10:19 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [config/spring-mail.xml] 2015-11-9 14:10:19 org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
信息: Overriding bean definition for bean 'estorePropConfigurer': replacing [Generic bean: class [org.springframework.context.support.PropertySourcesPlaceholderConfigurer]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [config/spring-mybatis.xml]] with [Generic bean: class [org.springframework.context.support.PropertySourcesPlaceholderConfigurer]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [config/spring-mail.xml]] 2015-11-9 14:10:19 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@a0b1cd0: startup date [Mon Nov 09 14:10:19 CST 2015]; root of context hierarchy 2015-11-9 14:10:20 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
信息: Loading properties file from class path resource [properties/estore.properties] 2015-11-9 14:10:20 org.springframework.test.context.TestContextManager prepareTestInstance
....
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'QQMailAuthenticator' defined in class path resource [config/spring-mail.xml]: Could not resolve placeholder 'Mail.username' in string value "${Mail.username}" ....

日志上在加载estore.properties配置文件后(所属于spring-mybatis.xml),就开始注入spring-mail.xml的bean类,此时QQMail.properties文件(所属于spring-mail.xml)还未加载,理所当然找不到${Mail.username}占位符。
想知道如何控制properties的加载?

加载中
0
maradona
maradona

经百度  spring多个ropertySourcesPlaceholderConfigurer

打开第一个链接 http://my.oschina.net/ikkyuuC/blog/91247

试试...

撸码的iJIAJIA
撸码的iJIAJIA
谢谢,我在问这个问题之前也百度了很久,但问题描述和关键字没你这样准确,现在问题已经解决了,谢谢了
0
中场三分
中场三分

建议使用如下方式:

<context:property-placeholder ignore-resource-not-found="true" ignore-unresolvable="true"  location="classpath:example.properties,classpath*:override-example.properties,classpath*:*-more.properties"/>
如果在多个Spring配置文件中声明<context:property-placeholder/>,需要将配置属性:
ignore-unresolvable="true"

当然,最好的方式就是,配置loacation一次性引入多个。

撸码的iJIAJIA
撸码的iJIAJIA
嗯,你的方法和楼上是一样的,问题已经解决,谢谢了。对于你的建议,我那时是觉得将配置文件置于同一个locations里面的话感觉不便于维护,所以就没采用这个方法
返回顶部
顶部