SpringMVC启动分析

如下分析基于JDK1.8web

dfd2c59af095fb78a6826fce19fc46bb.png

启动的第一步是执行监听器,这里web.xml中配置了一个监听器org.springframework.web.context.ContextLoaderListener
spring

接下来,看ContextLoaderLisener
app

e75b9a2945fdbe5ab5ae239c29903141.png

5b53527236ad1bd5a56c2de6cfc6af8e.png

604e0485caf63f4a31551cd5d58601af.png

285d82d176116a5ebd4290ae56aa1752.png

在Web应用启动的时候,全部的ServletContextListener会在filter和servlet以前执行,因此这里会首先执行contextInitialized方法
ide

6308613ac89a92caa6794dfbf4cf76c2.png


2e13472ae07957f840687cf7cadf52e0.png


接下来,看一下XmlWebApplicationContext在实例化的时候作了什么spa

答案是并无,就是实例化一个对象3d

接下来,isActive确定是false的xml

因而乎,配置XmlWebApplicationContext对象

dc8ddc130acede1bc312c4308ad1222a.png

在这段代码中最重要的是wac.refresh()
blog

refresh()方法前面的文章中已经看过了,这里再也不看了。在这个方法中很重要的一个步骤是获取BeanFactory以前会加载全部的BeanDefinition,而XmlWebApplicationContext中就定义了如何加载这些Bean定义继承

c5ffb1323e5e5e1a05c6dd054ed44171.png

a4a8300c46b2ec82e1e306ef19465b21.png

至此,WebApplicationContext已经建立好了,最后将其设置到ServletContext中



到这里,只完成了监听器的工做,接下来是Servlet

4de7a82ee51cf26acaeb9cdf45ac5b38.png

做为标准的Servlet,DispatcherServlet的init()方法是继承HttpServletBean的,而HttpServletBean在其init()方法中最重要的一件事是调用initServletBean()方法,而initServletBean()在HttpServletBean中是一个抽象方法,具体是在FrameworkServlet中实现的。下面具体看下FrameworkServlet

fa732f7e514fdb1c1bb2747459e2b56b.png

initServletBean中作了两件事情,一个是建立WebApplicationContext,另外一个是初始化SpringMvc的一些组件

下面,重点看这两个方法

14833d48c31ad690292b7d792bd0a18f.png


6d49118d83300593c5bdd6cfe2be83f3.png这里有一点须要注意,在本示例中,咱们在web.xml中配置DispatcherServlet的时候并无指定<init-param>全部,在这一步中getContextConfigLocation()是null

9d0569fb60d4ab1f062226b3563168df.png


又看到调用refresh()方法了,这个方法中重要的一步是加载BeanDefinition。

那么,它从哪儿去加载呢?固然是配置文件啦。

那么,它是怎么找到配置文件的呢?看XmlWebApplicationContext中是如何加载Bean定义的。

3674bd26fd47236c05aee7fa57c07b57.png

ee02b1429904de9d06fb82167474a601.png

至此,SpringMVC的WebApplicationContext也建立好了。

先来总结一下,这一步其实就是基于以前Spring的WebApplicationContext再建立一个SpringMVC本身的WebApplicationContext,两者构成父子关系,由于在建立后置的时候setParent()了。

建立第一个WebApplicationContext的时候依据的是Spring的配置文件applicationContext.xml

建立第二个WebApplicationContext的时候依据的是SpringMVC的配置文件xxx-servlet.xml



最后一步,调用FrameworkServlet的onRefresh()方法,这个方法是在其子类DispatcherServlet中实现的。这一步所作的工做就是将在xxx-servlet.xml中配置的各类组件注入到DispatcherServlet中对应的成员变量中。而这些组件在上一步建立WebApplicationContext的时候已经被容器管理起来了,因此,直接从容器中获取便可。

若是配置文件中没有配置的话,会根据一些策略进行默认的自动配置。

141188309c12934b16be856436e85d63.png

23723dfae20b5eaa48b64337bd95b818.png


30eda0e69d1501733fce047b565c4a9c.png

至此,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

相关文章
相关标签/搜索