以下分析基于JDK1.8
<a href="https://s4.51cto.com/oss/201711/01/dfd2c59af095fb78a6826fce19fc46bb.png" target="_blank"></a>
啟動的第一步是執行監聽器,這裡web.xml中配置了一個監聽器org.springframework.web.context.ContextLoaderListener
接下來,看ContextLoaderLisener
<a href="https://s3.51cto.com/oss/201710/27/e75b9a2945fdbe5ab5ae239c29903141.png" target="_blank"></a>
<a href="https://s3.51cto.com/oss/201710/27/5b53527236ad1bd5a56c2de6cfc6af8e.png" target="_blank"></a>
<a href="https://s1.51cto.com/oss/201710/27/604e0485caf63f4a31551cd5d58601af.png" target="_blank"></a>
<a href="https://s1.51cto.com/oss/201710/27/285d82d176116a5ebd4290ae56aa1752.png" target="_blank"></a>
在Web應用啟動的時候,所有的ServletContextListener會在filter和servlet之前執行,是以這裡會首先執行contextInitialized方法
<a href="https://s5.51cto.com/oss/201710/27/6308613ac89a92caa6794dfbf4cf76c2.png" target="_blank"></a>
<a href="https://s1.51cto.com/oss/201710/27/2e13472ae07957f840687cf7cadf52e0.png" target="_blank"></a>
接下來,看一下XmlWebApplicationContext在執行個體化的時候做了什麼
答案是并沒有,就是執行個體化一個對象
接下來,isActive肯定是false的
于是乎,配置XmlWebApplicationContext
<a href="https://s2.51cto.com/oss/201711/01/dc8ddc130acede1bc312c4308ad1222a.png" target="_blank"></a>
在這段代碼中最重要的是wac.refresh()
refresh()方法前面的文章中已經看過了,這裡不再看了。在這個方法中很重要的一個步驟是擷取BeanFactory之前會加載所有的BeanDefinition,而XmlWebApplicationContext中就定義了如何加載這些Bean定義
<a href="https://s5.51cto.com/oss/201711/01/c5ffb1323e5e5e1a05c6dd054ed44171.png" target="_blank"></a>
<a href="https://s5.51cto.com/oss/201711/01/a4a8300c46b2ec82e1e306ef19465b21.png" target="_blank"></a>
至此,WebApplicationContext已經建立好了,最後将其設定到ServletContext中
到這裡,隻完成了監聽器的工作,接下來是Servlet
<a href="https://s1.51cto.com/oss/201711/01/4de7a82ee51cf26acaeb9cdf45ac5b38.png" target="_blank"></a>
作為标準的Servlet,DispatcherServlet的init()方法是繼承HttpServletBean的,而HttpServletBean在其init()方法中最重要的一件事是調用initServletBean()方法,而initServletBean()在HttpServletBean中是一個抽象方法,具體是在FrameworkServlet中實作的。下面具體看下FrameworkServlet
<a href="https://s4.51cto.com/oss/201711/01/fa732f7e514fdb1c1bb2747459e2b56b.png" target="_blank"></a>
initServletBean中做了兩件事情,一個是建立WebApplicationContext,另一個是初始化SpringMvc的一些元件
下面,重點看這兩個方法
<a href="https://s2.51cto.com/oss/201711/01/14833d48c31ad690292b7d792bd0a18f.png" target="_blank"></a>
<a href="https://s5.51cto.com/oss/201711/01/9d0569fb60d4ab1f062226b3563168df.png" target="_blank"></a>
又看到調用refresh()方法了,這個方法中重要的一步是加載BeanDefinition。
那麼,它從哪兒去加載呢?當然是配置檔案啦。
那麼,它是怎麼找到配置檔案的呢?看XmlWebApplicationContext中是如何加載Bean定義的。
<a href="https://s3.51cto.com/oss/201711/01/3674bd26fd47236c05aee7fa57c07b57.png" target="_blank"></a>
<a href="https://s3.51cto.com/oss/201711/01/ee02b1429904de9d06fb82167474a601.png" target="_blank"></a>
至此,SpringMVC的WebApplicationContext也建立好了。
先來總結一下,這一步其實就是基于之前Spring的WebApplicationContext再建立一個SpringMVC自己的WebApplicationContext,二者構成父子關系,因為在建立後置的時候setParent()了。
建立第一個WebApplicationContext的時候依據的是Spring的配置檔案applicationContext.xml
建立第二個WebApplicationContext的時候依據的是SpringMVC的配置檔案xxx-servlet.xml
最後一步,調用FrameworkServlet的onRefresh()方法,這個方法是在其子類DispatcherServlet中實作的。這一步所做的工作就是将在xxx-servlet.xml中配置的各種元件注入到DispatcherServlet中對應的成員變量中。而這些元件在上一步建立WebApplicationContext的時候已經被容器管理起來了,是以,直接從容器中擷取即可。
如果配置檔案中沒有配置的話,會根據一些政策進行預設的自動配置。
<a href="https://s4.51cto.com/oss/201711/01/141188309c12934b16be856436e85d63.png" target="_blank"></a>
<a href="https://s5.51cto.com/oss/201711/01/23723dfae20b5eaa48b64337bd95b818.png" target="_blank"></a>
<a href="https://s5.51cto.com/oss/201711/01/30eda0e69d1501733fce047b565c4a9c.png" target="_blank"></a>
至此,SpringMVC就啟動成功了。
整個SpringMVC啟動的過程就是建立兩個父子WebApplicationContext的過程
下面總一下啟動過程:
(1)建立Spring的WebApplicationContext,并将其放到ServletContext中
(2)根據ServletContext中的WebApplicationContext建立SpringMVC的WebApplicationContext
(3)從上一步中的WebApplicationContext中擷取Bean并且設定到DispatcherServlet中
再簡練一點就是,
(1)執行個體化applicationContext.xml中定義的Bean
(2)執行個體化xxx-servlet.xml中定義的Bean
(3)将SpringMVC自己特有的Bean設定到DispatcherServlet中
其實,後兩步可以歸結為執行個體化DispatcherServlet
本文轉自 手不要亂摸 51CTO部落格,原文連結:http://blog.51cto.com/5880861/1978182