当我使用 DispatcherServlet 时,我得到一个 java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered? 当我使用 DelegatingFilterProxy 过滤器时出错。因此,我删除了 DispatcherServlet,现在改用 ContextLoaderListener,我的 Spring 应用程序加载正常。但是,我对一个非常重要的 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,没问题(除了我的过滤器再次无用)。我怎样才能让这个 bean 从 ContextLoaderListener 中正确加载?
干杯
尼克
最佳答案
您需要 ContextLoaderListener 和 DispatcherServlet - 错误消息没有告诉您删除 servlet。
为了阐明 Spring 在这里做什么,DispatcherServlet 创建了它自己的 ApplicationContext(通常使用 xxx-servlet.xml),但是任何您在 web.xml 中配置的 Spring 过滤器无权访问 servlet 的 ApplicationContext。
ContextLoaderListener 创建第二个 ApplicationContext(与整个 webapp 关联),并将自身链接到 servlet 的 ApplicationContext,允许过滤器和 servlet通过 Spring 进行通信。
关于java - DefaultAnnotationHandlerMapping 通过 ContextLoaderListener 而不是 Spring 3 上的 DispatcherServlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1464881/