1. 程式人生 > >Spring @Value("$XXX")注入值失敗,錯誤資訊提示:Could not resolve placeholder佔位符不能被解析

Spring @Value("$XXX")注入值失敗,錯誤資訊提示:Could not resolve placeholder佔位符不能被解析

問題原因:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'wx.app.config.appid' in string value "${wx.app.config.appid}" 解決辦法: 知道了怎麼配置,就知道自己有沒有配置錯誤,如果配置沒有問題,那麼就繼續找問題,Could not resolve placeholder佔位符不能被解析,那就是沒有在.properties中找到要注入的值了,原來是在xml裡面配置了2個ignore-unresolvable,這就是定位問題所在了。

<!-- 配置資料庫相關引數properties的屬性:${url} --> <context:property-placeholder location="classpath*:/mybatis/db.properties"/> <!-- 引入property檔案 --> <context:property-placeholder ignore-unresolvable="true" location="classpath:config/config-wxapp.properties" ignore-resource-not-found="true"/> 一個是引入配置檔案.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" />