Spring的初始化:org.springframework.web.context.ContextLoaderListener
在web.xml中配置
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext.xml的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。
ApplicationContext.xml这个配置文件部一般默认放置在。applicationContext的默认的路径是”/WEB-INF/applicationContext.xml。也可以在web.xml中配置该文件的其他位置,配置如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
classpath:applicationContext-security.xml;
</param-value>
</context-param>
以下详解
org.springframework.web.context.ContextLoaderListener类实现了javax.servlet.ServletContextListener接口。ServletContextListener接口能够监听ServletContext对象的生命周期,因为每个web应用仅有一个ServletContext对象,故实际上该接口监听的是整个web应用。
实现该接口的类在web.xml中作为监听器配置后,当web应用启动后,会触发ServletContextEvent事件,调用ContextLoaderListener的contextInitialized(ServletContextEvent sce)方法。
ContextLoaderListener通过一个ContextLoader对象来初始化Spring容器。在contextInitialized方法中调用contextLoader.initWebApplicationContext(event.getServletContext())。
ContextLoader类的initWebApplicationContext方法即可返回一个WebApplicationContext对象context。并通过 servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context)将WebApplicationContext对象放置在ServletContext对象中。initWebApplicationContext方法通过调用以下方法实例化并设置WebApplicationContext对象。
protected WebApplicationContext createWebApplicationContext(ServletContext servletContext, ApplicationContext parent)
throws BeansException
{
Class contextClass = determineContextClass(servletContext);//通过servletContext确定WebApplicationContext的具体类型
if(!(org.springframework.web.context.ConfigurableWebApplicationContext.class).isAssignableFrom(contextClass))
{
throw new ApplicationContextException("Custom context class [" + contextClass.getName() + "] is not of type [" + (org.springframework.web.context.ConfigurableWebApplicationContext.class).getName() + "]");
} else
{
ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext)BeanUtils.instantiateClass(contextClass);
wac.setParent(parent);
wac.setServletContext(servletContext);
wac.setConfigLocation(servletContext.getInitParameter("contextConfigLocation"));//设置配置文件的路径名
customizeContext(servletContext, wac);
wac.refresh();
return wac;
}
}
因此可以通过WebApplicationContextUtils.getWebApplicationContext(ServletContext sc)获取WebApplicationContext。内部实现是通过servletContext对象查找该对象,属性名为WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE。
Spring的初始化:org.springframework.web.context.ContextLoaderListener的更多相关文章
- [key]严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener(Spring配置异常)
详细错误为: 严重: Exception sending context initialized event to listener instance of class org.springframe ...
- 异常将上下文初始化事件发送到类的侦听器实例.[org.springframework.web.context.ContextLoaderListener] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class p
严重: 异常将上下文初始化事件发送到类的侦听器实例.[org.springframework.web.context.ContextLoaderListener]org.springframework ...
- maven创建spring项目之后,启动报错java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
出错情景:maven中已经加载了spring的核心包,但是项目启动时,报错: org.apache.catalina.core.StandardContext listenerStart严重: Err ...
- java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener,环境Spring+Maven
记录一下莫名出现的错误.Spring+Maven+STS. 严重: Error configuring application listener of class org.springframewor ...
- 在eclipse中运行spring web application时的异常: java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis ...
- Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
严重: Exception sending context initialized event to listener instance of class org.springframework.we ...
- org.springframework.web.struts.ContextLoaderPlugIn 和 org.springframework.web.context.ContextLoaderListener
org.springframework.web.struts.ContextLoaderPlugIn 和 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 ...
- 真正解决问题:maven eclipse tomcat java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
在使用eclipse进行整合springmvc时经常会碰到这样的异常: java.lang.ClassNotFoundException:org.springframework.web.context ...
随机推荐
- helpera64开发板下制作ubuntu rootfs镜像(二)
上一篇路径:https://www.cnblogs.com/jizizh/p/10380513.html Helpera64开发板ubuntu剩于工作: 1.背光调节 答:/sys/class/bac ...
- 一个C语言萌新的学习之旅(持续更新中...)
三:计算和类型 一:隐式转换和显示转换 隐式转换:隐式转换指的是自动类型转换,自动向精确,大范围类型转换. 显示转换:例如:(int)3.5*6.0f=18.0f (int)(3.5*6.0f)=21 ...
- 【blockly教程】第一章 Google Blockly教学应用手册
1.1 Google Blockly概述 美国计算科学教育一直认为计算机作为当今各个领域的基础技术工具,有必要让学生尽早的了解和学习计算机工作原理,理解基础的编程思维.在2006年周以真教授提出计算思 ...
- 20155302 2016-2017-2 《Java程序设计》 第1周学习总结
20155302 2016-2017-2 <Java程序设计> 第1周学习总结 教材学习内容总结 浏览全书提出问题 chapter1:怎么保证现在系统在用最高版本的JRE呢?在哪里查看及升 ...
- 20155313 2016-2017-2《Java程序设计》课程总结
20155313 2016-2017-2<Java程序设计>课程总结 目录 一.每周作业链接汇总 二.实验报告链接汇总 三.代码托管链接 四.课堂项目实践 五.课程收获与不足 六.问卷调查 ...
- 20155318 2016-2017-2 《Java程序设计》第一周学习总结
20155318 2016-2017-2 <Java程序设计>第一周学习总结 教材学习内容总结 上周总结 上周学习了一些大学的学习方法,比如知识分为为三种:元知识.软知识和硬知识,讲述技能 ...
- [arc065E]Manhattan Compass[曼哈顿距离和切比雪夫距离转换]
Description 传送门 Solution 题目要求的是曼达顿距离,对于每个点(x,y),我们把它变为(x-y,x+y),就可以转换成求切比雪夫距离了. 证明如下:$max(\left | (x ...
- Error running 'Tomcat 7': Unable to open debugger port (127.0.0.1:9342)
这个只需要把java虚拟机进程结束掉就行了
- dotnet core 开发中遇到的问题
1.发布的时候把视图cshtml文件也编译为dll了,如何控制不编译视图? 编辑功能文件(xx.csproj),加入一个选项: <PropertyGroup> <TargetFram ...
- 数据库表,id自动递增重置,从1开始
问题: 删除数据表的数据后,发现下次插入数据,主键id并没有重置,还是在原来基础上继续增加 解决: ; TRUNCATE table table_name; ; 参考: https://stackov ...