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

    今天跑了下新搭的一个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中的申明顺序运行的.不存在并发的的情况.



原文地址:https://www.cnblogs.com/cynchanpin/p/7002413.html