Spring中报"Could not resolve placeholder"的解决方案

除去properites文件路径错误、拼写错误外,出现”Could not resolve placeholder”很有可能是使用了多个PropertyPlaceholderConfigurer或者多个<context:property-placeholder>(<context:property-placeholder>底层也是PropertyPlaceholderConfigurer)的原因。


因为spring扫描注册PropertyPlaceholderConfigurer的bean被设置为单例模式,spring只会扫描一个PropertyPlaceholderConfigurer或者<context:property-placeholder>配置,其它多余将被忽略,所以才会出现替代属性值的占位符无法解析。

解决方案:

方案一:

坚持使用多个PropertyPlaceholderConfigurer或者多个<context:property-placeholder>,那么需要添加ignoreUnresolvablePlaceholders属性或者ignore-unresolvable属性,设置其值为true

如下代码所示:

<bean id="xxx" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    <property name="location" value="xxx.properties" />  
    <property name="ignoreUnresolvablePlaceholders" value="true" />   
</bean>  

<bean id="yyy" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    <property name="location" value="yyy.properties" />  
    <property name="ignoreUnresolvablePlaceholders" value="true" />   
</bean> 

或者:

<context:property-placeholder location="xxx.properties" ignore-unresolvable="true"/>  
<context:property-placeholder location="yyy.properties" ignore-unresolvable="true"/>  

参照:http://blog.csdn.net/linwei_1029/article/details/6873764

方案二:

将多个PropertyPlaceholderConfigurer或者多个<context:property-placeholder>合并到一个里面,统一加载所有的资源文件

如下代码所示:

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:xxx.properties</value>
            <value>classpath:jyyy.properties</value>
        </list>
    </property>
</bean> 

或者:

<context:property-placeholder location="classpath:xxx.properties,classpath:yyy.properties"/>

加载的日志可以发现,创建的propertyPlaceholderConfigurer这个bean是一个单例bean
这里写图片描述

参照:http://stamen.iteye.com/blog/1926166

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]和\[2\]中的错误信息显示,出现了"Could not resolve placeholder"的错误,这意味着在配置文件中找不到对应的占位符。具体来说,错误信息中提到了'spring.datasource.druid.initialSize'和'show.tips.text'这两个占位符无法解析。引用\[3\]中给出了一些解决方法。首先,确认配置文件中是否存在这些字段,并且字段名是否正确。其次,检查配置文件是否正确引用了相应的配置文件。在Spring Boot中,我们可以使用application.yml文件来指定使用哪个配置文件。例如,可以在application.yml中使用'spring.profiles.active'来指定使用名为'druid'的配置文件。如果以上步骤都正确无误,但仍然出现该错误,可能是由于其他原因导致的。可以进一步检查项目的依赖和版本是否正确,以及相关的配置是否正确。 #### 引用[.reference_title] - *1* *3* [springboot启动报错Could not resolve placeholder xxx](https://blog.csdn.net/a_helloword/article/details/118334203)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [【解决方法汇总】SpringBoot项目报错 Could not resolve placeholder ‘‘ in value “${}“](https://blog.csdn.net/yu1812531/article/details/123466616)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值