1、ContextLoaderListener的相关接口和类
那么让我们来看看spring是如何通过ContextLoaderListener进行启动的,那么我们首先来看看web.xml中配置的ContextLoaderListener的相关class和interface。
从图中我们可以看到,ContextLoaderListener是从ContextLoader继承的,并且实现了ServletContextListener接口。ServletContextListener是在Servlet的javax.servlet包中定义的接口。该接口中定义了两个方法,其中,contextInitialized方法是在所有的servlet和filters初始化之前进行调用的,contextDestroyed方法是在所有的servlet和filters都销毁后才进行调用的。由于ContextLoaderListener实现了接口,所以也实现了接口中定义的两个方法:
public
void
contextInitialized ( ServletContextEvent sce );
public
void
contextDestroyed ( ServletContextEvent sce );
在contextInitialized方法中,实际上调用了其父类ContextLoader的实现来创建WebApplicationContext,以及进行容器的初始化工作。
2、ContextLoader的主要工作
从ContextLoader调用执行的初始化代码中可以看到主要包括两个部分,第一,WebApplicationContext的构建。第二,WebApplicationContext的配置和初始化。
接下来我们围绕这两部分进行讨论。