SpringMVC中的RootWebApplicationContext與ServletWebApplicationContext

版權聲明:轉載請聲明轉自http://blog.csdn.net/thewindkee https://blog.csdn.net/thewindkee/article/details/95483639


RootWebApplicationContext以下簡稱RootAC
ServletWebApplicationContext以下簡稱ServletAC

簡介

Web on Servlet Stack 1.1.1. Context Hierarchy中描述到了SpringMVC中兩個WebApplicationContext的繼承關係。RootAC會被注入到ServletAC的parentBeanFactory中。
在這裏插入圖片描述

典型的SpringMVC中web.xml配置

<web-app>
    <display-name>spring-web</display-name>
    <context-param>
    <!--spring相關-->
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-a.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatch</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
        	 <!--springMVC相關-->
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:dispatch-servlet.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatch</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

通常,
在web.xml中context-param的使用contextConfigLocation定義一個配置掃描Spring的service和dao的配置文件。ContextLoaderListener,ContextLoaderListener本質是一個ServletContextListener,在創建ServletContext的時候會去加載這些配置文件, 這些配置文件可以看做Spring相關的配置。(如果這裏掃描了SpringMVC相關的配置,某些bean被加載多次)

ServletContextListener監聽ServletContext。當創建ServletContext時,激發contextInitialized(ServletContextEvent sce)方法;
在web.xml中DispatcherServletcontextConfigLocation定義掃描Controller的配置文件。

servletContext和兩個配置文件的關係如下:

servletContext>contextLoaderListener->Root ApplicationContext->Spring
servletContext>dispacherServlet->Servlet WebAppContext->SpringMVC

兩者關係

ServletAC中含有RootAC

ServletAC包含RootAC
parentBeanFactory中含有RootAC
在這裏插入圖片描述

從parentBeanFactory中獲取bean

用當無法找到對應的bean則從parentBeanFactory中查找
在這裏插入圖片描述

如果不配置ContextListener和對應contextConfigLocation,這裏RootAC也是null,ServletAC的parent是null
在這裏插入圖片描述

代碼中獲取RootWebApplicationContext與ServletWebApplicationContext

推薦使用RequestContextUtils.findWebApplicationContext(request);獲取ServletWebApplicationContext,該applicationContext包含SpringMVC與Spring配置的bean。

    WebApplicationContext servletApplicationContext = RequestContextUtils.findWebApplicationContext(request);//推薦,ServletAC包含RootAC
    WebApplicationContext rootApplicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());//RootAC

Root WebApplicationContext

在這裏插入圖片描述

Servlet WebApplicationContext

在這裏插入圖片描述

★什麼時候將RootAC注入到ServletAC中的?

    • 當啓動的時候先會初始化ContextLoaderListener對應的RootAC。(TODO)
    • 當第一次HTTP訪問的時候會將初始化ServletAC,並將RootAC注入其中。

DispatcherServlet本質是一個HttpServletBean繼承HttpServlet
在這裏插入圖片描述

那麼當一次過來的時候 由於我們配置的<url-pattern>/*</url-pattern>將所有的請求交給DispacherServlet處理。

調用鏈

httpServletBean.init->initServletBean->initWebApplicationContext->createWebApplicationContext(root)->wac.refresh()

在這裏插入圖片描述
將RootAC傳入,準備創建ServletAC
在這裏插入圖片描述

設置ServletAC的contextConfigLocation,並調用wac.setParent將RootAC注入到ServletAC
在這裏插入圖片描述

refresh啓動ApplicationContext
在這裏插入圖片描述

refresh-SpringIOC源碼學習總結
Web on Servlet Stack 1.1.1. Context Hierarchy
《看透SpringMVC》第9章 創建SpringMVC之器

本文發佈於2019年7月11日16:26:28

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章