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;        
    }
}

切點表達式參考


免責聲明!

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



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