美文网首页
Spring Aop - 各类通知简介

Spring Aop - 各类通知简介

作者: 鱼da王 | 来源:发表于2018-12-10 17:28 被阅读0次
  1. 各类通知

    • 前置通知(@Before):在目标方法之前之执行,和目标方法紧挨着。注意,如果有环绕通知,先执行环绕通知代码块中目标方法前的代码,接着再执行前通知。
    • 环绕通知(@Around):可在目标方法执行的前后,分别增强。其中包含了目标方法的执行
    • 后置通知(@After):在目标方法和环绕通知执行之后执行
    • 后置目标方法返回通知(@AfterReturning):在目标方法返回参数之后执行
    • 异常通知(@AfterThrowing):当目标方法抛出异常后,执行此异常通知
  2. 各类通知的执行顺序

    顾名思义,按照通知的注解名称可大致看出执行顺序为:

    @Before -> @Around(start) -> target method -> @Around(end) -> @After -> @AfterReturning

  3. 简单用法

    此事例,建立在了解springboot简单基础知识基础上。

    @Aspect
    @Service
    public class AopSimpleService {
    
        /**
         * 切点
         */
        @Pointcut("execution(public * com.gold.aoptest.aopinter.impl.BeAopedService.doWorkSimple(..))")
        public void aspect(){}
    
        /**
         * 前置通知
         * @param joinPoint 连接点
         */
        @Before("aspect()")
        public void beforeMethod(JoinPoint joinPoint) {
            System.out.println("@Before 前置通知 Start ...");
            System.out.println("@Before 前置通知 END ...");
         }
    
        /**
         * 环绕通知
         * @param pjd
         * @return 返回值即目标方法的返回值
         */
        @Around(value = "aspect()")
        public Object aroundAdvice(ProceedingJoinPoint pjd){
            System.out.println("@Around 环绕通知 Start ...");
            Object o = null;
            try {
                // 执行被代理的方法,并获取反参
                o = pjd.proceed();
                System.out.println("@Around 环绕通知 END ...");
            } catch (Throwable throwable) {
                // 注意如果目标方法未捕获的异常,会在此捕获
                throwable.printStackTrace();
            }
            return o;
        }
    
        /**
         * 后置通知 方法执行完即执行此通知,无论异常与否
         * @param joinPoint 连接点
         */
        @After("aspect()")
        public void afterMethod(JoinPoint joinPoint) {
            System.out.println("@After 后置通知 Start...");
            System.out.println("@After 后置通知 END ...");
        }
    
        /**
         * 后置返回通知
         * 方法执行完即执行此通知,无论异常与否 在@After之后
         * @param joinPoint 连接点
         * @param result 目标方法的返回值
         */
        @AfterReturning(pointcut = "aspect()", returning = "result")
        public Object afterMethod(JoinPoint joinPoint, Object result ) {
            System.out.println("@AfterReturning 后置返回通知 Start ...");
            System.out.println("@AfterReturning 后置返回通知 END ...");
            return result;
        }
    
        /**
         * 异常切面
         * 当目标方法抛出异常后,会执行此通知代码
         * @param joinPoint 连接点
         * @param ex 目标方法抛出的异常
         */
        @AfterThrowing(value="aspect()", throwing="ex")
        public void afterThrowing(JoinPoint joinPoint, Exception ex) {
            System.out.println("@AfterThrowing Start ...");
            System.out.println("@AfterThrowing END ...");
        }
    
    }
    
    

    执行结果(不包含异常通知切面):


    Spring_AOP_1.png

相关文章

网友评论

      本文标题:Spring Aop - 各类通知简介

      本文链接:https://www.haomeiwen.com/subject/fjjdhqtx.html