天天看点

SpringMVC启动分析

以下分析基于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