1.今天想使用ApplicationContextAware接口方式去获取上下文环境,从而去获取bean,自己照着例子去写了一遍,但是最后还是报错,找不到这个bean,貌似实现了ApplicationContextAware的这个类根本就没有被Spring初始化的时候初始化执行setApplicationContext()方法(我已经在配置文件配置了这个bean),下面是我的代码:
2.工程代码
(1)web.xml
spring-test contextConfigLocation classpath*:config/spring/local/appcontext-*.xml org.springframework.web.context.ContextLoaderListener index.jsp
(2).spring配置文件,我用的是idea,配置文件在config/spring/local下,Resource下面
(3).SpringTest.java
package com.fcc.spring.test; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.support.ClassPathXmlApplicationContext; import javax.annotation.Resource; public class SpringTest implements ApplicationContextAware{ private static ApplicationContext applicationContext; public static void main(String[] args){ HelloWorld obj = (HelloWorld)SpringTest.getBean("helloWorld"); System.out.println("obj = " + obj); System.out.println("The message value is " + obj.getMessage()); } public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { System.out.println("applicationContext before : " + applicationContext); SpringTest.applicationContext = applicationContext; } public static ApplicationContext getApplicationContext(){ return applicationContext; } public static Object getBean(String name) throws BeansException{ System.out.println("applicationContext = " + applicationContext); return applicationContext.getBean(name); } }
HelloWorld的代码我就不贴了,就是一个简单的JavaBean,有个message:String属性
我理解的初始化顺序是这样的:
启动tomcat容器,加载web.xml,建立整个容器(Servlet容器,这里是tomcat吧)的上下文,ServletContext,这时web.xml有个监听器,就是ContextLoaderListener,监听到这个事件,就会去扫描spring配置文件,默认是applicationContext.xml文件(classpath,idea是Resource下),如果自定义,就应该如web.xml中的
It seems that I have found the problem. I am really stupid, really stupid. . .
It is said that the web.xml file will be started only after the tomcat container is started, but what I wrote is a controller program and there is no tomcat at all. . .
What does it have to do with web containers? There is more than one way to load a spring container.
ioc container is not initialized, instantiate an ApplicationContext first
@青楼guilty ApplicationContextAware has the getBean() method. Why do you have to implement it? Write your own getBean() method. Can't you just use its method directly? Don't understand. So much code, doesn't it mean that spring obtains java beans through application context?
SpringTest(实现ApplicationContextAware的类)
必须要加入spring bean
去管理,可以用Compnent
注解或在xml
文件去配。不然
spring
不会处理这个,这也是applicationContext
为null
reasons.I also encountered the same problem a few days ago. In my case, it was in a class instantiated through reflection and an instance managed by spring. I also wrote a class like this to get the spring instance, but I couldn't inject it at first. , then I just added lazy-init="false". I didn't need to add it when I used spring3. This time I used spring4. I hope it will be helpful to you