Spring @Value注解失败

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类是没有注入常量的。


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值