[原創]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 }

 

 

 

 

 

    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM