SpringBoot启动流程分析知识点–AOP(二)
一、概述
源码基于SpringBoot 2.7.xx版本
1.1 简介
上一节中介绍了在创建AOP代理对象时会先获取当前bean的Advices和Advisors,本节介绍Spring
AOP组件中的Aspect、Pointcut、Advice、Advisor、Advised。
1.2 Spring AOP组件
-
Aspect
- “切面”,切⾯(Aspect)由多个切入点(Pointcut)和多个通知(Advice)组成。
- 注解所在的包为–org.aspectj.lang.annotation.Aspect。
-
Pointcut
“切入点”,它的作用就是定义切面的匹配点。(简单的说就是我去切哪些类、哪些方法…) 在 Spring Aop 中匹配的点主要是 class 与
method 这两个方面,分别为ClassFilter 与 MethodMatcher。- 注解所在的包为–org.aspectj.lang.annotation.Pointcut。
- 最终转换为Spring中的对象–org.springframework.aop.Pointcut。
目前Spring支持的切点匹配表达式主要有以下几种: execution:可以定义到的最小粒度是方法,修饰符,包名,类名,方法名,Spring AOP主要也是使用这个匹配表达式; within:只能定义到类;例如@Pointcut(within(com.jnu.example.*)) this:当前生成的代理对象的类型匹配; target:目标对象类型匹配; args:只针对参数; annotation:针对注解; -
Advice
“通知”,需要执行的增强逻辑方法。包括 “around”, “before” and “after 等。
- 注解所在的包为–org.aspectj.lang.annotation.Around。
- 最终转换为Spring中的对象–org.springframework.aop.aspectj.AspectJAroundAdvice。
- 注解所在的包为–org.aspectj.lang.annotation.Before。
- 最终转换为Spring中的对象–org.springframework.aop.aspectj.AspectJMethodBeforeAdvice。
- 注解所在的包为–org.aspectj.lang.annotation.After。
- 最终转换为Spring中的对象–org.springframework.aop.aspectj.AspectJAfterAdvice。
- 注解所在的包为–org.aspectj.lang.annotation.AfterReturning。
- 最终转换为Spring中的对象–org.springframework.aop.aspectj.AspectJAfterReturningAdvice。
- 注解所在的包为–org.aspectj.lang.annotation.AfterThrowing。
- 最终转换为Spring中的对象–org.springframe
- 注解所在的包为–org.aspectj.lang.annotation.Around。

本文详细分析了Spring Boot启动流程中的AOP组件,包括Aspect、Pointcut、Advice、Advisor和Advised的概念及其相互关系。Aspect结合了切入点和通知,Pointcut定义匹配点,Advice定义增强逻辑,Advisor是Pointcut和Advice的组合,而Advised则代表了已织入的代理对象。文中还提及了@AspectJ和Advisor的区别以及它们在实际应用中的转换过程。
最低0.47元/天 解锁文章
430

被折叠的 条评论
为什么被折叠?



