AOP 依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId>
</dependency>

@Pointcut

定义切入点,有以下 2 种方式:

方式一:设置为注解 @LogFilter1 标记的方法,有标记的方法触发该 AOP,没有标记就没有。

@Aspect
@Component
public class LogFilter1Aspect {@Pointcut(value = "@annotation(com.train.aop.annotation.LogFilter1)")public void pointCut(){}
}

附上 LogFilter1 代码:

@Target(ElementType.METHOD)
@Retention(value = RetentionPolicy.RUNTIME)
public @interface LogFilter1 {}

对应的 Controller 方法如下,手动添加 @LogFilter1 注解:

@RestController
public class AopController {@RequestMapping("/aop")@LogFilter1public String aop(){System.out.println("这是执行方法");return "success";}
}

方式二:采用表达式批量添加切入点,如下方法,表示 AopController 下的所有 public 方法都添加 LogFilter1 切面

@Pointcut(value = "execution(public * com.train.aop.controller.AopController.*(..))")
public void pointCut(){}

@Around

环绕通知,可以说是使用最频繁的方式,会将整个方法包围起来

@Around(value = "pointCut()")
public Object round(ProceedingJoinPoint joinPoint){System.out.println("1、Round begin");Object obj = null;try {obj = joinPoint.proceed();} catch (Throwable throwable) {throwable.printStackTrace();}System.out.println("1、Round end");return obj;
}

@Before

前置方法,在目标方法执行前触发

@Before(value = "pointCut()")
public void before(){System.out.println("1、Before");
}

@After

后置方法,与 @Before 相反,在目标方法执行完毕后执行

@After(value = "pointCut()")
public void after(){System.out.println("1、After");
}

@AfterReturning

后置通知,在将返回值返回时执行

@AfterReturning(value = "pointCut()", returning = "result")
public void afterReturning(Object result){System.out.println("1、AfterReturning");
}

测试

接下来执行最开始声明的 AopController,输出如下:

1、Round begin
1、Before
这是执行方法
1、Round end
1、After
1、AfterReturning

由此可见,AOP 的顺序是:

  1. 首先进入 Around
  2. 执行 joinPoint.proceed () 之前,触发 Before
  3. 然后执行 joinPoint.proceed ()
  4. 执行 joinPoint.proceed () 之后,触发 After
  5. 最后触发 AfterReturning

多个 AOP 执行顺序

当创建了多个自定义注解,并标记到同一个方法上,可以通过设置 Order 来指定执行顺序。
这边创建一个 LogFilter2、LogFilter2Aspect,代码跟上面的例子一样,把 “1” 改成 “2” 就行

方式一:添加注解 @Order

@Aspect
@Component
@Order(1)
public class LogFilter1Aspect {}
@Aspect
@Component
@Order(2)
public class LogFilter2Aspect {}

方式二:实现 Ordered 接口

@Aspect
@Component
public class LogFilter1Aspect implements Ordered {@Overridepublic int getOrder() {return 1;}
}
@Aspect
@Component
public class LogFilter2Aspect implements Ordered {@Overridepublic int getOrder() {return 2;}
}

在 Controller 代码把 2 个注解都加上:

@RequestMapping("/aop")
@LogFilter1
@LogFilter2
public String aop(){System.out.println("这是执行方法");return "success";
}

执行结果如下:

1、Round begin
1、Before
2、Round begin
2、Before
这是执行方法
2、Round end
2、After
2、AfterReturning
1、Round end
1、After
1、AfterReturning

由此可见,Order 越小的越优先执行,但是,After 方法反而越后面执行,用一张图表示

1、外层的是 LogFilter1,因为 Order 设置为 1,最小优先进入
2、里层的是 LogFilter2,因为 Order 设置为 2,所以在 LogFilter1 后面执行
3、出来的时候,先穿过的是 LogFilter2,然后才穿过 LogFilter1,这也就是为什么 LogFilter2 的 After 方法会先执行
4、用一句话来形容,就是层层包围,层层嵌套

参考资料

https://blog.csdn.net/XWForever/article/details/103163021

Spring AOP 讲解(Pointcut、Before、Around、AfterReturning、After)相关推荐

  1. Spring AOP中Pointcut,dvice 和 Advisor三个概念

    Spring  AOP中Pointcut,dvice 和 Advisor三个概念介绍 在理解了Spring的AOP后,需要重点理解的三个概念是:Pointcut    Advice   Advisor ...

  2. 【老王读Spring AOP-3】Spring AOP 执行 Pointcut 对应的 Advice 的过程

    Spring AOP 执行 Pointcut 对应的 Advice 的过程 前言 版本约定 正文 jdk proxy 是如何执行 Pointcut 对应的 Advice 的? 获取 Advice 链的 ...

  3. Spring AOP切入点@Pointcut -- execution表达式

    Spring AOP 切入点@Pointcut – execution表达式 表达式示例 execution(* com.sample.service.impl..*.*(..)) 详述: execu ...

  4. Spring AOP中pointcut 切点详解

    Spring AOP中pointcut 是指那些方法需要被执行"AOP",是由"Pointcut Expression"来描述的. Pointcut可以有下列方 ...

  5. 【源码】Spring AOP 4 Pointcut

    [源码]Spring AOP 4 Pointcut 前言 Pointcut ClassFilter AnnotationClassFilter AnnotationCandidateClassFilt ...

  6. spring aop中pointcut表达式

    spring aop中pointcut表达式 本文主要介绍spring aop中9种切入点表达式的写法 execute within this target args @target @within ...

  7. Spring AOP 切点 Pointcut 表达式介绍与使用

    一.前言 面向切面编程 AOP 是一种常见的编程思想,是面向对象编程的一种补充,AOP 框架通过修改源代码,将处理逻辑编织到指定的业务模块中 常见的处理比如:在方执行法前进行校验,在方法执行后进行日志 ...

  8. Spring AOP之pointcut语法

    在文章Spring AOP之术语简介中有提到,pointcut定义了一种模式,用于匹配join point.Spring AOP中使用了AspectJ的pointcut模式定义语言. 声明一个poin ...

  9. SpringAOP专题之6、Spring AOP中@Pointcut 12种用法

    本文主要内容:掌握@Pointcut的12种用法. Aop相关阅读 阅读本文之前,需要先掌握下面几篇篇文章内容,不然会比较吃力. 代理详解(java动态代理&CGLIB代理) jdk动态代理和 ...

  10. Spring AOP AspectJ Pointcut Expressions With Examples--转

    原文地址:http://howtodoinjava.com/spring/spring-aop/writing-spring-aop-aspectj-pointcut-expressions-with ...

最新文章

  1. android7.1 动态申请权限改为默认授权,修改PackageManagerService.java下的grantPermissions为true
  2. 解决企业子CA无法检查吊销的问题
  3. CVPR 2021 3D视觉相关最新进展分享
  4. 【JAVA基础】HashSet、LinkedHashSet、TreeSet使用区别
  5. mysql中毫秒的保存类型
  6. 快速使用js验证输入的数字类型
  7. Php真太阳时计算公式,李顺祥:真太阳时的计算与应用
  8. knockoutjs与ajax,MVVM架构~knockoutjs系列之为Ajax传递Ko数组对象
  9. 宽度优先算法求解八数码问题
  10. CVPR2022论文速递(2022.3.24)!共11篇含表情识别/deepfake检测/插帧等
  11. 论文阅读: Anomaly Detection with Partially Observed Anomalies
  12. java登录界面圆形头像,Android使用CircleImageView实现圆形头像的方法
  13. app应用程序的好处
  14. 解魔方神器开源啦!摄像头看一眼,就能还原全步骤
  15. Django+sqlite开发简易记账本
  16. 使用.net 操作 微信公众平台 —— 接收用户操作 —— 关注/取消关注 公众号
  17. Kali之Crunch:自定义字典
  18. WIN10-VS2019-SeetaFace6编译
  19. java投票排名怎么弄_微信投票中,怎样快速投票把排名提上去呢?
  20. 牢记公式,ardupilot EKF2就是纸老虎(二)!

热门文章

  1. 重磅!阿里云发布业界首本云网络白皮书
  2. html 超链接嵌套,嵌套的超链接区域,HTML源中没有嵌套的链接元素
  3. 【图像处理】基于matlab GUI数字图像处理平台【含Matlab源码 381期】
  4. 【三维路径规划】基于matlab麻雀搜索算法无人机三维路径规划【含Matlab源码 171期】
  5. 【TWVRP】基于matlab蚁群算法求解带时间窗的多中心车辆路径规划问题【含Matlab源码 113期】
  6. icons在java显示出来_java – 制作jfilechooser显示图像缩略图
  7. list add java_list.add()和list.addAll()的区别
  8. break用于什么场景python_break语句陷入循环
  9. c语言数组转指针,(转)c语言指针数组
  10. 后缀的形容词_4.1.3后缀or形容词比较级【p78】