DefaultAnnotationHandlerMapping通过ContextLoaderListener而不是Spring 3上的DispatcherServlet

当我使用DispatcherServlet时 ,我得到一个java.lang.IllegalStateException:找不到WebApplicationContext:没有注册ContextLoaderListener? 我使用DelegatingFilterProxyfilter时出错。 因此我删除了DispatcherServlet ,现在我使用ContextLoaderListener ,我的Spring应用程序加载正常。 但是,我有一个非常重要的bean的问题:

        

这个bean不再有效,我的@Controller都不再是URL映射了。 如果我切换回使用DispatcherServlet ,没问题(除了我的filter再次没用)。 如何从ContextLoaderListener中正确加载此bean?

干杯

您需要ContextLoaderListener DispatcherServlet – 错误消息没有告诉您删除servlet。

为了阐明Spring在这里做了什么, DispatcherServlet创建了自己的ApplicationContext (通常使用xxx-servlet.xml ),但是在web.xml中配置的任何Spring Filter都无法访问servlet的ApplicationContext

ContextLoaderListener创建第二个ApplicationContext (与整个webapp相关联),并将自身与servlet的ApplicationContext链接,允许filter和servlet通过Spring进行通信。

Spring MVC基本应用程序的web.xml虽然没有ContextLoaderListener? 请参阅https://src.springframework.org/svn/spring-samples/mvc-basic/trunk/src/main/webapp/WEB-INF/web.xml 。

我问的原因是因为我的申请中出现以下错误:

ERROR [[Spring MVC Dispatcher Servlet]] servlet的Servlet.service()Spring MVC Dispatcher Servlet抛出exceptionjava.lang.IllegalStateException:找不到WebApplicationContext:没有注册ContextLoaderListener?

我没有ContextLoaderListener(这可能会导致问题)。

有关为什么MVC基本应用程序可以工作的任何想法?