继承ApplicationContextAware
接口,并注入静态ApplicationContext
对象
在Spring的配置文件中声明新定义的Bean
使用
问题
当使用XML配置的方式,SpringContextHolder可以获得所有的声明Bean,但是一旦使用注解方式声明Bean,就会导致找不到。
SpringContextHolder.getBean("testService")
会是Null。
问题是由于Spring MVC与Spring的上下文管理不在一个容器中,而我的自动扫描路径配置在了Spring MVC的web-app-servlet.xml中,导致Spring找不这些bean
解决方法
解决方法有2中
- 第一种是把所有的自动扫描放到Spring的ApplicationContext.xml文件中
- 第二种是在web.xml文件中Spring的contextConfigLocation中添加上web-app-servlet.xml的配置