gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:01:21 25 4
gpt4 key购买 nike

当我使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com