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