现在的位置: 首页 > 综合 > 正文

No WebApplicationContext found: no ContextLoaderListener registered?报错解决

2018年05月24日 ⁄ 综合 ⁄ 共 1991字 ⁄ 字号 评论关闭

    今天跑了下新搭的一个SSI框架。报如下错误:

严重: Exception sending context initialized event to listener instance of class com.j.chat.upgrade.web.core.SystemLaunchListener
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
	at org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:84)
	at com.j.chat.upgrade.web.core.SystemLaunchListener.contextInitialized(SystemLaunchListener.java:27)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4779)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5273)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1566)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1556)
	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
	at java.util.concurrent.FutureTask.run(FutureTask.java:138)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
	at java.lang.Thread.run(Thread.java:662)
2014-7-14 19:49:10 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext

    很纳闷,我在web.xml里面已经声明了ContextLoaderListener 这个监听器。我的web.xml配置文件如下:

<listener>
	<listener-class>com.j.chat.upgrade.web.core.SystemLaunchListener</listener-class>
</listener>
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>	
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:spring-config.xml</param-value>
</context-param>

    分析了下,我是在SystemLaunchListener里面执行的时候,发现ContextLoaderListener 这个监听器没有注册。难道是因为这两个listener是先后执行的?调了下这个两个监听器的顺序,把spring的监听器放在了最前面。重启tomcat,就没有这个报错了。

    网上搜了下web.xml文件里listener的执行顺序。下面是一个可信的说法:

    在java web规范中说明.按照listner,filter,servlet的顺序初始化. 
    其中listener是按照在web.xml中的申明顺序执行的.不存在并发的的情况.



抱歉!评论已关闭.