【242期】面试官——Spring AOP有哪些通知类型,它们的执行顺序是怎样的?

本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!

本文GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了6个月总结的一线大厂Java面试总结,本人已拿大厂offer,欢迎star

原文链接:blog.ouyangsihai.cn >> 【242期】面试官——Spring AOP有哪些通知类型,它们的执行顺序是怎样的?

【242期】面试官:Spring AOP有哪些通知类型,它们的执行顺序是怎样的?

前言

这篇比较简单,但是群友反馈面试被问到了,关键还没答出来,而且还是项目中用的比较多的技术点。还是要在平时开发中有一丢丢好奇心,多点进去看看代码啊!

通知类型

在基于Spring AOP编程的过程中,基于AspectJ框架标准,spring中定义了五种类型的通知,它们分别是:

  • 前置通知 (@Before) 。
  • 返回通知 (@AfterReturning) 。
  • 异常通知 (@AfterThrowing) 。
  • 后置通知 (@After)。
  • 环绕通知 (@Around) :(优先级最高)
  • 通知执行顺序

    将上面的所有通知类型写入同一个切面中,它的执行顺序为:

    代码展示

    
    package com.cy.pj.common.aspect;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;
    
    @Aspect
    @Component
    public class SysTimeAspect {
    
     /**
      * 切入点
      */
     @Pointcut("bean(sysMenuServiceImpl)")
     public void doTime(){}
    
     @Before("doTime()")
     public void doBefore(JoinPoint jp){
      System.out.println("time doBefore()");
     }
     @After("doTime()")
     public void doAfter(){//类似于finally{}代码块
      System.out.println("time doAfter()");
     }
     /**核心业务正常结束时执行
      * 说明:假如有after,先执行after,再执行returning*/
     @AfterReturning("doTime()")
     public void doAfterReturning(){
      System.out.println("time doAfterReturning");
     }
     /**核心业务出现异常时执行
      * 说明:假如有after,先执行after,再执行Throwing*/
     @AfterThrowing("doTime()")
     public void doAfterThrowing(){
      System.out.println("time doAfterThrowing");
     }
     @Around("doTime()")
     public Object doAround(ProceedingJoinPoint jp)
       throws Throwable{
      System.out.println("doAround.before");
      try {
      Object obj=jp.proceed();
      return obj;
      }catch(Throwable e) {
      System.out.println("doAround.error--"+e.getMessage());
      throw e;
      }finally {
      System.out.println("doAround.after");
      }
     }
    
    }
    

    代码正常结束

    代码出现异常

    **作者:其乐m** **来源:my.oschina.net/u/4115134/blog/3216359**

    来源:my.oschina.net/u/4115134/blog/3216359

    END

    十期推荐


    与其在网上拼命找题?** 不如马上关注我们~**

    【242期】面试官:Spring AOP有哪些通知类型,它们的执行顺序是怎样的?

    原文始发于微信公众号(Java面试题精选):

    本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!

    本文GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了6个月总结的一线大厂Java面试总结,本人已拿大厂offer,欢迎star

    原文链接:blog.ouyangsihai.cn >> 【242期】面试官——Spring AOP有哪些通知类型,它们的执行顺序是怎样的?


     上一篇
    【240期】面试官——你了解JVM的内存溢出吗? 【240期】面试官——你了解JVM的内存溢出吗?
    Java堆溢出Java堆用于存储对象实例,只要不断地创建对象,当对象数量到达最大堆的容量限制后就会产生内存溢出异常。最常见的内存溢出就是存在大的容器,而没法回收,比如:Map,List等。 内存溢出:内存空间不足导致,新对象无法分配到
    2021-04-05
    下一篇 
    【243期】面试官——什么是前缀索引、为什么要用前缀使用、用在什么场景下? 【243期】面试官——什么是前缀索引、为什么要用前缀使用、用在什么场景下?
    什么是前缀索引?前缀索引也叫局部索引,比如给身份证的前 10 位添加索引,类似这种给某列部分信息添加索引的方式叫做前缀索引。 为什么要用前缀索引?前缀索引能有效减小索引文件的大小,让每个索引页可以保存更多的索引值,从而提高了索引查询的速
    2021-04-05