首页 > Spring如何处理多个Xml配置文件中PropertySourcesPlaceholderConfigurer的加载顺序的?

Spring如何处理多个Xml配置文件中PropertySourcesPlaceholderConfigurer的加载顺序的?

在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}占位符。
想请问下如何解决上述问题。


为每个PropertySourcesPlaceholderConfigurer添加一个

<property name="ignoreUnresolvablePlaceholders" value="true"/>

属性就解决了

【热门文章】
【热门文章】