1. 程式人生 > 程式設計 >淺談Spring與SpringMVC父子容器的關係與初始化

淺談Spring與SpringMVC父子容器的關係與初始化

Spring和SpringMVC的容器具有父子關係,Spring容器為父容器,SpringMVC為子容器,子容器可以引用父容器中的Bean,而父容器不可以引用子容器中的Bean。

瞭解了Spring與SpringMVC父子容器的關係,接下來讓我們看看Spring與SpringMVC容器的初始化過程。

以下講解使用的web.xml檔案如下:

 <context-param>
    <param-name>contextConfigLocation</param-name>//指定spring ioc配置檔案的位置
    <param-value>classpath*:spring/*.xml</param-value>
  </context-param>
  <!-- Creates the Spring Container shared by all Servlets and Filters -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
<!-- 配置DisaptcherServlet -->
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 初始化引數,配置springmvc配置檔案 -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>springMVC配置檔案的路徑</param-value>
    </init-param>
    <!-- web容器啟動時載入該Servlet -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

spring ioc容器初始化的過程

1、web應用程式啟動時,tomcat會讀取web.xml檔案中的context-parm(含有配置檔案的路徑)和listener節點,接著會為應用程式建立一個ServletContext,為全域性共享,Spring ioc容器就是儲存在這裡

2、tomcat將context-param節點轉換為鍵值對,寫入到ServletContext中

3、建立listener節點中的ContextLoaderListener例項,呼叫該例項,初始化webapplicationContext,這是一個介面,其實現類為XmlWebApplicationContext(即spring的IOC容器),其通過ServletContext.getinitialParameter("contextConfigLoaction")從ServletContext中獲取context-param中的值(即spring ioc容器配置檔案的路徑),這就是為什麼要有第二步的原因。接著根據配置檔案的路徑載入配置檔案資訊(其中含有Bean的配置資訊)到WebApplicationContext(即spring ioc容器)中,將WebApplicationContext以WebApplicationContext.ROOTWEBAPPLICATIONCONTEXTATTRIBUTE為屬性Key,將其儲存到ServletContext中,便於獲取。至此,spring ioc容器初始化完畢

4、容器初始化web.xml中配置的servlet,為其初始化自己的上下文資訊servletContext,並載入其設定的配置資訊到該上下文中。將WebApplicationContext(即spring ioc容器)設定為它的父容器。其中便有SpringMVC(假設配置了SpringMVC),這就是為什麼spring ioc是springmvc ioc的父容器的原因

SpringMVC初始化過程

SpringMVC通過web.xml檔案中servlet標籤下的DispatcherServlet類完成自身的初始化

DispatcherServlet類的繼承體系如下:

淺談Spring與SpringMVC父子容器的關係與初始化

請注意每個長方形中第三行的方法,其為完成SpringMVC ioc容器初始化的關鍵。

我們知道,每個servlet在初始化時,會先呼叫servlte的建構函式(為預設建構函式),接著呼叫init函式,而DispatcherServlet的init方法在其父類HttpServlet中。

HttpServlet中的init方法

/DispatcherServlet第一次載入時呼叫init方法
@Override
  public final void init() throws ServletException {
    if (logger.isDebugEnabled()) {
      logger.debug("Initializing servlet '" + getServletName() + "'");
    }
    // Set bean properties from init parameters.
    try {
/*載入web.xml檔案中的servlet標籤中的init-param,其中含有springMVC的配置檔案的名字和路徑
 *若沒有,則預設為(servlet-name)-servlet.xml,
 *預設路徑為WEF—INF下
 */
      PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(),this.requiredProperties);
     //建立BeanWrapper例項,為DispatcherServlet設定屬性
      BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
      ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
      bw.registerCustomEditor(Resource.class,new ResourceEditor(resourceLoader,getEnvironment()));
      initBeanWrapper(bw);
     //把init-param中的引數設定到DispatcherServlet裡面去
      bw.setPropertyValues(pvs,true);
    }
    catch (BeansException ex) {
      logger.error("Failed to set bean properties on servlet '" + getServletName() + "'",ex);
      throw ex;
    }
 
 
    // Let subclasses do whatever initialization they like.
    //該方法在FrameworkServlet中
    initServletBean();
 
 
    if (logger.isDebugEnabled()) {
      logger.debug("Servlet '" + getServletName() + "' configured successfully");
    }
  }

FrameworkServlet中的initServletBean方法

@Override
  protected final void initServletBean() throws ServletException {
    getServletContext().log("Initializing Spring FrameworkServlet '" + getServletName() + "'");
    if (this.logger.isInfoEnabled()) {
      this.logger.info("FrameworkServlet '" + getServletName() + "': initialization started");
    }
    long startTime = System.currentTimeMillis();
 
    try {
      //建立springmvc的ioc容器例項
      this.webApplicationContext = initWebApplicationContext();
      initFrameworkServlet();
    }
    catch (ServletException ex) {
      this.logger.error("Context initialization failed",ex);
      throw ex;
    }
    catch (RuntimeException ex) {
      this.logger.error("Context initialization failed",ex);
      throw ex;
    }
 
    if (this.logger.isInfoEnabled()) {
      long elapsedTime = System.currentTimeMillis() - startTime;
      this.logger.info("FrameworkServlet '" + getServletName() + "': initialization completed in " +
          elapsedTime + " ms");
    }
  }

FrameworkServlet中的initWebapplicationContext方法

protected WebApplicationContext initWebApplicationContext() {
    //首先通過ServletContext獲得spring容器,因為子容器springMVC要和父容器spring容器進行關聯
    //這就是為什麼要在ServletContext中註冊spring ioc容器的原因
    WebApplicationContext rootContext =
        WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    //定義springMVC容器wac
    WebApplicationContext wac = null;
 
 
    //判斷容器是否由程式設計式傳入(即是否已經存在了容器例項),存在的話直接賦值給wac,給springMVC容器設定父容器
    //最後呼叫重新整理函式configureAndRefreshWebApplicationContext(wac),作用是把springMVC的配置資訊載入到容器中去(之前已經將配置資訊的路徑設定到了bw中)
    if (this.webApplicationContext != null) {
      // A context instance was injected at construction time -> use it
      wac = this.webApplicationContext;
      if (wac instanceof ConfigurableWebApplicationContext) {
        ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
        if (!cwac.isActive()) {
 
 
          if (cwac.getParent() == null) {
            // The context instance was injected without an explicit parent -> set
            // the root application context (if any; may be null) as the parent
            //將spring ioc設定為springMVC ioc的父容器
            cwac.setParent(rootContext);
          }
 
 
          configureAndRefreshWebApplicationContext(cwac);
        }
      }
    }
    if (wac == null) {
      // 在ServletContext中尋找是否有springMVC容器,初次執行是沒有的,springMVC初始化完畢ServletContext就有了springMVC容器
      wac = findWebApplicationContext();
    }
 
 
    //當wac既沒有沒被程式設計式註冊到容器中的,也沒在ServletContext找得到,此時就要新建一個springMVC容器
    if (wac == null) {
      // 建立springMVC容器
      wac = createWebApplicationContext(rootContext);
    }
 
 
    if (!this.refreshEventReceived) {
      //到這裡mvc的容器已經建立完畢,接著才是真正呼叫DispatcherServlet的初始化方法onRefresh(wac)
      onRefresh(wac);
    }
 
 
    if (this.publishContext) {
      //將springMVC容器存放到ServletContext中去,方便下次取出來
      String attrName = getServletContextAttributeName();
      getServletContext().setAttribute(attrName,wac);
      if (this.logger.isDebugEnabled()) {
        this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() +
            "' as ServletContext attribute with name [" + attrName + "]");
      }
    }
    return wac;
  }

FrameworkServlet中的createWebApplicationContext(WebApplicationContext parent)方法

protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) {
    Class<?> contextClass = getContextClass();
    if (this.logger.isDebugEnabled()) {
      this.logger.debug("Servlet with name '" + getServletName() +
          "' will try to create custom WebApplicationContext context of class '" +
          contextClass.getName() + "'" + ",using parent context [" + parent + "]");
    }
    if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
      throw new ApplicationContextException(
          "Fatal initialization error in servlet with name '" + getServletName() +
          "': custom WebApplicationContext class [" + contextClass.getName() +
          "] is not of type ConfigurableWebApplicationContext");
    }
    //例項化空白的ioc容器
    ConfigurableWebApplicationContext wac =
        (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
    //給容器設定環境
    wac.setEnvironment(getEnvironment());
    //給容器設定父容器(就是spring容器),兩個ioc容器關聯在一起了
    wac.setParent(parent);
    //給容器載入springMVC的配置資訊,之前已經通過bw將配置檔案路徑寫入到了DispatcherServlet中
    wac.setConfigLocation(getContextConfigLocation());
    //上面提到過這方法,重新整理容器,根據springMVC配置檔案完成初始化操作,此時springMVC容器建立完成
    configureAndRefreshWebApplicationContext(wac);
 
    return wac;
  }

DispatcherServlet的onRefresh(ApplicationContext context)方法

@Override
  protected void onRefresh(ApplicationContext context) {
    initStrategies(context);
  }

DispatcherServlet的initStrategies(ApplicationContext context)方法

protected void initStrategies(ApplicationContext context) {
    initMultipartResolver(context);//檔案上傳解析
    initLocaleResolver(context);//本地解析
    initThemeResolver(context);//主題解析
    initHandlerMappings(context);//url請求對映
    initHandlerAdapters(context);//初始化真正呼叫controloler方法的類
    initHandlerExceptionResolvers(context);//異常解析
    initRequestToViewNameTranslator(context);
    initViewResolvers(context);//檢視解析
    initFlashMapManager(context);
  }

總結以下DispatcherServlet及各個父類(介面)的功能:

HttpServlet:實現了init方法,完成web,xml中與DispatcherServlet有關的引數的讀入,初始化DispatcherServlet。

FrameworkServlet:完成了springMVC ioc 容器的建立,並且將spring ioc容器設定為springMVC ioc容器的父容器,將springMVC ioc容器註冊到ServletContext中

DispatcherServlet:完成策略元件的初始化

至此,SpringMVC容器初始化完成

以上這篇淺談Spring與SpringMVC父子容器的關係與初始化就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。