开发者社区> 问答> 正文

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

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


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


测试日志如下:

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的加载?

展开
收起
a123456678 2016-03-18 15:17:53 4571 0
1 条回答
写回答
取消 提交回答
  • 建议使用如下方式:


    如果在多个Spring配置文件中声明,需要将配置属性:
    ignore-unresolvable="true"

    2019-07-17 19:06:48
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多