SpringMVC 容器和啓動分析

    在分析SpringMVC容器以前,咱們先了解下ServletContext和ServletContextListener。
    什麼是ServletContext?
    ServletContext表明一個Web應用程序的上下文,程序開始時建立,程序結束時銷燬,ServletContext是一個全局信息空間,一個程序只有一個ServletContext,ServletContext能夠用來存儲屬性,獲取資源url等,ServletContext接口由Servlet容器實現。
    什麼是ServletContextListener?
  ServletContextListener是用來監聽ServletContext生命週期變化的,程序啓動時,ServletContextListener的contextInitialized方法會被調用,能夠在裏面建立好緩存;程序關閉時,ServletContextListener的contextDestroyed方法會被調用,能夠在裏面保存緩存信息。

讓咱們看下web.xml配置:前端

<web-app>
    <display-name>Archetype Created Web Application</display-name>

    <context-param>
        <param-name>log4jConfig</param-name>
        <param-value>classpath:/log4j.properties</param-value>
    </context-param>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <!-- ContextLoaderListener繼承ServletContextListener用於監聽ServletContext變化 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Servlet初始化配置 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>WEB-INF/springmvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
</web-app>

    在web.xml配置文件中,有兩個主要的配置:ContextLoaderListener和DispatcherServlet。一樣的關於spring配置文件的相關配置也有兩部分:context-param中的contextConfigLocation和DispatcherServlet中的init-param。

    在SpringMVC中,Spring Context是以父子的繼承結構存在的。Web環境中存在一個Root Context,這個Context是整個應用的根上下文,是其餘context的parent Context。同時SpringMVC也對應的持有一個獨立的Servlet Context,它是Root Context的子上下文,爲了區別於ServletContext,後面統稱爲MVC Context。二者都是WebApplicationContext,MVC Context能夠調用Root Context配置,反過來則不行。ServletContext則爲Root Context和MVC Context提供了宿主環境,而ServletContext則由Servlet容器提供。java

  • Root Context是在ContextLoaderListener中配置的,當Web應用程序建立ServletContext後,會調用ContextLoaderListener的contextInitialized方法,ContextLoaderListener讀取context-param中的contextConfigLocation指定的配置文件,默認爲/WEB-INF/applicationContext.xml文件,建立Root Context。Root Context主要負責建立業務有關的對象,Dao, 事務等。
  • MVC Context是在DispatcherServlet中配置的,當Web應用程序啓動後會調用DispatcherServlet父類HttpServletBean的init方法,FrameworkServlet則實現了此方法,FrameworkServlet讀取DispatchServlet中的init-param中的contextConfigLocation指定的配置文件,默認爲/WEB-INF/[servlet-name]-servlet.xml文件,建立MVC Context。MVC Context負責建立Servlet相關的組件,如Controller, ViewResolver等。


SpringMVC啓動過程大體分爲兩個過程:web

  • ContextLoaderListener初始化,實例化Root IOC容器,並註冊到ServletContext中;
  • DispatcherServlet初始化,實例化MVC IOC容器做爲Root IOC的子容器,並註冊到ServletContext中;

    主要涉及到WebApplicationContext, XmlWebApplicationContext, ContextLoaderListener, 
ContextLoader, DispatcherServlet, FrameworkServlet幾個類。

啓動過程:
1. WepApplicationContext擴展ApplicationContext接口添加對Web環境支持spring

public interface WebApplicationContext extends ApplicationContext {
	String ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";
	String SCOPE_REQUEST = "request";
	String SCOPE_SESSION = "session";
	String SCOPE_GLOBAL_SESSION = "globalSession";
	String SCOPE_APPLICATION = "application";
	String SERVLET_CONTEXT_BEAN_NAME = "servletContext";
	String CONTEXT_PARAMETERS_BEAN_NAME = "contextParameters";
	String CONTEXT_ATTRIBUTES_BEAN_NAME = "contextAttributes";

	ServletContext getServletContext();
}


2. ContextLoaderListener繼承ServletContextListener建立Root Context
    1) ContextLoaderListener繼承ServletContextListener,經過initWebApplicationContext初始化Root Context緩存

public void contextInitialized(ServletContextEvent event) {
    initWebApplicationContext(event.getServletContext());
}


    2) ContextLoader初始化Root Context,其中包括entity, dao, service等組件的初始化,並將Root Context註冊到ServletContext中,以便後面MVC Context調用Root Context中的組件。session

public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
    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 {
        // Store context in local instance variable, to guarantee that
        // it is available on ServletContext shutdown.
        if (this.context == null) {
            // 建立Root Context XmlWebApplicationContext
            this.context = createWebApplicationContext(servletContext);
        }
        if (this.context instanceof ConfigurableWebApplicationContext) {
            ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
            if (!cwac.isActive()) {
                // The context has not yet been refreshed -> provide services such as
                // setting the parent context, setting the application context id, etc
                if (cwac.getParent() == null) {
                    // The context instance was injected without an explicit parent ->
                    // determine parent for root web application context, if any.
                    
                    ApplicationContext parent = loadParentContext(servletContext);
                    cwac.setParent(parent);
                }
                // applicationContext配置加載而且刷新Root Context
                configureAndRefreshWebApplicationContext(cwac, servletContext);
            }
        }
        // 將context放到servletContext中WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE屬性下
        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

        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;
    }
}

    
    3) ContextLoader加載contextConfigLocation配置並刷新容器mvc

protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) {
    if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
        // The application context id is still set to its original default value
        // -> assign a more useful id based on available information
        String idParam = sc.getInitParameter(CONTEXT_ID_PARAM);
        if (idParam != null) {
            wac.setId(idParam);
        }
        else {
            // Generate default id...
            wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
                    ObjectUtils.getDisplayString(sc.getContextPath()));
        }
    }

    wac.setServletContext(sc);
    String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM);
    if (configLocationParam != null) {
        // 加載contextConfigLocation配置
        wac.setConfigLocation(configLocationParam);
    }

    // The wac environment's #initPropertySources will be called in any case when the context
    // is refreshed; do it eagerly here to ensure servlet property sources are in place for
    // use in any post-processing or initialization that occurs below prior to #refresh
    ConfigurableEnvironment env = wac.getEnvironment();
    if (env instanceof ConfigurableWebEnvironment) {
        ((ConfigurableWebEnvironment) env).initPropertySources(sc, null);
    }

    customizeContext(sc, wac);
    wac.refresh();
}


3. DispatcherServlet繼承HttpServlet建立MVCContext
    1) HttpServletBean繼承HttpServlet,根據Servlet規範,Web容器調用它的init方法進行初始化app

public final void init() throws ServletException {
    if (logger.isDebugEnabled()) {
        logger.debug("Initializing servlet '" + getServletName() + "'");

    }  

    // 1. 設置servlet初始化參數到組件上
    try {
        PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
        BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
        ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
        bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
        initBeanWrapper(bw);
        bw.setPropertyValues(pvs, true);
    }  catch (BeansException ex) {
        logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
        throw ex;
    }

    // 2. 提供給子類的擴展點
    initServletBean();
    if (logger.isDebugEnabled()) {
        logger.debug("Servlet '" + getServletName() + "' configured successfully");
    }
}


    2) FrameworkServlet繼承HttpServletBean經過initServletBean初始化MVC Contextide

protected final void initServletBean() throws ServletException {
    /*省略部分代碼*/
    try {
        // 1. 初始化Web上下文
        this.webApplicationContext = initWebApplicationContext();
        // 2. 提供給子類擴展點
        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;
    }
    /*省略部分代碼*/
}


    3) FrameworkServlet經過initWebApplicationContext方法初始化MVC contextpost

protected WebApplicationContext initWebApplicationContext() {
    WebApplicationContext rootContext =
            WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    WebApplicationContext wac = null;

    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()) {
                // The context has not yet been refreshed -> provide services such as
                // setting the parent context, setting the application context id, etc
                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
                    cwac.setParent(rootContext);
                }
                configureAndRefreshWebApplicationContext(cwac);
            }
        }
    }
    if (wac == null) {
        // No context instance was injected at construction time -> see if one
        // has been registered in the servlet context. If one exists, it is assumed
        // that the parent context (if any) has already been set and that the
        // user has performed any initialization such as setting the context id
        wac = findWebApplicationContext();
    }
    if (wac == null) {
        // No context instance is defined for this servlet -> create a local one
        // 建立mvc context並設置root context爲parent context
        wac = createWebApplicationContext(rootContext);
    }

    if (!this.refreshEventReceived) {
        // Either the context is not a ConfigurableApplicationContext with refresh
        // support or the context injected at construction time had already been
        // refreshed -> trigger initial onRefresh manually here.
        onRefresh(wac);
    }

    if (this.publishContext) {
        // Publish the context as a servlet context attribute.
        String attrName = getServletContextAttributeName();
        
        // 將context加到servletContext中
        getServletContext().setAttribute(attrName, wac);
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() +
                    "' as ServletContext attribute with name [" + attrName + "]");
        }
    }

    return wac;
}


    4) FrameworkServlet 加載配置建立MVC Context,並將Root Context設爲本身的parent Context,從而能夠調用Root Context中的組件

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");
    }
    ConfigurableWebApplicationContext wac =
            (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);

    wac.setEnvironment(getEnvironment());
    // 設置root context爲parent context
    wac.setParent(parent);
    wac.setConfigLocation(getContextConfigLocation());

    configureAndRefreshWebApplicationContext(wac);

    return wac;
}


    5)  FrameworkServlet 刷新mvc容器

protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) {
    if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
        // The application context id is still set to its original default value
        // -> assign a more useful id based on available information
        if (this.contextId != null) {
            wac.setId(this.contextId);
        }
        else {
            // Generate default id...
            wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
                    ObjectUtils.getDisplayString(getServletContext().getContextPath()) + "/" + getServletName());
        }
    }

    wac.setServletContext(getServletContext());
    wac.setServletConfig(getServletConfig());
    wac.setNamespace(getNamespace());
    wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener()));

    // The wac environment's #initPropertySources will be called in any case when the context
    // is refreshed; do it eagerly here to ensure servlet property sources are in place for
    // use in any post-processing or initialization that occurs below prior to #refresh
    ConfigurableEnvironment env = wac.getEnvironment();
    if (env instanceof ConfigurableWebEnvironment) {
        ((ConfigurableWebEnvironment) env).initPropertySources(getServletContext(), getServletConfig());
    }

    postProcessWebApplicationContext(wac);
    applyInitializers(wac);
    // 容器刷新時會觸發FrameworkServlet onRefresh方法
    wac.refresh();
}


    6) DispatcherServlet繼承FrameworkServlet實現onRefresh()方法提供前端配置

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

/**
 * Initialize the strategy objects that this servlet uses.
 * <p>May be overridden in subclasses in order to initialize further strategy objects.
 */
protected void initStrategies(ApplicationContext context) {
    initMultipartResolver(context);
    initLocaleResolver(context);
    initThemeResolver(context);
    // 初始化映射包括Controller和靜態資源等
    initHandlerMappings(context);
    initHandlerAdapters(context);
    initHandlerExceptionResolvers(context);
    initRequestToViewNameTranslator(context);
    initViewResolvers(context);
    initFlashMapManager(context);
}

    從啓動過程當中咱們能夠發現,Root Context做爲MVC Context的parent context存在,做爲子容器的MVC Context能夠調用Root Context中的配置,反過來則不行。從啓動過程當中也能夠看出在配置若是在Root Context中配置Controller會致使Service事務失效,由於此時的Service事務仍是未裝配事務的,只有把Controller放到MVC Context中配置。

相關文章
相關標籤/搜索