本文是Spring版本5.0.7的
在搭建SSM項目是都需要在web.xml文件裏面配置一個監聽器以及啓動參數(context-param)如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</context-param>
<!-- 監聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
先來張圖
web容器啓動時Spring啓動流程:
1.通過監聽器執行ContextLoaderListener.
2.初始化ContextLoaderListener裏面的Contextinitialed方法
3.初始化父類(ContextLoader)的initContextApplicationContext方法
4.在initContextApplicationContext方法中要執行三個任務
①創建WebApplicationContext容器
②加載context-param中配置的spring配置文件
③初始化配置文件中及創建配置文件中的bean
5.web容器停止時候會執行ContextLoaderListener的contextDestroyed方法銷燬context容器