版權聲明:轉載請聲明轉自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中DispatcherServlet
的contextConfigLocation
定義掃描Controller的配置文件。
servletContext和兩個配置文件的關係如下:
servletContext>contextLoaderListener->Root ApplicationContext->Spring
servletContext>dispacherServlet->Servlet WebAppContext->SpringMVC
兩者關係
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