在前面我们分析了IoC容器的基本实现,下面我们来看看在Web容器中,Spring MVC是建立在IoC容器基础上的.了解Spring MVC,首先要了解Spring IoC容器是如何在Web环境中被载入并起作用的.
Spring IoC是一个独立的模块,并不是直接在Web容器中发挥作用的,要在Web环境中使用IoC容器,需要Spring为IoC设计一个启动过程,把IoC容器导入,并在Web容器中建立起来.
先来看一个实际项目场景:
![img_06da42e16f9568866cad9fbcb78a5b52.png](https://i-blog.csdnimg.cn/blog_migrate/7bafe9274e2a2f7fe472f7246e58346b.png)
上面是web.xml配置文件中的主要部分
contextConfigLocation 对应的value是Spring配置文件的绝对路径
监听器主要用来对Servlet容器(这里指Tomcat)的行为进行监听
我们先来看看监听器类 ContextLoaderListener 中有什么东西
![img_cfc7196316717a5d439aea6fcc8e237f.png](https://i-blog.csdnimg.cn/blog_migrate/bb24347089f7e1d79baafa7c6c393219.png)
![img_4792c298a93664a1de42b1d2a4886e7f.png](https://i-blog.csdnimg.cn/blog_migrate/49297a6b6c043349fc3d56114a9c7941.png)
可以发现 ContextLoaderListener 继承自 ContextLoader,并且还实现了 ServletContextListener 并且它的构造函数中传入了一个WebApplicationContext,它是继承自ApplicationContext接口的高级IoC容器。
![img_94f375fa70b9a918ddf201e00324bdd0.png](https://i-blog.csdnimg.cn/blog_migrate/6570f40f6e7d64ed1a7abde8bcbeb567.png)
ServletContextListener 是 Servlet 中比较重要的一个接口:监听 Servlet 容器的启动和销毁事件.所以在 ContextLoaderListener 中:
contextInitialized :参数为所要监听的ServletContextEvent,也就是Tomcat启动加载完web.xml会产生的事件,ServletContextEvent 持有从web.xml加载的初始化配置的 ServletContext 上下文
contextDestroyed :在Tomcat关闭的时候执行该方法
启动时,ServletContextListener 的执行顺序与web.xml中的配置顺序一致,停止时执行顺序正相反
梳理流程:当Servlet容器启动事件发生时,将被ContextLoaderLister 监听。此时 ContextLoaderListener 会调用实现 ServletContextListener 接口后实现的 contextInitialized 方法,并把在web.xml加载初始化后获取的 ServletContext 传入initWebApplicationContext方法中进行IoC容器的初始化
initWebApplicationContext 方法从 ContextLoader 继承而来,进入ContextLoader 源码中看看
![img_f8a9bf855460843819431963cd9ff6bb.png](https://i-blog.csdnimg.cn/blog_migrate/cfd9a59ffdda4ec7cde65904d15af340.png)
建一个 ClassPathResource 对象,同时把值为 ContextLoader.properties 的一个常量作为参数传入。易知ContextLoader.properties 文件与 ContextLoader 类是在同一个目录下的;ContextLoader.properties 文件内容如下
org.springframework.web.context.WebApplicationContext=
org.springframework.web.context.support.XmlWebApplicationContext
因此可知Spring默认初始化的是 XmlWebApplicationContext 容器
得到一个 Properties 对象,后面将根据类名来创建对应的 ApplicationContext 容器
下面看看 initiWebApplicationContext 方法
![img_6ae53e41edd7f8917f325829a7bd5f77.png](https://i-blog.csdnimg.cn/blog_migrate/616f815c21e0c0b80dcaa7e2733a68b1.png)
![img_a9c1a5f8eff5e1ec339ef1489faffd17.png](https://i-blog.csdnimg.cn/blog_migrate/158601a19e5cb244cd26cc742d29dcf6.png)
现在可以接着刚才的流程:当调用ContextLoaderListener中的initWebApplicationContext方法并且将获取到的servletContext作为参数传入之后,initWebApplicationContext首先会尝试从servletContext中获取根容器,如果容器不为空,则容器初始化失败,因为web.xml中可能定义了多个IoC容器的加载器。假如此时容器还未初始化,则调用createWebApplicationContext方法创建一个容器。创建完容器之后,将会调用一个非常重要的configureAndRefreshWebApplicationContext方法。在执行这个方法的时候,会将从ApplicationContext.xml配置文件中获取到的内容配置到已经创建好了的XmlWebApplicationContext容器中去,并调用refresh方法来完成容器的初始化。然后,再将已经完成初始化的XmlWebApplicationContext容器注册到servletContext中去。
其实在Web容器中,ServletContext为Spring的IoC容器提供了宿主环境,对应的建立起一个IoC容器的体系。其中,首先需要建立的是根上下文,这个上下文持有的对象可以有业务对象、数据存取对象、资源、事务管理器等各种中间层对象。在这个上下文的基础上,与Web MVC相关还会有一个上下文来保持控制器之类的MVC对象,这样就构成了一个层次化的上下文结构。因为在initWebApplicationContext方法中我们可以看到其实创建ApplicationContext容器的工作是交由createWebApplicationContext方法来实现的,下面我们来看看这个方法
![img_8f719ecfc736ad4dcd594b28aac0e82f.png](https://i-blog.csdnimg.cn/blog_migrate/f3452d1081574803d7692a4321bbd31c.png)
此方法功能
决定要创建的ApplicationContext类型
实例化一个ApplicationContext
那么它是如何决定要创建的ApplicationContext类型的呢?
起作用的是determineContextClass方法
![img_e1102b7ab8c414d0988ff031c89f0a36.png](https://i-blog.csdnimg.cn/blog_migrate/bb7dc103c4638c5589f56402acf6113f.png)
完成IoC容器的创建后,在initWebApplicationContext中将调用configureAndRefreshWebApplicationContext初始化该容器
为创建好的IoC容器设置Web应用的上下文,以便二者整合
为同一个IoC容器设置配置文件的绝对路径
调用IoC容器的refresh函数对其进行初始化
![img_2d6dc729fa15afcce15b99b792968a2f.png](https://i-blog.csdnimg.cn/blog_migrate/355f2e25ac39fbb687055543a5c9383b.png)