无法从子WebApplicationContext访问在根ApplicationContext中配置的Spring拦截器 [英] Spring Interceptors configured in root ApplicationContext cannot be reached from child WebApplicationContext

查看:209
本文介绍了无法从子WebApplicationContext访问在根ApplicationContext中配置的Spring拦截器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,我了解一个事实,即通过DispatcherServlet配置的任何Web应用程序上下文都继承了根WebApplicationContext中已经定义的所有bean.不过,我已经在我的根ApplicationContext中配置了一些拦截器

Ok I understand the fact that any web application context configured through DispatcherServlet inherits all the beans already defined in the root WebApplicationContext. Nevertheless i have configured some interceptors in my root ApplicationContext

     <bean   class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
                <property name="order" value="2"/>
                <property name="interceptors">
                    <list>
                        <ref bean="statisticsInterceptor"/>
                        <ref bean="sessionDiagnosticsInterceptor"/>
...

然后在我的web.xml中,我有类似的内容:

Then in my web.xml I have something like this:

<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener> 

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/applicationContext*.xml
        </param-value>
    </context-param>


    <servlet>
       <servlet-name>dispatcher</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
           <init-param>
               <param-name>contextConfigLocation</param-name>
                 <param-value></param-value>
        </init-param>
                 <load-on-startup>1</load-on-startup>
    </servlet>

<servlet>
        <servlet-name>dispatcher-api</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/appOtherContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>


<servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>dispatcher-api</servlet-name>
        <url-pattern>/example/apiOther/*</url-pattern>
    </servlet-mapping>

所以我遇到的问题是,每次我转到包含"/example/apiOther/"的任何URL时,都不会访问/运行拦截器.我的问题是:为什么??handlerMapping的范围是什么?我以为,因为它在根applicationContext中,所以应该应用于所有子上下文.我一直在做一些研究,我认为HandlerMappings仅限于其上下文,即使它是根上下文.是吗?

So the problem I have is that every time I go to any URL that contains "/example/apiOther/", the Interceptors will not be reached/run. My question is: WHY? What is the scope of the handlerMapping? I thought that because it is in the root applicationContext it should apply to all child contexts. I've been doing some research and I think that HandlerMappings are limited to its context even if it is the root Context. Is that right?

推荐答案

通过DefaultAnnotationHandlerMapping配置拦截器时,它们将仅用于同一ApplicationContext中的控制器.您可以(即使不是很好)在根应用程序上下文中声明拦截器,只要引用它的DefaultAnnotationHandlerMapping是在servlet应用程序上下文中声明的,您可以在其中直接或通过注释声明控制器.

As you configure your interceptors through DefaultAnnotationHandlerMapping, they will be used only for controllers in same ApplicationContext. You can (even it is not very nice) declare the interceptors in root application context, provided the DefaultAnnotationHandlerMapping that referes to them is declared in servlet application context where you declare your controllers, either directly or through annotations.

这篇关于无法从子WebApplicationContext访问在根ApplicationContext中配置的Spring拦截器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆