@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

相关文章
相关标签/搜索