配置ContextLoaderListener 8.1.2越狱后无法访问问

21 条评论分享收藏感谢收起赞同 添加评论分享收藏感谢收起博客分类:
一、Spring与WEB容器整合
web项目中,Spring启动是在web.xml配置监听器,如下所示:
&!-- 配置Spring上下文监听器 --&
&listener&
&listener-class&org.springframework.web.context.ContextLoaderListener&/listener-class&
&/listener&
可以看看ContextLoaderListener类,它实现了Tomcat容器的ServletContextListener接口,所以它与普通的Servlet监听是一样的。同样是重写到两个方法:contextInitialized()方法在web容器初始化时执行,contextDestroyed()方法在容器销毁时执行。
WEB容器启动时会触发初始化事件,ContextLoaderListener监听到这个事件,其contextInitialized()方法会被调用,在这个方法中Spring会初始化一个根上下文,即WebApplicationContext。这是一个接口,其实际默认实现类是XmlWebApplicationContext。这个就是Spring IOC的容器,其对应bean定义的配置信息由web.xml中的context-param来指定
&!-- 配置Spring配置文件路径 --&
&context-param&
&param-name&contextConfigLocation&/param-name&
&param-value&classpath*:applicationContext.xmlclasspath*:applicationContext-shiro.xml
&/param-value&
&/context-param&
在Spring IOC 容器启动初始化完毕之后,会将其储存到ServletContext中。形式如下:servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, WebApplicationContext context);
注:以前我们写Servlet程序的时候,如果在Tomcat容器启动的时候需要绑定一些参数或者做一些全局处理,那么就需要实现ServletContextListener接口,在contextInitialized方法中编写必要的代码。然后在web.xml添加配置listener
在ContextLoaderListener类中,只是实现了ServletContextListener提供的到两个方法,Spring启动主要的逻辑在父类ContextLoader的方法initWebApplicationContext实现。ContextLoaderListener的作用就是启动web容器时自动装配ApplicationContext的配置信息。更细化一点讲,Spring的启动过程其实就是Spring IOC容器的启动过程。
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
* Initialize the root web application context.
public void contextInitialized(ServletContextEvent event) {
initWebApplicationContext(event.getServletContext());
* Close the root web application context.
public void contextDestroyed(ServletContextEvent event) {
closeWebApplicationContext(event.getServletContext());
ContextCleanupListener.cleanupAttributes(event.getServletContext());
二、ContextLoader剖析
从上一部分ContextLoaderListener类可以得知,ContextLoader实际执行Spring容器的初始化,Spring整个的配置工作都是在ContextLoader完成的,这里参数ServletContextEvent由web容器提供,不做说明。ContextLoaderListener很好理解,所以我们主要看ContextLoader类。用Maven引入Spring的源码,打开ContextLoader类,类注释的第一行就是
* Performs the actual initialization work for the root application context.
用google翻译:实际执行根应用上下文的初始化工作。这里的根应用上下文就是上文所写的WebApplicationContext。我们先看看ContextLoader的时序图 ContextLoader类initWebApplicationContext()方法
* Initialize Spring's web application context for the given servlet context,
* using the application context provided at construction time, or creating a new one
* according to the "{@link #CONTEXT_CLASS_PARAM contextClass}" and
* "{@link #CONFIG_LOCATION_PARAM contextConfigLocation}" context-params.
* @param servletContext current servlet context
* @return the new WebApplicationContext
* @see #ContextLoader(WebApplicationContext)
* @see #CONTEXT_CLASS_PARAM
* @see #CONFIG_LOCATION_PARAM
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
//判断ServletContext是否已经存在WebApplication,如果存在则抛出异常
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");
long startTime = System.currentTimeMillis();
// Store context in local instance variable, to guarantee that
// it is available on ServletContext shutdown.
if (this.context == null) {
//创建WebApplicationContext(下文有说明)
this.context = createWebApplicationContext(servletContext);
if (this.context instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.
if (!cwac.isActive()) {
// The context has not yet been refreshed -& provide services such as
// setting the parent context, setting the application context id, etc
if (cwac.getParent() == null) {
// 得到根上下文的父上下文,然后设置到根上下文 。一般的web项目parent为空
ApplicationContext parent = loadParentContext(servletContext);
cwac.setParent(parent);
//从web.xml加载参数,初始化跟上下文WebApplicationContext,创建bean工厂和bean对象。
//这个过程比较麻烦,下一篇文章专门分析
configureAndRefreshWebApplicationContext(cwac, servletContext);
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) {
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);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
catch (Error err) {
logger.error("Context initialization failed", err);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);
在这个方法中ServletContext是由web容器监听器(ContextLoaderListener)提供。首先判断servlectContext中是否已经存在根上下文,如果存在,则抛出异常;否则通过createWebApplicationContext方法创建新的根上下文。然后通过loadParentContext()方法为其设置父上下文。再通过configureAndRefreshWebApplicationContext为根上下文构建bean工厂和bean对象。 最后把上下文存入servletContext,并且存入currentContextPerThread。至此初始化过程完毕,接下来可以获取WebApplicationContext,进而用getBean("bean name")得到bean。
createWebApplicationContext()方法用来创建根上下文:
protected WebApplicationContext createWebApplicationContext(ServletContext sc) {
//从web.xml配置的contextClass参数中获取上下文类名,如果contextClass为空,则使用默认的。
//下文有说明
Class&?& contextClass = determineContextClass(sc);
//根上下文必须是ConfigurableWebApplicationContext的子类,否则抛出异常
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
"] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
//BeanUtils.instantiateClass工具方法,根据类名创建类
return (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
determineContextClass()方法返回根上下文的类名
protected Class&?& determineContextClass(ServletContext servletContext) {
//从web.xml获得参数contextClass,在一般的web项目中,此参数为null
String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);
if (contextClassName != null) {
return ClassUtils.forName(contextClassName, ClassUtils.getDefaultClassLoader());
catch (ClassNotFoundException ex) {
throw new ApplicationContextException(
"Failed to load custom context class [" + contextClassName + "]", ex);
//获得根上下文WebApplicationContext的默认实现类的类名,defaultStrategies是Properties类型,
//在CotnextLoader类开头static语句块中初始化
contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());
return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());
catch (ClassNotFoundException ex) {
throw new ApplicationContextException(
"Failed to load default context class [" + contextClassName + "]", ex);
WebApplicationContext默认实现类的类名获取
//获取当前包下面的ContextLoader.properties文件,文件内容是:
//org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext
ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class);
defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
catch (IOException ex) {
throw new IllegalStateException("Could not load 'ContextLoader.properties': " + ex.getMessage());
浏览: 71223 次
来自: 杭州
如果一个service的方法中,涉及到5个数据的事务,岂不是要 ...
我想问问怎么把共享的css.js一起连同tab加载起来
云上太阳 写道herman_liu76 写道请教:完全不用时, ...
id.alex 写道herman_liu76 写道请教:完全不 ...
herman_liu76 写道请教:完全不用时,为啥不在dof ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'SSH整合后,web.xml配置的问题导致无法读取页面一直404_百度知道
SSH整合后,web.xml配置的问题导致无法读取页面一直404
&!--指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找--&&context-param&&param-name&contextConfigLocation&/param-...
&!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 --& &context-param&
&param-name&contextConfigLocation&/param-name&
&param-value&classpath:applicationContext.xml&/param-value& &/context-param& &!-- 对Spring容器进行实例化 --& &listener&
&listener-class&org.springframework.web.context.ContextLoaderListener&/listener-class& &/listener& 在web.xml中去掉这段配置后,项目可正常启动。引入的包没有少,因为是又另一个成功SSH整合的项目中全部不漏导入的包
答题抽奖
首次认真答题后
即可获得3次抽奖机会,100%中奖。
一年单恋一天知道合伙人
一年单恋一天
采纳数:38
获赞数:47
地址错了你把 classpath:applicationContext.xml换成/applicationContext.xml不行的话就把这个xml文件建在 web-inf目录下然后 classpath:applicationContext.xml换成/web-inf/applicationContext.xml
尝试了一下换你说的写法,是不行的……
把两个标签换一下位置呢?&listener&&/listener&这个标签放到上面试试看。再不行-- 我也不知道了。
已经自己解决了。我上面的写法是没有错的,也是默认的一种写法主要问题是出在applicationContext.xml里面存在一些配置错误的问题,我在action里使用了一个对象属性变量(用spring注解的方式),却没有在applicationContext.xml里用&bean&标签声明,所以web.xml配置错误,这种错误没有报错,还是要再谨慎一些才行。分就给你了。辛苦你了。
为你推荐:
其他类似问题
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。Spring org.springframework.web.context.ContextLoaderListener
public class&ContextLoaderListener
extends&implements&
作用:在启动容器时,自动装配Spring&的配置信息。
因为它实现了这个接口,在配置这个监听器,启动容器时,就会默认执行它实现的方法。在中关联了这个类,所以整个加载配置过程由来完成。
看看ContextLoader的说明
第一段说明可以由&和生成。如果查看的,可以看到它也关联了这个类而且它继承了类
第二段,创建的是&这样一个类,它实现的接口是
这样一来中的所有都由这个类来创建
第三段讲如何部署的文件,如果在中不写任何参数配置信息,默认的路径是,在目录下创建的文件的名称必须是。如果是要自定义文件名可以在里加入这个参数:
在里指定相应的文件名,如果有多个文件,可以写在一起并一号分隔。上面的采用通配符,比如这那个目录下有,,等文件,都会一同被载入。
-----------------------------------------------------------------------------------------
例子,在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&/param-value&& &/context-param&
阅读(...) 评论()关于jeesite部署无法找到ContextLoaderListener的问题?
使用jeesite,常会遇到Error configuring application
listener of class com.thinkgem.jeesite.modules.sys.listener.WebContextListener的问题。
一般的做法通常都是 删除部署文件,清空tomcat下的 work文件夹
(catalina文件夹), project clean ,run as maven clean,然后重新部署。
有的时候这样做不能起作用。如果你打开部署文件夹,点开 java代码路径,会发现没有编译生成的.class文件,这样的话就不是tomcat部署的问题,而是
javacompiler 的问题,解决办法参考我的另一篇文章
解决部署jeesite项目报错 com.thinkgem.jeesite.modules.sys.listener.WebContextListener
JeeSite的开发环境部署
-- 异常:java.lang.ClassNotFoundException: javax.servlet.ServletContainerInitializer
解决 jeesite 项目部署到linux 服务器上 ckfinder 无法上传图片的问题
jeesite在idea中部署的一个关键问题
解决ssm环境部署出现listener的问题 classnotfoundexception
没有更多推荐了,

我要回帖

更多关于 系统更新后出现无法访问移动网络 的文章

 

随机推荐