Spring @Value注解失败
来源:互联网 发布:淘宝网冬之恋羊绒线 编辑:程序博客网 时间:2024/06/07 13:13
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 0
- Spring @Value注解失败
- Spring @Value注解问题
- 【Spring】学习@Value注解
- spring中@value注解
- spring中@value注解
- Spring 注解@Value详解
- Spring 注解@Value
- Spring @Value注解的使用
- Spring @Value注解的用法
- Spring用resource注解失败
- Spring注解注入失败问题
- spring 3中新增的@value注解
- spring 3中新增的@value注解
- spring注解@Configuration,@Value,@Bean实例
- Spring中Value注解的使用
- spring 3中新增的@value注解
- Spring中Value注解的使用
- spring 3中新增的@value注解
- 设计模式-代理模式和装饰模式
- android webview 访问https页面 SslError 处理
- VS2015 调试后DOS窗口闪一下的解决方案
- FCN中的transplant
- JS爬虫,Java爬虫
- Spring @Value注解失败
- java实现输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
- 单例模式的常见应用场景
- MySQL数据库(23)
- 学习nginx-rtmp:replay push代码流程
- tensorflow之训练数据制作
- 【小程序码
- UVa 437 巴比伦塔(The Tower of Babylon)详细题解
- 3.excel 生成 sql