views:

20

answers:

3

I'm developing webapp using spring 3 mvc and using annotation-based controllers. I want to define an interceptor, that will intercept requests to specific beans not for all. How I can do that?

Now i use following config, but it intercept requests to all beans

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

<bean id="sessionInterceptor" class="app.interceptors.SessionCheckInterceptor"/>

Thanks!

+1  A: 

In Spring 3 you can bind interceptors to specific URLs using mvc namespace:

<mvc:interceptors>
    <mvc:interceptor>
        <bean class="app.interceptors.SessionCheckInterceptor" />
        <mvc:mapping path = "/somePath/**" />
    </mvc:interceptor>
</mvc:interceptors>

For more complex cases you may also use AOP to intercept controller method calls.

axtavt
+1  A: 

I think the best choice for intercepting calls to your bean is using Spring AOP.

@Aspect
public class AroundExample {

  @Around("com.xyz.myapp.SystemArchitecture.businessService()")
  public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
    // start stopwatch
    Object retVal = pjp.proceed();
    // stop stopwatch
    return retVal;
  }
}
amra
A: 

I have found a solution using springplugins project (http://code.google.com/p/springplugins/) It define two own mappers:

  • SelectedAnnotationHandlerMapping handle only defined url's

  • IgnoreSelectedAnnotationHandlerMapping handle all urs's except defined

If you know a better way to solve this problem, you a welcome

glebreutov