springMVC(12) ContextLoaderListener

前言

我们打开springMVC工程的web.xml,我们可以发现这样一行配置


    org.springframework.web.context.ContextLoaderListener

这个监听器就是用来初始化spring容器的。

springMVC启动过程

摘自https://segmentfault.com/q/1010000000210417

  1. 首先,对于一个web应用,其部署在web容器中,web容器提供其一个全局的上下文环境,这个上下文就是ServletContext,其为后面的spring IoC容器提供宿主环境;
  2. 其次,在web.xml中会提供有contextLoaderListener。在web容器启动时,会触发容器初始化事件,此时contextLoaderListener会监听到这个事件,其contextInitialized方法会被调用,在这个方法中,spring会初始化一个启动上下文,这个上下文被称为根上下文,即WebApplicationContext,这是一个接口类,确切的说,其实际的实现类是XmlWebApplicationContext。这个就是spring的IoC容器,其对应的Bean定义的配置由web.xml中的context-param标签指定。在这个IoC容器初始化完毕后,spring以WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE为属性Key,将其存储到ServletContext中,便于获取;
  3. 再次,contextLoaderListener监听器初始化完毕后,开始初始化web.xml中配置的Servlet,这个servlet可以配置多个,以最常见的DispatcherServlet为例,这个servlet实际上是一个标准的前端控制器,用以转发、匹配、处理每个servlet请求。DispatcherServlet上下文在初始化的时候会建立自己的IoC上下文,用以持有spring mvc相关的bean。在建立DispatcherServlet自己的IoC上下文时,会利用WebApplicationContext.ROOTWEBAPPLICATIONCONTEXTATTRIBUTE先从ServletContext中获取之前的根上下文(即WebApplicationContext)作为自己上下文的parent上下文。有了这个parent上下文之后,再初始化自己持有的上下文。这个DispatcherServlet初始化自己上下文的工作在其initStrategies方法中可以看到,大概的工作就是初始化处理器映射、视图解析等。这个servlet自己持有的上下文默认实现类也是mlWebApplicationContext。初始化完毕后,spring以与servlet的名字相关(此处不是简单的以servlet名为Key,而是通过一些转换,具体可自行查看源码)的属性为属性Key,也将其存到ServletContext中,以便后续使用。这样每个servlet就持有自己的上下文,即拥有自己独立的bean空间,同时各个servlet共享相同的bean,即根上下文(第2步中初始化的上下文)定义的那些bean。

从这段话中我们可以知道ContextLoaderListener的作用。ContextLoaderListener主要实现了ServletContextListener接口中的两个函数contextInitialized和contextDestroyed,分别在web容器启动时执行和容器销毁时执行。

类图

我们可以很容易地画出ContextLoaderListener类的结构。

springMVC(12) ContextLoaderListener_第1张图片
ContextLoaderListener类图

简单地先说一下这个类图。其中ContextLoaderListener实现了ServletContextListener,而真正的具体实现方式确实交给ContextLoader来完成的。

EventListener接口

public interface EventListener {
}

我们可以看到EventListener接口中没有任何东西。那么很显然EventListener只是一个标记接口,就类似于Serializable一样。所有的时间监听器接口都必须扩展该接口。

ServletContextListener接口

public interface ServletContextListener extends EventListener {
  
   // 在web容器初始化时执行。会在filter和servlet初始化之前执行
    public void contextInitialized ( ServletContextEvent sce );

   // 在web容器关闭时执行。会在所有的servlets和filters执行destroy之后执行
    public void contextDestroyed ( ServletContextEvent sce );
}

ContextLoaderListener类

public class ContextLoaderListener extends ContextLoader implements ServletContextListener {

   public ContextLoaderListener() {
   }

   public ContextLoaderListener(WebApplicationContext context) {
      super(context);
   }

   // 初始化根WebApplicationContext
   @Override
   public void contextInitialized(ServletContextEvent event) {
      initWebApplicationContext(event.getServletContext());
   }

   // 关闭根WebApplicationContext
   @Override
   public void contextDestroyed(ServletContextEvent event) {
      closeWebApplicationContext(event.getServletContext());
      ContextCleanupListener.cleanupAttributes(event.getServletContext());
   }
}

此处印证了前面类图那处所说的,最终真正的实现是由ContextLoader来做的。

ContextLoader类

首先从ContextLoaderListener类中contextInitialized调用的initWebApplicationContext方法开始看起。

public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
   // 判断web.xml中是否存在不止一个ContextLoader
   if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
      throw new IllegalStateException(
            "Cannot initialize context because there is already a root application context present - " +
            "check whether you have multiple ContextLoader* definitions in your web.xml!");
   }

   Log logger = LogFactory.getLog(ContextLoader.class);
   servletContext.log("Initializing Spring root WebApplicationContext");
   if (logger.isInfoEnabled()) {
      logger.info("Root WebApplicationContext: initialization started");
   }
   long startTime = System.currentTimeMillis();

   try {
      // 创建WebApplicationContext
      if (this.context == null) {
         this.context = createWebApplicationContext(servletContext);
      }
      if (this.context instanceof ConfigurableWebApplicationContext) {
         ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
         if (!cwac.isActive()) { // cwac还没有刷新过
            // 设置父上下文
            if (cwac.getParent() == null) {
               ApplicationContext parent = loadParentContext(servletContext);
               cwac.setParent(parent);
            }
            // 初始化WebApplicationContext
            configureAndRefreshWebApplicationContext(cwac, servletContext);
         }
      }
      // 将this.context设置为servletContext的ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE属性,这样就可以直接通过servletContext中获取访问了
      servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

      // 本地实例变量中存储Context,保证哪怕ServletContext关闭了也可以访问。暂不清楚为何要以下面的形式来实现
      ClassLoader ccl = Thread.currentThread().getContextClassLoader();
      if (ccl == ContextLoader.class.getClassLoader()) {
         currentContext = this.context;
      }
      else if (ccl != null) {
         currentContextPerThread.put(ccl, this.context);
      }

      if (logger.isDebugEnabled()) {
         logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
               WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
      }
      if (logger.isInfoEnabled()) {
         long elapsedTime = System.currentTimeMillis() - startTime;
         logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
      }

      return this.context;
   }
   catch (RuntimeException ex) {
      logger.error("Context initialization failed", ex);
      servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
      throw ex;
   }
   catch (Error err) {
      logger.error("Context initialization failed", err);
      servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);
      throw err;
   }
}

我们来看看其中的createWebApplicationContext这一步,

// createWebApplicationContext方法

protected WebApplicationContext createWebApplicationContext(ServletContext sc) {
   Class contextClass = determineContextClass(sc);
   // 判断contextClass是否ConfigurableWebApplicationContext的子类
   if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
      throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
            "] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
   }
   return (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
}
// determineContextClass方法

protected Class determineContextClass(ServletContext servletContext) {
   String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);
   // 使用用户指定的contextClass
   if (contextClassName != null) {
      try {
         return ClassUtils.forName(contextClassName, ClassUtils.getDefaultClassLoader());
      }
      catch (ClassNotFoundException ex) {
         throw new ApplicationContextException(
               "Failed to load custom context class [" + contextClassName + "]", ex);
      }
   }
   // 使用默认的WebApplicationContext,即XmlWebApplicationContext
   else {
      contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());
      try {
         return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());
      }
      catch (ClassNotFoundException ex) {
         throw new ApplicationContextException(
               "Failed to load default context class [" + contextClassName + "]", ex);
      }
   }
}
// defaultStrategies的定义和初始化部分
// defaultStrategies中最终只有这样一条属性
// org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext

private static final String DEFAULT_STRATEGIES_PATH = "ContextLoader.properties";

private static final Properties defaultStrategies;

static {
   try {
      ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class);
      defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
   }
   catch (IOException ex) {
      throw new IllegalStateException("Could not load 'ContextLoader.properties': " + ex.getMessage());
   }
}
# ContextLoader.properties文件

org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext

这样子的话,我们梳理一下整个流程:

  1. 判断ContextLoader是否存在冲突
  2. 创建WebApplicationContext
  3. 设置WebApplicationContext的父上下文
  4. 初始化WebApplicationContext
  5. 向servletContext的ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE属性设置WebApplicationContext,以便于可以直接从servletContext中进行访问
  6. 本地实例变量存储context

你可能感兴趣的:(springMVC(12) ContextLoaderListener)