views:

105

answers:

1

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" />

then I'm having a interceptor bind to the 2 controllers via

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
   <property name="interceptors">
     <list>
       <ref bean="myInterceptor"/>
     </list>
   </property>
 </bean>

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

Docu Link

+4  A: 

When you inject interceptors into a HamdlerMapping bean, those interceptors apply to every handler mapped by that HandlerMapping. That was fine in the pre-annotation days, since you'd just have confgiure multiple HandlerMapping beans. However, with annotations, we tend to have a single DefaultAnnotationHandlerMapping mapping everything, so this model doesn't work.

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>
        <mapping path="/secure/*"/>
        <bean class="org.example.SecurityInterceptor" />
    </mvc:interceptor>
</mvc:interceptors>
skaffman
@skaffman Good job
Arthur Ronald F D Garcia
Thanks. But i don't get it. Can I add the snippet into the File containing "<context:component-scan base-package="com.myCompany" /> ? Then I get "org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:interceptors'."I added xmlns:mvc="http://www.springframework.org/schema/mvc"
Martin Dürrmeier
@Martin: See the fixed link to the docs. You need add the `schemaLocation` in addition to the namespace.
skaffman
Thanks skaffman. It works! I've overseen the link.. What Spring sources do you recommend for getting more involved?
Martin Dürrmeier
@Martin: Hard to say. I've been using Spring since 1.2.8, so I've kind of grown with it. I still think the reference manual is excellent, though.
skaffman
alright! another laud for the manual.I'll have a deeper look
Martin Dürrmeier