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