zoukankan      html  css  js  c++  java
  • HandlerInterceptor与MethodInterceptor

    HandlerInterceptor是springMVC项目中的拦截器,它拦截的目标是请求的地址,比MethodInterceptor先执行。实现一个HandlerInterceptor拦截器可以直接实现HandlerInterceptor接口,也可以继承HandlerInterceptorAdapter类。这两种方法殊途同归,其实HandlerInterceptorAdapter也就是声明了HandlerInterceptor接口中所有方法的默认实现,而我们在继承他之后只需要重写必要的方法。下面就是HandlerInterceptorAdapter的代码,可以看到一个方法只是默认返回true,另外两个是空方法

    1. public abstract class HandlerInterceptorAdapter implements HandlerInterceptor {  
    2.   
    3.     /** 
    4.      * This implementation always returns <code>true</code>. 
    5.      */  
    6.     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)  
    7.         throws Exception {  
    8.         return true;  
    9.     }  
    10.   
    11.     /** 
    12.      * This implementation is empty. 
    13.      */  
    14.     public void postHandle(  
    15.             HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)  
    16.             throws Exception {  
    17.     }  
    18.   
    19.     /** 
    20.      * This implementation is empty. 
    21.      */  
    22.     public void afterCompletion(  
    23.             HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)  
    24.             throws Exception {  
    25.     }  
    26.   
    27. }  
    28. MethodInterceptor是AOP项目中的拦截器,它拦截的目标是方法,即使不是controller中的方法。实现MethodInterceptor拦截器大致也分为两种,一种是实现MethodInterceptor接口,另一种利用AspectJ的注解或配置
       
      1. public class MethodInvokeInterceptor implements MethodInterceptor {  
      2.     @Override  
      3.     public Object invoke(MethodInvocation methodInvocation) throws Throwable {  
      4.         System.out.println("before method invoke");  
      5.         Object object = methodInvocation.proceed();  
      6.         System.out.println("after method invoke");  
      7.         return object;  
      8.     }  
      9. }  
      下面是基于注解的AspectJ方式
       
      1. @Component  
      2. @Aspect  
      3. public class AutoAspectJInterceptor {  
      4.   
      5.     @Around("execution (* com.test.controller..*.*(..))")  
      6.     public Object around(ProceedingJoinPoint point) throws Throwable{  
      7.         System.out.println("AutoAspectJInterceptor begin around");  
      8.         Object object = point.proceed();  
      9.         System.out.println("AutoAspectJInterceptor end around");  
      10.         return object;  
      11.     }  
      12.      
      13. }  
      下面是一个用于支持AspectJ方式拦截的普通的bean,当然你也可以在配置文件中声明这个bean 
      1. @Component  
      2.  public class AspectJInterceptor {  
      3.     public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {  
      4.         System.out.println("AspectJInterceptor around before");  
      5.         Object object = proceedingJoinPoint.proceed();  
      6.         System.out.println("AspectJInterceptor around after");  
      7.         return object;  
      8.     }  
      9. }  
      当然,这一切都离不开配置,具体看配置中的注释 
      1. <!-- 自定义拦截器 ,先过mvc:interceptors-->  
      2.     <bean id="methodInvokeInterceptor" class="com.test.interceptor.MethodInvokeInterceptor"/>  
      3.     <bean id="aspectInterceptor" class="com.test.interceptor.AspectJInterceptor"/>  
      4.   
      5.     <aop:config>  
      6.         <!--切入点,controlller -->  
      7.         <aop:pointcut id="pointcut_test"   expression="execution(* com.test.controller..*.*(..))" />  
      8.         <!--在该切入点使用自定义拦截器 ,按照先后顺序执行 -->  
      9.         <aop:advisor pointcut-ref="pointcut_test" advice-ref="methodInvokeInterceptor" />  
      10.   
      11.   
      12.         <aop:aspect ref="aspectInterceptor">  
      13.             <aop:around method="around" pointcut="execution(* com.test.controller..*.*(..))"/>  
      14.         </aop:aspect>  
      15.     </aop:config>  
      16.     <!-- 自动扫描使用了aspectj注解的类 -->  
      17.     <aop:aspectj-autoproxy/>  
  • 相关阅读:
    Nginx+tomcat负载均衡配置
    详解HttpURLConnection
    tomcat配置文件之Server.xml
    Android ADB命令大全(通过ADB命令查看wifi密码、MAC地址、设备信息、操作文件、查看文件、日志信息、卸载、启动和安装APK等)
    Tomcat多站点部署方式
    跨域、跨服务器调用时候session丢失的问题
    Linux系统的命令别名功能
    CentOS 升级GCC G++
    npm 安装碰到SSL问题
    centos图形界面的开启和关闭
  • 原文地址:https://www.cnblogs.com/cmfwm/p/7657101.html
Copyright © 2011-2022 走看看