Spring-AOP-環繞通知@Around


Spring-AOP-環繞通知@Around

環繞通知是Spring中最強大的通知

@Around:環繞:就是手寫版的動態代理

四合一通知其實就是環繞通知,而且環繞通知里面的執行順序是確定的

@Around(value = "myPointCut()")

    public Object myAround(ProceedingJoinPoint proceedingJoinPoint)

    {

        Object[] args = proceedingJoinPoint.getArgs();

        Object result=null;

        try {

            //前置通知@Before

            System.out.println("環繞前置通知");

            //目標方法執行

            result = proceedingJoinPoint.proceed(args);

            //環繞返回通知@AfterReturning

            System.out.println("環繞返回通知");

        } catch (Throwable throwable) {

            //環繞異常通知@AfterThrowing

            System.out.println("環繞異常通知");

            throw new RuntimeException(throwable);

        } finally {

            //最終通知@After

            System.out.println("環繞最終通知");

        }

        return result;

    }

@Around的執行順序

  Spring4.0

    正常情況:環繞前置=====目標方法執行=====環繞返回=====環繞最終

    異常情況:環繞前置=====目標方法執行=====環繞異常=====環繞最終

  Spring5.28

    正常情況:環繞前置=====目標方法執行=====環繞返回=====環繞最終

    異常情況:環繞前置=====目標方法執行=====環繞異常=====環繞最終

 


免責聲明!

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



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