注:本文梳理启动流程使用的Spring版本:4.0.2.RELEASE
To use spring configuration, you need to configure a spring listener and startup parameter (context-param) in web.xml, as follows:
<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>
When the web container starts, the following steps are executed for spring loading:
- Execute the ContextLoaderListener listener in web.xml
- Initialize the contextInitialized method in ContextLoaderListener
- The initWebApplicationContext method of the parent class (ContextLoader) is called in the contextInitialized method
- Three tasks are performed in the initWebApplicationContext method
- Create the WebApplicationContext container
- Load the spring configuration file configured in context-param
- Initialize the bean in the configuration file and create the bean in the configuration file
- Create the WebApplicationContext container
When the web container stops, the contextDestroyed method of ContextLoaderListener will be executed to destroy the context container