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 ...
随机推荐
- 单片机中不带字库LCD液晶屏显示少量汉字
单片机中不带字库LCD液晶屏如何显示少量汉字,一般显示汉字的方法有1.使用带字库的LCD屏,2.通过SD 卡或者外挂spi flash存中文字库,3.直接将需要的汉字取模存入mcu的flash中. 第 ...
- 生产环境rails console spring自动启动的问题
在生产环境执行rails console没反应无法进入控制台,或者执行rails console的时候spring自动启动,导致所有的类名都无法识别,报错:NameError: uninitializ ...
- 如何通过审计安全事件日志检测密码喷洒(Password Spraying)攻击
许多渗透测试人员和攻击者通常都会使用一种被称为“密码喷洒(Password Spraying)”的技术来进行测试和攻击.对密码进行喷洒式的攻击,这个叫法很形象,因为它属于自动化密码猜测的一种.这种针对 ...
- Linux下C语言编译的问题
在Linux下编程发现一个诡异的现象,就是在链接一个静态库的时候总是报错,类似下面这样的错误: (.text+0x13): undefined reference to `func' 关于undefi ...
- Python enumerate()方法
for循环中如果要获取当前元素的索引值,一个方法是定义一个计数器,每次取值的时候将这个值加一,如果是列表的话可以用index()函数,而python中有一个比较简洁的方法而已直接获得索引值,并可以方便 ...
- golang 错误处理与异常
原文地址 golang 中的错误处理的哲学和 C 语言一样,函数通过返回错误类型(error)或者 bool 类型(不需要区分多种错误状态时)表明函数的执行结果,调用检查返回的错误类型值是否是 nil ...
- centos系统误删libc.so.6
前段时间遇到开发人员更新glibc版本,把/usr/lib64/libc-2.12.so & libc.so.6 -> libc-2.12.so 这个软连接更改之后导致报错: ls: e ...
- Linux入门第五天——shell脚本入门(下)基础语法之循环
一.循环 1.不定循环 有两种形式: while [ condition ] <==中括号内的状态就是判断式 do <==do 是循环的开始! 程序段落 done <==done 是 ...
- Hello World 和 模块分解
Hello World 和 模块分解 在命令行中编译运行HelloWorld public class HelloWorld { public static void main(String[] ar ...
- Android开发——LinearLayout和RelativeLayout的性能对比
0. 前言 我们都知道新建一个Android项目自动生成的Xml布局文件的根节点默认是RelativeLayout,这不是IDE默认设置,而是由android-sdk\tools\templates\ ...