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"可以解決文章來源:http://www.zghlxwxcb.cn/news/detail-680855.html
<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文章來源地址http://www.zghlxwxcb.cn/news/detail-680855.html
到了這里,關于spring:Could not resolve placeholder ‘XXX‘ in value “${XXX}“的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!