在Spring 3上通过ContextLoaderListener而不是DispatcherServlet进行DefaultAnnotationHandlerMapping

nik*_*ers 7 java spring

当我使用DispatcherServlet时,我得到一个java.lang.IllegalStateException:找不到WebApplicationContext:没有注册ContextLoaderListener? 我使用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>
Run Code Online (Sandbox Code Playgroud)

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

干杯

ska*_*man 22

同时需要ContextLoaderListener DispatcherServlet-错误消息没有告诉你删除的servlet.

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

ContextLoaderListener创建第二ApplicationContext(与整个Web应用程序相关联),并用servlet的链接本身ApplicationContext,使过滤器和servlet来通过Spring沟通.

  • ContextLoaderListener创建的appcontext是servlet的appcontext的父上下文.这意味着ContextLoaderListener的appcontext中的bean对servlet的appcontext中的bean自动可见(但不是相反).所以共享的任何内容都应该在父级中声明. (2认同)