问题描述
当我使用 DispatcherServlet 时,我得到一个 java.lang.IllegalStateException:找不到WebApplicationContext:没有注册ContextLoaderListener?使用 DelegatingFilterProxy 过滤器时出现
错误。因此我删除了 DispatcherServlet ,现在我使用了 ContextLoaderListener ,我的Spring应用程序加载正常。但是,我有一个非常重要的bean的问题:
When I use DispatcherServlet, I get a java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered? error when I use a DelegatingFilterProxy filter. Therefore I've removed the DispatcherServlet and now I use a ContextLoaderListener instead, and my Spring application loads fine. However, I have a problem with one VERY important bean:
<context:component-scan base-package="com.mydomain"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<ref bean="openSessionInViewInterceptor" />
</list>
</property>
</bean>
这个bean不再有效,我的@Controller都不再是URL映射了。如果我切换回使用 DispatcherServlet ,没问题(除了我的过滤器再没用)。如何从 ContextLoaderListener 中正确加载此bean?
This bean no longer works, none of my @Controller's are URL mapped anymore. If I switch back to using DispatcherServlet, no problem (except that my filter is useless again). How can I get this bean to load correctly from within a ContextLoaderListener?
干杯
Nik
推荐答案
您需要 ContextLoaderListener
和 DispatcherServlet
- 错误消息没有告诉您删除servlet。
You need both the ContextLoaderListener
and the DispatcherServlet
- the error message didn't tell you to remove the servlet.
澄清Spring在这里做什么, DispatcherServlet
创建自己的 ApplicationContext
(通常使用 xxx-servlet .xml
),但是您在web.xml中配置的任何Spring Filters都无权访问servlet的 ApplicationContext
。
To clarify what Spring is doing here, the DispatcherServlet
creates its own ApplicationContext
(typically using xxx-servlet.xml
), but any Spring Filters that you configure in web.xml don't have access to the servlet's ApplicationContext
.
ContextLoaderListener
创建第二个 ApplicationContext
(与整个webapp),并将自己与servlet的 ApplicationContext
链接,允许过滤器和servlet通过Spring进行通信。
The ContextLoaderListener
creates a second ApplicationContext
(associated with the whole webapp), and links itself with the servlet's ApplicationContext
, allowing filters and servlets to communicate via Spring.
这篇关于DefaultAnnotationHandlerMapping通过ContextLoaderListener而不是Spring 3上的DispatcherServlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!