Spring 版本 3.0.5.Release
@Value注解有两种形式:
1、@Value("${}")
如果只在application-context.xml中注册配置文件:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath:base.properties</value>
<value>classpath:web.properties</value>
</list>
</property>
</bean>
在xxx-servlet.xml中不注册改配置文件,此时@Controller注解的类无法使用@Value("${}")注入常量。(也即配置文件是在ContextLoaderListener初始化时加载。通常@Controller是由DispatcherServlet进行扫描并实例化的)。
这种情况是ContextLoaderListener初始化加载配置文件发生在前,而DispatcherServlet扫描并实例化Controller类是发生在后,这个时候为什么@Value注解失败?
如果我在xxx-servlet.xml中添加上下面的配置@Controller就可以直接使用@Value("${}")注入了。(也即在DispatcherServlet中也进行配置文件加载)
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath:base.properties</value>
<value>classpath:web.properties</value>
</list>
</property>
</bean>
2、@Value("#{webProperties['properties.name']}")
只在application-context.xml中注册配置文件:
<util:properties id="webProperties" location="classpath:web.properties" />
此时,@Controller和其它的@Component都可以使用@Value("#{webProperties['properties.name']}")注入常量。
ContextLoaderListener和DispatcherServlet实例化的对象都能用这种方式注入常量。
现象已经明了,但是为什么会出现这种情况还有待深究,特别是@Value("${}")的形式,似乎ContextLoaderListener加载的配置文件对DispatcherServlet不可见。
检测@Value注入问题最好先排查下bean是否被多次扫描,实例化了多次。然后在@PostConstruct中打印出@Value常量。我遇到的情况是ContextLoaderListener和DispatcherServlet都扫描并实例化了@Controller注解类,但是DispatcherServlet实例化的类@Value注入常量失败,ContextLoaderListener实例化的类@Value注入常量成功,但是ContextLoaderListener实例化早,被覆盖掉了。最终导致实例化的@Controller类是没有注入常量的。