2.1 spring源碼學習 給aop切面註解獲取相關參數

注:原始學習資料來自於享學課堂

接:https://blog.csdn.net/qq_22701869/article/details/102627657

在對切面操作的時候,有時需要獲取到被切面方法的參數以及執行結果等信息,具體使用發方法如下

@Aspect
public class LogAspects {
    @Pointcut("execution(public int cn.enjoy.bean.Calculator.*(..))")
    public void pointCut(){}

    @Around("pointCut()")
    public Object Around(ProceedingJoinPoint proceedingJoinPoint) 
throws  Throwable{
        System.out.println("@around 之前"+proceedingJoinPoint.getArgs());
        Object o = proceedingJoinPoint.proceed();
        System.out.println("@around之後")
        ;
        return o;
    }
    @Before("pointCut()")
    public void logStart(JoinPoint joinPoint){
        System.out.println(joinPoint.getSignature().getName()+
"方法運行-------參數列表是:{"+ Arrays.asList(joinPoint.getArgs())+"}");
    }
    @After("pointCut()")
    public void logEnd(JoinPoint joinPoint){
        System.out.println(joinPoint.getSignature().getName()+"運行結束-------");
    }
    @AfterReturning(value = "pointCut()",returning = "result")
    public void logReturn(Object result){
        System.out.println("運行正常-------結果是:{"+ result +"}");
    }
    @AfterThrowing(value = "pointCut()",throwing = "exception")
    public void logException(Exception exception){
        System.out.println("運行異常-------異常信息是:{"+exception+"}");
    }
}

執行結果

IOC容器初始化完成
@around 之前[Ljava.lang.Object;@2df3b89c
div方法運行-------參數列表是:{[4, 2]}
-----------計算除法中--------
@around之後
div運行結束-------
運行正常-------結果是:{2}

其中@After和Before使用JoinPoint獲取相關參數

@Around使用ProceedingJoinPoint獲取闡述

@AfterReturning使用returning = "result"
@AfterThrowing 使用throwing = "exception"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章