Spring Boot 學習之路——4.1 AOP註解方式實現打印日誌 詳解

以下內容轉自:https://www.cnblogs.com/lixiang1993/p/7447853.html


1.聲明一個切面類,並把這個切面類加入到IOC容器中

@Component
@Aspect
public class LogAspect{
    @Pointcut(value="execution(* *.*(..))")
    public void showLog(){}
    @Before(value="showLog()")
    public void showBeginLog(){
        System.out.println("AOP日誌開始");
    }
    @After(value="showLog()")
    public void showReturnLog(){
        System.out.println("AOP方法結束");
    }
    @AfterThrowing(value="showLog()")
    public void showExceptionLog(){
        System.out.println("AOP方法異常");
    }
    @AfterReturning(value="showLog()")
    public void showAfterLog(){
        System.out.println("AOP方法最終返回");
    }
}

2.被代理的對象也需要加入IOC容器

@Component
public class MathCalculator {
    public void add(int i, int j) {
        int result=i+j;
        System.out.println("目標方法add(int)執行了");
    }
    public void sub(int i, int j) {
        int result=i-j;
        System.out.println("目標方法sub執行了");
    }
    public void mult(int i, int j) {
        int result=i*j;
        System.out.println("目標方法mult執行了");
    }
    public void div(int i, int j) {
        int result=i/j;
        System.out.println("目標方法div執行了");
    }
}

3.切入點表達式

  • 切入點表達式的語法格式

execution([權限修飾符] [返回值類型] [簡單類名/全類名] [方法名]([參數列表]))

  • 切入點表達式支持通配符
  • 兩種切入表達式
    • 最詳細的切入點表達式:

 

execution(public void com.bwlu.aop.MathCalculator.add(int, int))

 

    • 最模糊的切入點表達式:
execution (* *.*(..))

execution(public void com.bwlu.aop.MathCalculator.add(int, int)):只有add方法加入了4個通知,

execution(public void com.bwlu.aop.MathCalculator.*(int, int)):任意方法,參數爲int,int

execution(public void com.bwlu.aop.MathCalculator.*(..)):MathCalculator中的任意方法,任意參數列表

execution(public * com.bwlu.aop.MathCalculator.*(..)):MathCalculator中的任意方法,任意參數列表,任意返回值

execution( * com.bwlu.aop.MathCalculator.*(..)):MathCalculator中的任意方法,任意參數列表,任意返回值,任意訪問修飾符

execution (* *.*(..)):任意匹配

需要注意的是:權限是不支持寫通配符的,當然你可以寫一個*表示所有權限所有返回值!


4.通知方法的細節

(1)在通知中獲取目標方法的方法名和參數列表

  • 在通知方法中聲明一個JoinPoint類型的形參
  • 調用JoinPoint對象的getSignature()方法獲取目標方法的簽名
  • 調用JoinPoint對象的getArgs()方法獲取目標方法的實際參數列表

(2)在返回通知中獲取方法的返回值

  • 在@AfterReturning註解中添加returning屬性:@AfterReturning (value="showLog()", returning= "result")
  • 在返回通知的通知方法中聲明一個形參,形參名和returning屬性的值一致:showReturnLog(JoinPoint joinPoint, Object result)

(3)在異常通知中獲取異常對象

  • 在@ AfterThrowing註解中添加throwing屬性:@AfterThrowing (value="showLog()",throwing= "throwable" )
  • 在異常通知的通知方法中聲明一個形參,形參名和throwing屬性值一致:showExceptinLog(JoinPoint joinPoint, Throwable throwable)
@Component
@Aspect
public class LogAspect{
    @Pointcut(value="execution(* *.*(..))")
    public void showLog(){}
    @Before(value="showLog()")
    public void showBeginLog(JoinPoint jPoint){
        Object[] args = jPoint.getArgs();
        List<Object> asList = Arrays.asList(args);
        Signature signature = jPoint.getSignature();
        String name = signature.getName();
        System.out.println("AOP日誌開始");
        System.out.println("目標方法名:"+name+",參數列表:"+asList);
    }
    @After(value="showLog()")
    public void showReturnLog(){
        System.out.println("AOP方法結束");
    }
    @AfterThrowing(value="showLog()",throwing="ex")
    public void showExceptionLog(Exception ex){
        System.out.println("AOP方法異常");
        System.out.println("異常信息:"+ex.getMessage());
    }
    @AfterReturning(value="showLog()",returning="result")
    public void showAfterLog(Object result){
        System.out.println("方法返回值:"+result);
        System.out.println("AOP方法最終返回");
    }
}


5.環繞通知@Around

環繞通知需要在方法的參數中指定JoinPoint的子接口類型ProceedingJoinPoint爲參數:

public void around(ProceedingJoinPoint joinPoint){}

環繞通知能夠替代其他4個通知。
注意:@Around修飾的方法一定要將方法的返回值返回!本身相當於代理!

@Component
@Aspect
public class LogAspect{
    @Around(value="execution(* *.*(..))")
    public Object showLog(ProceedingJoinPoint point){
        Object[] args = point.getArgs();
        List<Object> asList = Arrays.asList(args);
        Signature signature = point.getSignature();
        String name = signature.getName();
        Object result = null;
        try {
            try {
                //目標方法之前要執行的操作,相當於@before
                System.out.println("[環繞日誌]"+name+"開始了,參數爲:"+asList);
                //調用目標方法
                result = point.proceed(args);
            } finally {
                //方法最終結束時執行的操作,相當於@after
                System.out.println("[環繞日誌]"+name+"結束了!");
            }
            //目標方法正常執行之後的操作,相當於@AfterReturning
            System.out.println("[環繞日誌]"+name+"返回了,返回值爲:"+result);
        } catch (Throwable e) {
            //目標方法拋出異常信息之後的操作,相當於@AfterThrowing
            System.out.println("[環繞日誌]"+name+"出異常了,異常對象爲:"+e);
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }
}


6、切面的優先級

對於同一個代理對象,可以同時有多個切面共同對它進行代理。
可以在切面類上通過@Order (value=50)註解來進行設置,值越小優先級越高!

@Aspect

@Component

@Order(value=40)

public class LogAspect {}




明天再加日誌配置說明,洗洗睡了先



















發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章