赞
踩
1、背景
使用@Value
注解从配置文件中读取值时出现"Could not resolve placeholder"很有可能是使用了多个PropertyPlaceholderConfigurer或者多个context:property-placeholder的原因。
2、原因
因为spring通过反射机制加载外部资源 <context:property-placeholder location="classpath:XXX.properties"/>
在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceholderConfigurer替代PropertyPlaceholderConfigurer了),所以根据加载的顺序,配置的第二个property-placeholder就被没有被spring加载,所以在使用@Value注入的时候解析不了
2、解决方案
在所有<context:property-placeholder location=“classpath:XXX.properties”/>中加入
ignore-unresolvable="true"可以解决
<context:property-placeholder location="classpath:XXX.properties" ignore-unresolvable="true"/>
参考地址:https://blog.csdn.net/linwei_1029/article/details/6873764?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-6873764-blog-89931435.pc_relevant_3mothn_strategy_recovery&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-6873764-blog-89931435.pc_relevant_3mothn_strategy_recovery&utm_relevant_index=2
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。