fixcontextlisteneroaderlistener加载的哪个配置参数

他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)1117人阅读
Spring(27)
ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。至于ApplicationContext.xml这个配置文件部署在哪,如何配置多个xml文件,书上都没怎么详细说明。现在的方法就是查看它的API文档。在ContextLoaderListener中关联了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参数:view plaincopy to clipboardprint?&context-param&
&param-name&contextConfigLocation&/param-name&
&param-value&
/WEB-INF/classes/applicationContext-*.xml
&/param-value&
&/context-param& &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等文件,都会一同被载入。由此可见applicationContext.xml的文件位置就可以有两种默认实现:第一种:直接将之放到/WEB-INF下,之在web.xml中声明一个listener、第二种:将之放到classpath下,但是此时要在web.xml中加入&context-param&,用它来指明你的applicationContext.xml的位置以供web容器来加载。按照Struts2 整合spring的官方给出的档案,写成:view plaincopy to clipboardprint?&!-- Context Configuration locations for Spring XML files --& &context-param&
&param-name&contextConfigLocation&/param-name&
&param-value&/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml&/param-value& &/context-param&
文章:31篇
阅读:88716
阅读:14273博客分类:
在SSH项目中我们自动加载spring配置文件需要在web.xml文件中添加一段配置:
&context-param&
&param-name&contextConfigLocation&/param-name&
&param-value&
classpath*:applicationContext-*.xml
&/param-value&
&/context-param&
&listener&
&listener-class&
org.springframework.web.context.ContextLoaderListener
&/listener-class&
&/listener&
=========================================== ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。至于ApplicationContext.xml这个配置文件部署在哪,如何配置多个xml文件,书上都没怎么详细说明。现在的方法就是查看它的API文档。在ContextLoaderListener中关联了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等文件,都会一同被载入
浏览: 141557 次
来自: 北京
[/b][b][i][/i][u][/u]引用
[img][/ ...
排版太乱了吧,看起来很吃力。
写的非常好 很多东西总结的浅显易懂
请问下,安装后,是如何使用的
很不错!写得非常容易理解。
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'最近用到在Tomcat服务器启动时自动加载数据到缓存,这就需要创建一个自定义的缓存监听器并实现ServletContextListener接口,并且在此自定义监听器中需要用到Spring的依赖注入功能.在web.xml文件中监听器配置如下:
org.springframework.web.context.ContextLoaderListener
com.wsjiang.test.listener.CacheListener
上面的配置细细大意为,先配置spring监听器,启动spring,再配置一个缓存监听器,我希望他们是顺序执行的,因为在缓存监听器中需要 spring注入其他对象的实例,我期望在服务器加载缓存监听器前加载Spring的监听器,将其优先实例化。但是实际运行发现他们并不是按照配置的顺序 加载的。
对上面的问题我查询了很多资料,找到了一种解决方案,希望能帮助遇到同类问题的朋友。
思路就是,既然listener的顺序是不固定的,那么我们可以整合两个listener到一个类中,这样就可以让初始化的顺序固定了。我就重写了 org.springframework.web.context.ContextLoaderListener这个类的 contextInitialized方法.大致代码如下:
public class ContextLoaderListenerOverWrite extends ContextLoaderListener {
private IStationService stationS
private IOSCache osC
public void contextInitialized(ServletContextEvent event) {
super.contextInitialized(event);
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
stationService = (IStationService) applicationContext.getBean("stationService");
osCache = (IOSCache) applicationContext.getBean("osCache");
web.xml的配置就由两个listener变为一个:
com.wsjiang.test.listener.ContextLoaderListenerOverWrite
这样就能保证Spring的IOC容器先于自定义的缓存监听器初始化,在缓存监听器加载的时候就能使用依赖注入的实例.
fengbin2005
浏览: 855578 次
有空看看,谢谢
我觉得Lucene版本变化太大!! 对于版本的变动 太巨大了
[img][/img]大大
[*]案说法是否
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'博客分类:
ContextLoaderListener这个监听器是启动根IoC容器并把它载入到Web容器的主要功能模块,也是整个Spring Web应用加载IoC的第一个地方。
为了方便在Web环境中使用IoC容器,Spring为Web应用提供了上下文的扩展接口WebApplicationContext来满足启动过程的需要,结构图如下:
这个常量用于在ServletContext中存取根上下文
String ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";
通过getServlectContext()方法可以得到当前Web容器的Servlet上下文环境,通过这个方法,相当于提供了一个Web容器级别的全局环境。
在启动过程中,Spring 会使用一个默认的WebApplicationContext实现作为IoC容器,就是XmlWebApplicationContext
XmlWebApplicationContext类的结构图
这里是设置默认BeanDefinition的地方,如果不特殊指定其他文件,IoC容器会从/WEB-INF/applicationContext.xml文件
读取BeanDefinition来初始化IoC容器。
public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";
/** Default prefix for building a config location for a namespace */
public static final String DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/";
/** Default suffix for building a config location for a namespace */
public static final String DEFAULT_CONFIG_LOCATION_SUFFIX = ".xml";
* 这里是取得Resource位置的地方,使用默认的配置位/WEB-INF/applicationContext.xml。
protected String[] getDefaultConfigLocations() {
if (getNamespace() != null) {
return new String[] {DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace() + DEFAULT_CONFIG_LOCATION_SUFFIX};
return new String[] {DEFAULT_CONFIG_LOCATION};
* 这个加载过程在容器refresh()时启动
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
// Create a new XmlBeanDefinitionReader for the given BeanFactory.
// 对于XmlWebApplicationContext,当然是使用XmlBeanDefinitionReader来对BeanDefinition信息进行解析。
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
// 因为XmlWebApplicationContext是DefaultResourceLoader的子类,
// 所以这里同样使用DefaultResourceLoader来定位BeanDefinition。
beanDefinitionReader.setResourceLoader(this);
beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
// 钩子,允许子类自定义XmlBeanDefinitionReader的属性配置
initBeanDefinitionReader(beanDefinitionReader);
//这里使用定义好的XmlBeanDefinitionReader来载入BeanDefinition
loadBeanDefinitions(beanDefinitionReader);
* 如果有多个BeanDefinition文件的定义,需逐个载入,都是通过reader来完成的
* 这个初始化过程是由refreshBeanFactory方法来完成的,这里只负责载入BeanDefinition。
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException {
String[] configLocations = getConfigLocations();
if (configLocations != null) {
for (String configLocation : configLocations) {
reader.loadBeanDefinitions(configLocation);
public interface ServletContextListener extends EventListener {
//服务器启动的时候初始化入口
public void contextInitialized ( ServletContextEvent sce );
//服务器销毁的时候销毁入口
public void contextDestroyed ( ServletContextEvent sce );
ContextLoader 类是用来建立WEB环境的根上下文环境
ContextLoaderListener 类的contextInitialized方法实现
* Initialize the root web application context.
public void contextInitialized(ServletContextEvent event) {
//这个方法实现的本意是提供一个占位符方法createContextLoader()给子类机会创建客户化的环境加载,
//但是,后来这个证明不是非常有用的,已经鼓励不再使用了,事实上,子类可以通过重写本方法达到同样的目的
this.contextLoader = createContextLoader();
//实际上是为了使用超类的默认实现
if (this.contextLoader == null) {
this.contextLoader =
//调用超类的加载根共享Web应用程序环境的默认实现
this.contextLoader.initWebApplicationContext(event.getServletContext());
ContextLoader 类的 initWebApplicationContext(ServletContext servletContext)代码
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
//如果已经存在了根共享Web应用程序环境,则抛出异常提示客户
if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
throw new IllegalStateException(
"Cannot initialize context because there is already a root application context present - " +
"check whether you have multiple ContextLoader* definitions in your web.xml!");
Log logger = LogFactory.getLog(ContextLoader.class);
servletContext.log("Initializing Spring root WebApplicationContext");
if (logger.isInfoEnabled()) {
logger.info("Root WebApplicationContext: initialization started");
//记录创建根Web应用程序环境的开始时间
long startTime = System.currentTimeMillis();
//决定根Web应用程序环境是否存在父应用程序环境,一般是返回null
ApplicationContext parent = loadParentContext(servletContext);
//创建根Web应用程序环境,如果父环境存在则引用父环境,通常情况下父环境是不存在的 即parent=null
// 这是一个比较重要的方法,在第三步说明
this.context = createWebApplicationContext(servletContext, parent);
//把创建的根Web应用程序环境保存到Servlet环境中,每个派遣器Servlet加载的子环境会应用这个环境作为父环境
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
//取得线程的类加载器
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
if (ccl == ContextLoader.class.getClassLoader()) {
//如果线程和本类拥有相同的类加载器,则使用静态变量保存即可,因为同一类加载器加载同一份静态变量
currentContext = this.
else if (ccl != null) {
//如果线程和本类拥有不同的类加载器,则使用线程的类加载器作为关键在保存在一个映射对象里,保证析构时能拿到Web应用程序环境进行关闭操作
currentContextPerThread.put(ccl, this.context);
if (logger.isDebugEnabled()) {
logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
if (logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startT
logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
return this.
catch (RuntimeException ex) {
logger.error("Context initialization failed", ex);
//如果产生任何异常,则保存异常对象到Servlet环境里
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
catch (Error err) {
logger.error("Context initialization failed", err);
//如果产生任何错误,则保存错误对象到Servlet环境里
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);
ContextLoader 类的 createWebApplicationContext(ServletContext, ApplicationContext) 代码
protected WebApplicationContext createWebApplicationContext(ServletContext sc, ApplicationContext parent) {
//检测是否有配置的Web应用程序环境类(即存放bean的容器类),如果没有配置,则使用缺省的类XmlWebApplicationContext
// 例如在web.xml中配置
&context-param&
&param-name&contextClass&/param-name&
&param-value&org.springframework.web.context.support.GenericWebApplicationContext&/param-value&
&/context-param&
Class&?& contextClass = determineContextClass(sc);
//如果配置的Web应用程序环境类不是可配置的Web应用程序环境的子类,则抛出异常,停止初始化
//若按上面的配置,这里会抛出异常,因为不是ConfigurableWebApplicationContext的子类
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
"] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
//实例化Web应用程序环境类
ConfigurableWebApplicationContext wac =
(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
//设置Web应用程序环境的ID
if (sc.getMajorVersion() == 2 && sc.getMinorVersion() & 5) {
//如果 Servlet规范 &= 2.4,则使用web.xml里定义的应用程序名字定义Web应用程序名
String servletContextName = sc.getServletContextName();
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
ObjectUtils.getDisplayString(servletContextName));
// 如果Servlet规范是 2.5, 则使用配置的ContextPath定义Web应用程序名
String contextPath = (String) ServletContext.class.getMethod("getContextPath").invoke(sc);
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
ObjectUtils.getDisplayString(contextPath));
catch (Exception ex) {
throw new IllegalStateException("Failed to invoke Servlet 2.5 getContextPath method", ex);
//如果父环境存在,则引用使用父环境
wac.setParent(parent);
//保存Servlet环境
wac.setServletContext(sc);
//设置根环境XML文件存放的位置
//这里如果以下配置容器启动不会报错,但若不配置,则会去寻找/WEB-INF/applicationContext.xml,若该文件不存在就则抛出FileNotFound异常
//&context-param&
&param-name&contextConfigLocation&/param-name&
&param-value&&/param-value&
//&/context-param&
wac.setConfigLocation(sc.getInitParameter(CONFIG_LOCATION_PARAM));
//提供子类可互换Web应用程序环境的机会 占位符方法
customizeContext(sc, wac);
//刷新Web应用程序环境以加载Bean定义,这里才是把我们XML里定义的bean放入容器的时候
wac.refresh();
浏览: 42881 次
来自: 杭州
拿着Spring的源码一抄,翻译软件翻译注释,就叫源码分析了, ...
不错哦 很有意义!
不错 正在运用分析。好好学习!
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'}

我要回帖

更多关于 httplistenercontext 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信