创建ApplicationContext有两种方式,最常用的一种方式就是以声明的方式创建,如使用ContextLoader,这也是绝大多数web应用采用的一种方式。另一种方式就是用ApplicationContext的借口实现类以编程的方式实现。
在这里我主要讲一下声明方式的实现及其原理:
首先讲一下ContextLoader接口,它有两个实现:ContextLoaderListener 和ContextLoaderServlet.其中常用的是ContextLoaderListener.从spring 文档上可以查到,他们二者实现的功能基本一样,只是ContextLoaderListener不能在与Servlet2.2兼容的web容器中使用。另外,因为ContextLoaderLitener是一个servlet Listener,因此,它是在servlet context建立后立即执行,也就以为这servlet已建立,spring的ApplicationContext就得到了初始化,并且能够相应第一个请求,所以首选ContextLoaderListener.
下面是一个项目的web.xml文件的部分代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Baselib Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/spring-sup-middelbeans.xml
/WEB-INF/classes/spring-ueaac-action.xml
/WEB-INF/classes/spring-ueaac-beans.xml
/WEB-INF/classes/spring-ueaac-cm.xml
/WEB-INF/classes/spring-ueaac-hibernate.xml
/WEB-INF/classes/spring-ueaac-resource.xml
/WEB-INF/classes/spring-ueaac-sso.xml
/WEB-INF/classes/com/javaeye/jert/application_context.xml
</param-value>
</context-param>
…………………………
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
…………………
这里主要是配置了spring的监听器ContextLoaderListener,它检查contextConfigLocation 这个参数。如果它不存在的话,它将用/WEB-INF/applicationContext.xml 作为默认的配置文件。如果contextConfigLocation存在的话,它将根据该参数的值查找配置文件的位置,来一一读取spring参数。
在这里要注意一个问题(个人观点,不知对错),关于参数名称contextConfigLocation ,我个人认为是固定的,不能随意改动,因为我曾经做过试验,发现回出错,这时候监听器找不到contextConfigLocation参数,则用默认值,即它会去查找appilcationContext.xml文件,这时当然会抱错的拉。为了更有说服力,我查阅了一下spring源码,下面是ContextLoader.java的部分源码:
package org.springframework.web.context;
publicclass ContextLoader ...{
publicstaticfinal String CONTEXT_CLASS_PARAM = "contextClass";

/** *//**
*Nameofservletcontextparameterthatcanspecifytheconfiglocation
*fortherootcontext,fallingbacktotheimplementation'sdefault
*otherwise.
*
*/
publicstaticfinal String CONFIG_LOCATION_PARAM = "contextConfigLocation";
publicstaticfinal String LOCATOR_FACTORY_SELECTOR_PARAM = "locatorFactorySelector";
publicstaticfinal String LOCATOR_FACTORY_KEY_PARAM = "parentContextKey";
privatestaticfinal String DEFAULT_STRATEGIES_PATH = "ContextLoader.properties";
privatestaticfinal Properties defaultStrategies;

static ...{
// Load default strategy implementations from properties file.
// This is currently strictly internal and not meant to be customized
// by application developers.
try ...{
ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class);
defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
}
catch (IOException ex) ...{
thrownew IllegalStateException("Could not load 'ContextLoader.properties': " + ex.getMessage());
}
}
…………………………………………………
其中有一个属性
publicstaticfinal String CONFIG_LOCATION_PARAM = "contextConfigLocation";
说明了contextConfigLocation是被spring固定的,专门用来查 找配置文件位置的
本文介绍 Spring 应用上下文 ApplicationContext 的两种创建方式,重点讲解了通过 ContextLoaderListener 进行声明式配置的方法,包括其工作原理及 web.xml 中的具体配置。
695

被折叠的 条评论
为什么被折叠?



