spring异常:Could not resolve placeholder

本文探讨了在Spring MVC中遇到的BeanDefinitionStoreException异常,详细解析了问题出现的原因,即多个PropertyPlaceholderConfigurer配置导致的占位符无法解析的情况。文中提供了解决方案,包括调整ignoreUnresolvablePlaceholders属性的设置以及调整配置文件的加载顺序。

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

org.springframework.beans.factory.BeanDefinitionStoreException: 

Invalid bean definition with name '******' defined in null: Could not resolve placeholder 'displayName'

因为你配置了多个 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

这个配置用于 xml 中的占位符,如下:

<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />

解决方法:a.xml中配置第一个 PropertyPlaceholderConfigurer 时,将ignoreUnresolvablePlaceholders的值设为true

如下所示:

<bean id="myConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>

 

这个配置告诉spring,当某个placeholder无法找到时,先不要报错,并尝试用另一个PropertyPlaceholderConfigurer来设置placeholder的值。

假设 a.xml 配置了一个 PropertyPlaceholderConfigurer ,并且成功了。

 

后来,b.xml 也配置了一个 PropertyPlaceholderConfigurer ,这时候如果不做特别配置,b.xml 里配置的placeholder将无法使用,并报上面的错误。

如果还不行,将定义PropertyPlaceholderConfigurer 的bean提到applicationContext.xml的最前面

转载于:https://my.oschina.net/u/3676955/blog/1786307

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值