继续上一篇文章的内容,我在不同的模块键都需要将图片上传至图片服务器,但是报了这个错误:
Spring @Value(“$XXX”)注入值失败,错误信息提示:Could not resolve placeholder占位符不能被解析
原因如下:知道了怎么配置,就知道自己有没有配置错误,如果配置没有问题,那么就继续找问题,Could not resolve placeholder占位符不能被解析,那就是没有在.properties中找到要注入的值了,原来我的很多工程里都有config/application.properties这个文件。
但是:
一个是引入配置文件.properties,一个是另一个项目的引入db配置文件.properties,其实配置多个并不是报错的主要问题所在,是因为spring的加载机制,原来是Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceholderConfigurer替代PropertyPlaceholderConfigurer了),所以根据加载的顺序,配置的第二个property-placeholder就被没有被spring加载,我想引入的config-wxapp.properties就没有被引入,所以在使用@Value注入的时候占位符就解析不了…解决方法就是把2个property-placeholder注解配置合并在一起就好了。
解决方法
<!-- 引入property文件 -->
<context:property-placeholder ignore-unresolvable="true" location="classpath*:config/config-*.properties,classpath*:mybatis/db.properties" ignore-resource-not-found="true"/>
或者这样
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:resource/*.properties" />
参考:https://blog.csdn.net/zezezuiaiya/article/details/79172865