今天部署服务时出现的问题,本次部署的服务包含多个spring工程,其中两个子项目都有properties的扫描配置。
日志:
Invalid bean definition with name 'xxxxx' defined in class path resource [xxxxxx.xml]: Could not resolve placeholder 'xxxxxxxx.host' in string value "${xxxxxxxx.host}"
at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:209)[114:org.springframework.beans:3.2.5.RELEASE]
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.processProperties(PropertySourcesPlaceholderConfigurer.java:174)[116:org.springframework.context:3.2.5.RELEASE]
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:151)[116:org.springframework.context:3.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694)[116:org.springframework.context:3.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:669)[116:org.springframework.context:3.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)[116:org.springframework.context:3.2.5.RELEASE]
at com.travelsky.jcf.osgi.spring.SpringApplicationContextCreator$2.run(SpringApplicationContextCreator.java:360)[117:com.travelsky.jcf.osgi-spring:1.3.5]
at com.travelsky.jcf.osgi.spring.util.OSGISpringHelper$1.run(OSGISpringHelper.java:57)[117:com.travelsky.jcf.osgi-spring:1.3.5]
at java.lang.Thread.run(Thread.java:745)[:1.8.0_73]
单从日志上看,一开始只想到了是不是配置文件key的命名错了啊之类的,并没有想太多。之后发现问题的原因是在 context:property-placeholder
配置上
错误的配置:
1.<context:property-placeholder location="file:./xxx/config/xxx.properties"/>
2.<context:property-placeholder location="file:./xxx/config/xxx2.properties" ignore-unresolvable="true" />
程序走到配置1,完成扫描是没有问题的,但走到配置二时便会报上面的错误,最后的问题是 少写了ignore-unresolvable=”true” 引起的。
具体的原因看到基本上说是因为占位符冲突的原因。导致冲突的原因可能是这样的:实例化了多个properties对象,按照初始化顺序,依次匹配${key},配置了ignore-unresolvable=”true” 的properties实例在发现匹配失败后便继续下一个,而未配置的则停止报错,导致真正的所需的properties实例无法配置。