contextloaderlistener
http://blog.csdn.net/c5153000/article/details/6234207
作用:在启动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
- lt;/param-value>
- </context-param>
----------------------------------------------------------------------------------------------------------------------------------------------
spring在web中的启动是由ContextLoaderListener开始的。ContextLoaderListener实现了ServlContextListener接口,并继承了ContextLoader类。
笔记如下:
1.ServletContextListener 接口有两个方法:contextInitialized,contextDestroyed。
在服务器加载web应用的时候,这个Listener将被调用。
spring在contextInitialized中进行spring容器的初始化。
- <textarea cols="50" rows="15" name="code" class="c-sharp">this.contextLoader.initWebApplicationContext(event.getServletContext());
- </textarea><span style="color: #800000;"> </span>
2.ContextLoader的initWebApplicationContext方法:
首先判断web应用中是否有spring容器被加载过,如下
- <textarea cols="50" rows="15" name="code" class="java">if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null)
- </textarea><span style="color: #800000;"> </span>
如果有被加载过,将抛出异常。提示是否放置了多个ContextLoaderListener。
3.创建WebApplicationContext前,将先处理父上下文(对于ERA工程而言有用可能)。
普通的web项目,是没有父上下文的。
ApplicationContext parent = loadParentContext(servletContext);
下面这句是创建WebApplicationContext的主过程。
创建完的context置于servletContext中,这样Spring容器的宿主就是ServletContext(application Scope)。
4.createWebApplicationContext方法:
首先进行ApplicationContext Class的诊断:如果指定了ContextClass但没有实现ConfigurableWebApplicationContext的,将抛出异常。
默认将根据ContextLoader.properties中指定class的实例
即:org.springframework.web.context.support.XmlWebApplicationContext
之后根据Servlet的版本2.4之前及2.5的不同,获取web的DisplayName。
设置ConfigurableWebApplicationContext的id。
最后代码进入实质性的处理
- wac.setParent(parent);
- wac.setServletContext(sc);
- //获取configLocation的配置
- wac.setConfigLocation(sc.getInitParameter(CONFIG_LOCATION_PARAM));
- customizeContext(sc, wac);// 用于自定义ContextClass时,扩充自定义Context
- wac.refresh();// 完成context构造过程。
contextloaderlistener的更多相关文章
- Tomcat启动报错org.springframework.web.context.ContextLoaderListener类配置错误——SHH框架
SHH框架工程,Tomcat启动报错org.springframework.web.context.ContextLoaderListener类配置错误 1.查看配置文件web.xml中是否配置.or ...
- DispatcherServlet 和 ContextLoaderListener 的关系,到底用哪个?
我们先看下这两个东东的配置方法: 对于contextConfigLocation参数,有2个地方可以配置: 1)context-param 是全局性配置 2)servlet下的init-param 是 ...
- maven 项目启动tomcat报错 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
maven项目启动tomcat报错: java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderLi ...
- Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException:
严重: Exception sending context initialized event to listener instance of class org.springframework.we ...
- IDEA +maven+ ContextLoaderListener not find
tomcat 启动失败:SEVERE: Context [] startup failed due to previous errors 查看pox.xml 有spring-web依赖 查看tomca ...
- java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
参考: http://www.cnblogs.com/sunxucool/archive/2013/06/07/3124380.html ---------------------------&g ...
- 真正解决问题:maven eclipse tomcat java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
在使用eclipse进行整合springmvc时经常会碰到这样的异常: java.lang.ClassNotFoundException:org.springframework.web.context ...
- [Spring框架] Spring中的 ContextLoaderListener 实现原理.
前言: 这是关于Spring的第三篇文章, 打算后续还会写入AOP 和Spring 事务管理相关的文章, 这么好的两个周末 都在看code了, 确实是有所收获, 现在就来记录一下. 在上一篇讲解Spr ...
- maven 工程启动找不到 Spring ContextLoaderListener 的解决办法
1.错误: Error configuring application listener of class org.springframework.web.context.ContextLoader ...
- 【转】ContextLoaderListener 和 DispatcherServlet
转载地址: http://www.guoweiwei.com/archives/797 DispatcherServlet介绍 DispatcherServlet是Spring前端控制器的实现,提供S ...
随机推荐
- AngularJS基础概要整理(下)
五.AngularJS Scope(作用域) Scope(作用域)是应用在HTML(视图)和JavaScript(控制器)之间的纽带. Scope是一个对象,有可用的方法和属性. Scope可应用在视 ...
- 2Sigma OA prepare: Longest Chain
DP use HashMap: 根据string的长度sort,然后维护每个string的longest chain,default为1,如果删除某个char生成的string能提供更长的chain, ...
- 1.2 如何在visual studio 中建立C#程序
这一节简单介绍一下怎么在visual studio 2015中建立第一个C#程序,我使用的是2015版的visual studio,不同版本可能有一些差异,不过大体上是相同的,这些信息仅供新手参考,大 ...
- 原生js与css3结合的电风扇
最近学习了css3,就琢磨做些东西练练手,下面是自己写的一个电风扇,使用了原生js中的定时器和css3的一些属性 <!doctype html> <html lang="e ...
- 一个IP能建立的最大连接数是多少?
在探讨这个问题前,我们先假设一种经典的连接模型: Client -> Load Balancer-> RealServer Pool 并且我们假设这里使用NAT模式的负载均衡,在这种模式下 ...
- window.event
e = e || window.event是我们在做事件处理时候区分IE和其他浏览器事件对象时常用的写法.但是这行兼容性代码有没有必要出现在所有的事件句柄中呢?标准事件调用方式需要这行代码吗?下边我们 ...
- 关于mysql中int(1)中int后面的数字
mysql在建表的时候int类型后的长度代表什么? 是该列允许存储值的最大宽度吗? 为什么我设置成int(1), 也一样能存10,100,1000呢. 当时我虽然知道int(1),这个长度1并不代表 ...
- 【转】SVN库的迁移
转载地址:http://blog.csdn.net/windone0109/article/details/2841294 SVN服务器由于硬盘空间不足,需要将其迁移到另外一台机器上,并且更换Repo ...
- Viking Village维京村落demo中的地面积水效果
效果如下: 似乎是通过高光贴图实现的,查找后发现具体在这: 它使用了基于Standard的TerrainSurface自定义Shader,关闭该帖图后效果消失: 这个TerrainSurfaceSha ...
- Python学习笔记-常用模块
1.python模块 如果你退出 Python 解释器并重新进入,你做的任何定义(变量和方法)都会丢失.因此,如果你想要编写一些更大的程序,为准备解释器输入使用一个文本编辑器会更好,并以那个文件替代作 ...