关于springMvc启动容器的时候配置的ContextLoadListener,你必须知道的概念

一、ServletContext是什么?

ServletContext是一个web应用的上下文,是一个全局信息的存储空间,代表当前web应用。

二、ServletContext什么时候创建?

ServletContext在web应用(服务器)启动时创建。

三、ServletContext什么时候销毁?

ServletContext在Web应用(服务器)关闭时释放。

四、ServletContext包含哪些东西?

可以debug源码看一下:其中parameters参数是web.xml配置文件中<context-param></context-param>节点内容,通过转换为key-value的形式存在ServletContext中。


关于springMvc启动容器的时候配置的ContextLoadListener,你必须知道的概念

 

其中parameters参数是web.xml配置文件中<context-param></context-param>节点内容,通过转换为key-value的形式存在ServletContext中。

五、ServletContext与Spring容器(ApplicationContext)的关系

1、ServletContext创建之后,然后读取<context-param></context-param>节点内容;

2、触发ServletContextEvent事件,ServletContextListener监听这个事件,ServletContextListener有两个抽象方法,分别是ServletContext初始化时调用方法和ServletContext销毁时调用的方法。

关于springMvc启动容器的时候配置的ContextLoadListener,你必须知道的概念

3、ContextLoaderListener实继承ContextLoader并实现ServletContextListener,所以也监听ServletContextEvent事件,事件触发后ContextLoaderListener会执行初始化方法。

关于springMvc启动容器的时候配置的ContextLoadListener,你必须知道的概念

六、Spring容器(ApplicationContext)创建过程?

1、ContextLoaderListener监听ServletContextEvent事件,执行初始化contextInitialized(ServletContextEvent event)方法;

2、先判断是否已经创建过ApplicationContext;

关于springMvc启动容器的时候配置的ContextLoadListener,你必须知道的概念

3、创建WebApplicationContext对象(Spring容器);

关于springMvc启动容器的时候配置的ContextLoadListener,你必须知道的概念

4、转换成ConfigurableWebApplicationContext 对象;

关于springMvc启动容器的时候配置的ContextLoadListener,你必须知道的概念

5、执行configureAndRefreshWebApplicationContext方法,封装ApplicationContext上下文数据,其中会从ServletContext中读取applicationContext.xml配置,调用refresh方法完成所有bean的解析初始化创建。
关于springMvc启动容器的时候配置的ContextLoadListener,你必须知道的概念

6、ConfigurableWebApplicationContext 创建之后放到ServletContext中;

关于springMvc启动容器的时候配置的ContextLoadListener,你必须知道的概念