2018年2月22日

------------------blog 1

Spring之ContextLoaderListener的作用



Spring org.springframework.web.context.ContextLoaderListener

ContextLoaderListener

extends ​​Object​​​implements ​​ServletContextListener​

作用:在启动Web容器时,自动装配Spring applicationContext.xml的配置信息。

因为它实现了​​ServletContextListener​​这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。

看看ContextLoader的API说明



 

第一段说明ContextLoader可以由 ContextLoaderListener和ContextLoaderServlet生成。如果查看ContextLoaderServlet的API,可以看到它也关联了ContextLoader这个类而且它继承了​​HttpServlet​​类

第二段,ContextLoader创建的是 XmlWebApplicationContext这样一个类,它实现的接口是WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->

BeanFactory这样一来spring中的所有bean都由这个类来创建

第三段,讲如何部署applicationContext的xml文件,如果在web.xml中不写任何参数配置信息,默认的路径是"/WEB-INF/applicationContext.xml,在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml。如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext-*.xml
</param-value>
</context-param>

在<param-value> </param-value>里指定相应的xml文件名,如果有多个xml文件,可以写在一起并一“,”号分隔。上面的applicationContext-*.xml采用通配符,比如这那个目录下有applicationContext-ibatis-base.xml,applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都会一同被载入。

-----------------------------------------------------------------------------------------

例子,在web.xml文件中加入下面代码

 

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

如果applicationContext.xml文件没有在/WEB-INF/下,或文件名不一致,或存在多个Spring配置文件,在web.xml文件中根据下面代码修改

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml,/WEB-INF/applicationContext.xml,/WEB-INF/classes/applicationContext-*.xml
</param-value>


ContextLoaderListener加载过程

以下描述,将是最详细版spring的ContextLoaderListener加载过程,没有之一。

ContextLoaderListener实现了ServletContextListener接口,ServletContextListener是Java EE标准接口之一,类似tomcat,jetty的java容器启动时便会触发该接口的contextInitialized。

1 顾,java容器启动触发ContextLoaderListener的contextInitialized

2 contextInitialized 方法调用ContextLoader的initWebApplicationContext方法。

3 initWebApplicationContext调用createWebApplicationContext方法

4 createWebApplicationContext 调用determineContextClass方法

5 determineContextClass有如下代码

contextClassName = defaultStrategies
.getProperty(WebApplicationContext.class.getName());

显然是从defaultStrategies中加载的

ContextLoader 类中有段静态代码

static {
try {
ClassPathResource resource = new ClassPathResource(
"ContextLoader.properties", ContextLoader.class);
defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
} catch (IOException ex) {
throw new IllegalStateException(
"Could not load 'ContextLoader.properties': "
+ ex.getMessage());
}

currentContextPerThread = new ConcurrentHashMap(1);
}

ContextLoader.properties 文件内容如下:

org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext

至此,determineContextClass方法返回的是XmlWebApplicationContext

6 回到 initWebApplicationContext 方法,调用configureAndRefreshWebApplicationContext方法

7 configureAndRefreshWebApplicationContext 调用了AbstractApplicationContext的refresh方法

8 refresh 方法调用了obtainFreshBeanFactory

9 obtainFreshBeanFactory 调用了AbstractRefreshableApplicationContext类的refreshBeanFactory方法

10 refreshBeanFactory调用了XmlWebApplicationContext的loadBeanDefinitions

11 loadBeanDefinitions中加载了对应的applicationContext.xml