代码之家  ›  专栏  ›  技术社区  ›  glebreutov

接收对特定bean的请求

  •  0
  • glebreutov  · 技术社区  · 15 年前

    我正在使用spring3mvc和基于注释的控制器开发webapp。我想定义一个拦截器,它将截获对特定bean的请求,而不是全部。我怎么能做到?

    现在我使用下面的配置,但它拦截对所有bean的请求

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

    谢谢!

    3 回复  |  直到 15 年前
        1
  •  1
  •   axtavt    15 年前

    在Spring3中,您可以使用 mvc namespace

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

    对于更复杂的情况,还可以使用AOP来拦截控制器方法调用。

        2
  •  1
  •   amra    15 年前

    我认为拦截bean调用的最佳选择是使用 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;
      }
    }
    
        3
  •  0
  •   glebreutov    15 年前

    http://code.google.com/p/springplugins/ ) 它定义了两个自己的映射器:

    • SelectedAnnotationHandlerMapping句柄仅定义url的

    • 忽略SelectedAnnotationHandlerMapping处理除已定义的UR以外的所有UR