org.springframework.web.context.ContextLoaderListener 1
org.springframework.web.servlet.DispatcherServlet 2
SpringWebMvc中同時配置1和2
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:ApplicationContext.xml,classpath:RedisContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMvcContext.xml</param-value>
</init-param>
</servlet>
由於1被配置在監聽器標籤,並實現ServletContextListener接口,因此當容器啓動後,1會進行org.springframework.web.context.ContextLoaderListener#contextInitialized方法進行初始化。
初始化後,此時項目處於等待狀態,等待第一個請求到來,當第一個請求到達時,2纔會進行實例化並調用。
具體實例化過程,參見:https://www.cnblogs.com/hfultrastrong/p/10830517.htmlhtml