问题描述:

有一天小刘见隔壁同事小王抓耳挠腮,想必是遇到了什么问题,过去一问果不其然,小王在spring项目代码中怎么也获取不到通过注解@Autowired 自动注入的bean实例,如果这个bean直接配置在spring文件中就可以获取到,但是如果不配置就获取不到让我们来看看他是怎么获取的。


ApplicationContext context = new ClassPathXmlApplicationContext("Spring-applicationContext.xml");问题就出在这个applicationcontext,如果想要获取这个已经在扫描路径下自动注入的bean需要通过WebApplicationContext去获取,WebApplicationContext和applicationcontext不是同一个容器,这里讲的applicationcontext实际上是rootapplicationcontext,而WebApplicationContext是rootapplicationcontext的扩展它增加了WEB应用特性,还可以视图解析、主题解析、映射,通过ServletContext与servlet关联。


所以想要获取在自动注入并在扫描路径下的bean的时候可以通过webapplication去获取,那么怎么去获取这个WebApplicationContext呢,下面提供几种方式:|

方法一:通过Spring提供的工具类获取ApplicationContext对象
代码:

import org.springframework.web.context.support.WebApplicationContextUtils;
 ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
 ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
 ac1.getBean("beanId");
 ac2.getBean("beanId");


说明:
这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。

上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。

其中 servletContext sc 可以具体 换成 servlet.getServletContext()或者 this.getServletContext() 或者 request.getSession().getServletContext(); 另外,由于spring是注入的对象放在ServletContext中的,所以可以直接在ServletContext取出 WebApplicationContext 对象: WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

方法二:继承自抽象类ApplicationObjectSupport
说明:抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取到ApplicationContext。
Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。

方法三:继承自抽象类WebApplicationObjectSupport
说明:类似上面方法,调用getWebApplicationContext()获取WebApplicationContext

方法四:实现接口ApplicationContextAware(推荐使用)
说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。
Spring初始化时,会通过该方法将ApplicationContext对象注入。


在web应用中一般用ContextLoaderListener加载webapplication,如果需要在action之外或者control类之外获取webapplication思路之一是,单独写个类放在static变量中,
类似于:

public class AppContext {

   private static AppContext instance;

   private AbstractApplicationContext appContext;

   public synchronized static AppContext getInstance() {
     if (instance == null) {
       instance = new AppContext();
     }
     return instance;
   }

   private AppContext() {
     this.appContext = new ClassPathXmlApplicationContext(
         "/applicationContext.xml");
   }

   public AbstractApplicationContext getAppContext() {
     return appContext;
   }
 }

不过这样,还是加载了2次applicationcontext,servlet一次,路径加载一次;觉得不如直接用路径加载,舍掉servlet加载
在网上也找了些其他说法:实现ApplicationContextAware,,, 接口,或者servletcontextAware接口,还要写配置文件。有的竟然要把配置文件里的listener,换成自己的类,这样纯粹多此一举。不过有的应用不是替换,是在补一个listener,
我在一版的jpetstore(具体那一版不知道)里发现了这个:
[web.xml]里
      

<listener>
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>
     
     <listener>
         <listener-class>com.ibatis.jpetstore.util.SpringInit</listener-class>
     </listener>

其中SpringInit实现接口ServletContextListener :
 

package com.ibatis.jpetstore.util;

 import javax.servlet.ServletContextEvent;
 import javax.servlet.ServletContextListener;
 import org.springframework.context.ApplicationContext;
 import org.springframework.web.context.WebApplicationContext;
 import org.springframework.web.context.support.WebApplicationContextUtils;


 public class SpringInit implements ServletContextListener {
     

     private static WebApplicationContext springContext;
     
     public SpringInit() {
         super();
     }
     
     public void contextInitialized(ServletContextEvent event) {
         springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
     }
     

     public void contextDestroyed(ServletContextEvent event) {
     }
     
     public static ApplicationContext getApplicationContext() {
         return springContext;
     }

     
 }

以上就是几种获取webapplicationcontext的方法了。