spring:Could not resolve placeholder ‘XXX‘ in value “${XXX}“

本文探讨了在Spring框架中使用@Value注解时出现Could not resolve placeholder错误的原因,并提供了解决方案,即通过设置ignore-unresolvable属性为true来避免此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.youkuaiyun.com/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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十一*

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值