@Value取不到值,直接輸出了${name}字符串

項目中常常會用到配置文件,定義成properties的形式比較常見,爲了方便使用通常在spring配置文件中作以下配置:web

<context:property-placeholder ignore-resource-not-found="true" location="classpath:xxxx.properties" file-encoding="utf-8"/>spring

這樣在程序代碼中直接用@Value("${name}"),就能直接取到properties文件中定義的變量值。spring-mvc

可是在項目中發現一個狀況,在Controller中取不到這個值,直接輸出了${name}字符串,並無解析出值,而在service中卻能取到。明顯在Controller中並無引入properties文件中的變量,而被當作普通的字符串處理了。mvc

通過排查這個項目的web.xml中有2個配置文件,1個spring-config.xml,1個spring-mvc.xml,其中spring-config.xml中定義有placeholder。spa

 

因爲Controller是定義在springmvc的servlet配置文件中的,故只須要將placeholder從新在springmvc的配置中配置一遍,Controller中的@Value註解便能取到值了。xml

相關文章
相關標籤/搜索