开发者

Inrecept requests to specific beans

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 b开发者_如何学Pythoneans

 <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!


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.


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;
  }
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜