问题描述
使用Spring 3.0.2.RELEASE。我在com.myCompany包中有2个控制器。控制器通过组件扫描激活
Using Spring 3.0.2.RELEASE. I'm having 2 Controllers in package com.myCompany. The Controllers are activated via Component-scan
<context:component-scan base-package="com.myCompany" />
然后我通过
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<ref bean="myInterceptor"/>
</list>
</property>
</bean>
如何将拦截器仅绑定到一个特定的控制器或控制器内的某些方法?
背景:我想检查它包含某些参数的URL
How can i bind the interceptor to only one specific Controller or to only certain methods inside a Controller?Background: I want to inspect the URL that it contains certain parameters
推荐答案
当您将拦截器注入 HandlerMapping
bean时,这些拦截器将应用于由 HandlerMapping映射的每个处理程序
。这在预注释时很好,因为你只需要配置多个 HandlerMapping
bean。但是,使用注释时,我们往往会有一个 DefaultAnnotationHandlerMapping
来映射所有内容,因此这个模型不起作用。
When you inject interceptors into a HandlerMapping
bean, those interceptors apply to every handler mapped by that HandlerMapping
. That was fine in the pre-annotation days, since you'd just have configure multiple HandlerMapping
beans. However, with annotations, we tend to have a single DefaultAnnotationHandlerMapping
that maps everything, so this model doesn't work.
解决方案是使用< mvc:interceptors>
,您可以显式地将路径映射到拦截器bean。请参阅,这个例子:
The solution is to use <mvc:interceptors>
, where you explicitly map paths to interceptor beans. See the docs, and this example:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/secure/*"/>
<bean class="org.example.SecurityInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
这篇关于将Spring HandlerInterceptor仅绑定到一个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!