1. 程式人生 > 其它 >SpringBoot AOP中JoinPoint的用法和通知切點表示式

SpringBoot AOP中JoinPoint的用法和通知切點表示式

前言

上一篇文章講解了springboot aop 初步完整的使用和整合 這一篇講解他的介面方法和類

JoinPoint和ProceedingJoinPoint物件

  1. JoinPoint物件封裝了SpringAop中切面方法的資訊,在切面方法中新增JoinPoint引數,就可以獲取到封裝了該方法資訊的JoinPoint物件.

  2. ProceedingJoinPoint物件是JoinPoint的子介面,該物件只用在@Around的切面方法中

方法名 功能
Signature getSignature(); 獲取封裝了署名資訊的物件,在該物件中可以獲取到目標方法名,所屬類的Class等資訊
Object[] getArgs(); 獲取傳入目標方法的引數物件
Object getTarget(); 獲取被代理的物件
Object getThis(); 獲取代理物件
@Aspect
@Component
public class aopAspect {
    /**
     * 定義一個切入點表示式,用來確定哪些類需要代理
     * execution(* aopdemo.*.*(..))代表aopdemo包下所有類的所有方法都會被代理
     */
    @Pointcut("execution(* aopdemo.*.*(..))")
    public void declareJoinPointerExpression() {}

    /**
     * 前置方法,在目標方法執行前執行
     * @param joinPoint 封裝了代理方法資訊的物件,若用不到則可以忽略不寫
     */
    @Before("declareJoinPointerExpression()")
    public void beforeMethod(JoinPoint joinPoint){
        System.out.println("目標方法名為:" + joinPoint.getSignature().getName());
        System.out.println("目標方法所屬類的簡單類名:" +        joinPoint.getSignature().getDeclaringType().getSimpleName());
        System.out.println("目標方法所屬類的類名:" + joinPoint.getSignature().getDeclaringTypeName());
        System.out.println("目標方法宣告型別:" + Modifier.toString(joinPoint.getSignature().getModifiers()));
        //獲取傳入目標方法的引數
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            System.out.println("第" + (i+1) + "個引數為:" + args[i]);
        }
        System.out.println("被代理的物件:" + joinPoint.getTarget());
        System.out.println("代理物件自己:" + joinPoint.getThis());
    }

    /**
     * 環繞方法,可自定義目標方法執行的時機
     * @param pjd JoinPoint的子介面,添加了
     *            Object proceed() throws Throwable 執行目標方法
     *            Object proceed(Object[] var1) throws Throwable 傳入的新的引數去執行目標方法
     *            兩個方法
     * @return 此方法需要返回值,返回值視為目標方法的返回值
     */
    @Around("declareJoinPointerExpression()")
    public Object aroundMethod(ProceedingJoinPoint pjd){
        Object result = null;

        try {
            //前置通知
            System.out.println("目標方法執行前...");
            //執行目標方法
            //result = pjd.proeed();
            //用新的引數值執行目標方法
            result = pjd.proceed(new Object[]{"newSpring","newAop"});
            //返回通知
            System.out.println("目標方法返回結果後...");
        } catch (Throwable e) {
            //異常通知
            System.out.println("執行目標方法異常後...");
            throw new RuntimeException(e);
        }
        //後置通知
        System.out.println("目標方法執行後...");

        return result;
    }
}

切點表示式

  1. 在Spring AOP中,連線點始終代表方法的執行。切入點是與連線點匹配的,切入點表達語言是以程式設計方式描述切入點的方式。

  2. 切入點(Poincut)是定義了在“什麼地方”進行切入,哪些連線點會得到通知。顯然,切點一定是連線點

  3. 切點是通過@Pointcut註解和切點表示式定義的。@Pointcut註解可以在一個切面內定義可重用的切點。

execute表示式

*代表匹配任意修飾符及任意返回值,引數列表中..匹配任意數量的引數

可以使用&&、||、!、三種運算子來組合切點表示式,表示與或非的關係

  1. 攔截任意公共方法execution(public * *(..))
  2. 攔截以set開頭的任意方法execution(* set*(..))
  3. 攔截類或者介面中的方法
攔截AccountService(類、介面)中定義的所有方法
execution(* com.xyz.service.AccountService.*(..))
  1. 攔截包中定義的方法,不包含子包中的方法
攔截com.xyz.service包中所有類中任意方法,**不包含**子包中的類
execution(* com.xyz.service.*.*(..))
  1. 攔截包或者子包中定義的方法
攔截com.xyz.service包或者子包中定義的所有方法
execution(* com.xyz.service..*.*(..))

通知分類

@Before

  1. 前置通知: 在方法執行之前執行
  2. 前置通知使用@Before註解 將切入點表示式值作為註解的值

@After

  1. 後置通知, 在方法執行之後執行
  2. 後置通知使用@After註解 ,在後置通知中,不能訪問目標方法執行的結果

@AfterRunning

  1. 返回通知, 在方法返回結果之後執行
  2. 返回通知使用@AfterRunning註解

@AfterThrowing

  1. 異常通知, 在方法丟擲異常之後執行
  2. 異常通知使用@AfterThrowing註解

@Around

  1. 環繞通知, 圍繞著方法執行
  2. 環繞通知使用@Around註解
package com.jason.spring.aop.impl;
 
import java.util.Arrays;
import java.util.List;
 
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
 
 
//把這個類宣告為一個切面
//1.需要將該類放入到IOC 容器中
@Component
//2.再宣告為一個切面
@Aspect
public class LoggingAspect {
    
    //宣告該方法是一個前置通知:在目標方法開始之前執行 哪些類,哪些方法
    //作用:@before 當呼叫目標方法,而目標方法與註解宣告的方法相匹配的時候,aop框架會自動的為那個方法所在的類生成一個代理物件,在目標方法執行之前,執行註解的方法
    //支援萬用字元
    //@Before("execution(public int com.jason.spring.aop.impl.ArithmeticCaculatorImpl.*(int, int))")
    @Before("execution(* com.jason.spring.aop.impl.*.*(int, int))")
    public void beforeMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("The method " + methodName + " begins " + args);
    }
    
    /**
     * @Description:  在方法執行後執行的程式碼,無論該方法是否出現異常
     * @param joinPoint
     */
    @After("execution(* com.jason.spring.aop.impl.*.*(int, int))")
    public void afterMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("The method " + methodName + " end " + args);
    }
    
    /**
     * 
     * @Description:  在方法正常結束後執行程式碼,放回通知是可以訪問到方法的返回值
     *
     * @param joinPoint
     */
    @AfterReturning( value="execution(* com.jason.spring.aop.impl.*.*(..))", returning="result")
    public void afterReturning(JoinPoint joinPoint ,Object result){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " end with " + result);
    }
    
    /**
     * 
     * @Description:  在目標方法出現異常時會執行程式碼,可以訪問到異常物件,且,可以指定出現特定異常時執行通知程式碼
     *
     * @param joinPoint
     * @param ex
     */
    @AfterThrowing(value="execution(* com.jason.spring.aop.impl.*.*(..))",throwing="ex")
    public void afterThrowting(JoinPoint joinPoint, Exception  ex){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " occurs exceptions " + ex);
    }
    
    /**
     * 
     * @Description: 環繞通知需要攜帶 ProceedingJoinPoint 型別的引數
     *                    環繞通知 類似於  動態代理的全過程
     *                   ProceedingJoinPoint:可以決定是否執行目標方法
     *    環繞通知必須有返回值,返回值即為目標方法的返回值
     *    
     * @param proceedingJoinPoint
     */
    @Around("execution(* com.jason.spring.aop.impl.*.*(..))")
    public Object around(ProceedingJoinPoint proceedingJoinPoint){
        
        Object result = null;
        String methodName = proceedingJoinPoint.getSignature().getName();
        
        //執行目標方法
        try {
            //前置通知
            System.out.println("The method " + methodName + "begin with" + Arrays.asList(proceedingJoinPoint.getArgs()));
            
            result = proceedingJoinPoint.proceed();
            
            //後置通知
            System.out.println("The method " + methodName + "end with" + result);
            
        } catch (Throwable e) {
            //異常通知
            System.out.println("The method occurs exception : " + e);
            throw new RuntimeException();
        }
            //後置通知
            
        System.out.println("The method " + methodName + "end with" + result);
        
        return result;        
    }
}

切點表示式參考