[原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

本人互联网技术爱好者,互联网技术发烧友

微博:伊直都在0221

QQ:951226918

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1.通知分类: 

 @Before: 前置通知, 在方法执行之前执行

 @After: 后置通知, 在方法执行之后执行

 @AfterRunning: 返回通知, 在方法返回结果之后执行

 @AfterThrowing: 异常通知, 在方法抛出异常之后

 @Around: 环绕通知, 围绕着方法执行

  关于方法签名 看第五点

2.前置通知

    

3.后置通知:在后置通知中,不能访问目标方法执行的结果

           

4.返回通知

             

         

5.异常通知

        

6.环绕通知

      

      

  1 package com.jason.spring.aop.impl;
  2 
  3 import java.util.Arrays;
  4 import java.util.List;
  5 
  6 import org.aspectj.lang.JoinPoint;
  7 import org.aspectj.lang.ProceedingJoinPoint;
  8 import org.aspectj.lang.annotation.After;
  9 import org.aspectj.lang.annotation.AfterReturning;
 10 import org.aspectj.lang.annotation.AfterThrowing;
 11 import org.aspectj.lang.annotation.Around;
 12 import org.aspectj.lang.annotation.Aspect;
 13 import org.aspectj.lang.annotation.Before;
 14 import org.springframework.stereotype.Component;
 15 
 16 
 17 //把这个类声明为一个切面
 18 //1.需要将该类放入到IOC 容器中
 19 @Component
 20 //2.再声明为一个切面
 21 @Aspect
 22 public class LoggingAspect {
 23     
 24     //声明该方法是一个前置通知:在目标方法开始之前执行 哪些类,哪些方法
 25     //作用:@before 当调用目标方法,而目标方法与注解声明的方法相匹配的时候,aop框架会自动的为那个方法所在的类生成一个代理对象,在目标方法执行之前,执行注解的方法
 26     //支持通配符
 27     //@Before("execution(public int com.jason.spring.aop.impl.ArithmeticCaculatorImpl.*(int, int))")
 28     @Before("execution(* com.jason.spring.aop.impl.*.*(int, int))")
 29     public void beforeMethod(JoinPoint joinPoint){
 30         String methodName = joinPoint.getSignature().getName();
 31         List<Object> args = Arrays.asList(joinPoint.getArgs());
 32         System.out.println("The method " + methodName + " begins " + args);
 33     }
 34     
 35     /**
 36      * 
 37      * @Author:jason_zhangz@163.com
 38      * @Title: afterMethod 
 39      * @Time:2016年12月1日
 40      * @Description:  在方法执行后执行的代码,无论该方法是否出现异常
 41      *
 42      * @param joinPoint
 43      */
 44     @After("execution(* com.jason.spring.aop.impl.*.*(int, int))")
 45     public void afterMethod(JoinPoint joinPoint){
 46         String methodName = joinPoint.getSignature().getName();
 47         List<Object> args = Arrays.asList(joinPoint.getArgs());
 48         System.out.println("The method " + methodName + " end " + args);
 49     }
 50     
 51     /**
 52      * 
 53      * @Author:jason_zhangz@163.com
 54      * @Title: afterReturning 
 55      * @Time:2016年12月1日
 56      * @Description:  在方法正常结束后执行代码,放回通知是可以访问到方法的返回值
 57      *
 58      * @param joinPoint
 59      */
 60     @AfterReturning( value="execution(* com.jason.spring.aop.impl.*.*(..))", returning="result")
 61     public void afterReturning(JoinPoint joinPoint ,Object result){
 62         String methodName = joinPoint.getSignature().getName();
 63         System.out.println("The method " + methodName + " end with " + result);
 64     }
 65     
 66     /**
 67      * 
 68      * @Author:jason_zhangz@163.com
 69      * @Title: afterThrowting 
 70      * @Time:2016年12月1日
 71      * @Description:  在目标方法出现异常时会执行代码,可以访问到异常对象,且,可以指定出现特定异常时执行通知代码
 72      *
 73      * @param joinPoint
 74      * @param ex
 75      */
 76     @AfterThrowing(value="execution(* com.jason.spring.aop.impl.*.*(..))",throwing="ex")
 77     public void afterThrowting(JoinPoint joinPoint, Exception  ex){
 78         String methodName = joinPoint.getSignature().getName();
 79         System.out.println("The method " + methodName + " occurs exceptions " + ex);
 80     }
 81     
 82     /**
 83      * 
 84      * @Author:jason_zhangz@163.com
 85      * @Title: around 
 86      * @Time:2016年12月1日
 87      * @Description: 环绕通知需要携带 ProceedingJoinPoint 类型的参数
 88      *                    环绕通知 类似于  动态代理的全过程
 89      *                   ProceedingJoinPoint:可以决定是否执行目标方法
 90      *    环绕通知必须有返回值,返回值即为目标方法的返回值
 91      *    
 92      * @param proceedingJoinPoint
 93      */
 94     @Around("execution(* com.jason.spring.aop.impl.*.*(..))")
 95     public Object around(ProceedingJoinPoint proceedingJoinPoint){
 96         
 97         Object result = null;
 98         String methodName = proceedingJoinPoint.getSignature().getName();
 99         
100         //执行目标方法
101         try {
102             //前置通知
103             System.out.println("The method " + methodName + "begin with" + Arrays.asList(proceedingJoinPoint.getArgs()));
104             
105             result = proceedingJoinPoint.proceed();
106             
107             //后置通知
108             System.out.println("The method " + methodName + "end with" + result);
109             
110         } catch (Throwable e) {
111             //异常通知
112             System.out.println("The method occurs exception : " + e);
113             throw new RuntimeException();
114         }
115             //后置通知
116             
117         System.out.println("The method " + methodName + "end with" + result);
118         
119         return result;
120         
121     }
122     
123     
124 }

    

原文地址:https://www.cnblogs.com/jasonHome/p/6123508.html