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

    正常情况:环绕前置=====目标方法执行=====环绕返回=====环绕最终

    异常情况:环绕前置=====目标方法执行=====环绕异常=====环绕最终

 

posted @ 2020-10-24 16:53  orz江小鱼  阅读(3205)  评论(1编辑  收藏  举报