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

项目中经常会用到配置文件,定义成properties的形式比较常见,为了方便使用一般在spring配置文件中做如下配置:

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

这样在程序代码中直接用@Value("${name}"),就能直接取到properties文件中定义的变量值。

但是在项目中发现一个情况,在Controller中取不到这个值,直接输出了${name}字符串,并没有解析出值,而在service中却能取到。明显在Controller中并没有引入properties文件中的变量,而被当做普通的字符串处理了。

经过排查这个项目的web.xml中有2个配置文件,1个spring-config.xml,1个spring-mvc.xml,其中spring-config.xml中定义有placeholder。

由于Controller是定义在springmvc的servlet配置文件中的,故只需要将placeholder重新在springmvc的配置中配置一遍,Controller中的@Value注解便能取到值了。

猜你喜欢

转载自my.oschina.net/u/2262481/blog/1607319
今日推荐