springmvc引入属性文件报错误:Could not resolve placeholder

1、问题出现步骤

在springmvc项目中引入了属性文件

<context:property-placeholder location="classpath*:freemarker.properties"></context:property-placeholder>

freemarker.properties内容:

out_put_path=/usr/local/setmeal_html

在service类型使用@value 引入,如下:

@Value("${out_put_path}")
private String outPutPath;

启动时,报如下错误:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'out_put_path' in value "${out_put_path}"
	at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:172)
	at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124)

2、分析原因:

在项目中存在两个属性文件:

freemarker.properties

jdbc.properties

而在前面使用property-placeholder引入了jdbc.properties文件:

<context:property-placeholder location="classpath*:jdbc.properties"></context:property-placeholder>

而freemarker.properties是在jdbc.properties属性文件后引入的,而spring的加载机制:Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceholderConfigurer替代PropertyPlaceholderConfigurer了),所以根据加载的顺序,配置的第二个property-placeholder就被没有被spring加载,所以在使用@Value注入的时候占位符就解析不了

 

3、解决方法:

把freemarker.properties中的数据加入到jdbc.properties

 

 

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值