AOP术语、SpringAOP

  • AOP中的相关术语
  • SpringAOP中的实现
  • Spring对AOP的支持
  • 示例
    • 切点表达式、切点
    • 切面
    • 增强通知
    • 织入
    • 目标对象
    • 结果

AOP中的相关术语

官方
Aspect: A modularization of a concern that cuts across multiple classes. Transaction management is a good example of a crosscutting concern in enterprise Java applications. In Spring AOP, aspects are implemented by using regular classes (the schema-based approach) or regular classes annotated with the @Aspect annotation (the @AspectJ style).

Join point: A point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point always represents a method execution.

Advice: Action taken by an aspect at a particular join point. Different types of advice include “around”, “before” and “after” advice. (Advice types are discussed later.) Many AOP frameworks, including Spring, model an advice as an interceptor and maintain a chain of interceptors around the join point.

Pointcut: A predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut (for example, the execution of a method with a certain name). The concept of join points as matched by pointcut expressions is central to AOP, and Spring uses the AspectJ pointcut expression language by default.

Introduction: Declaring additional methods or fields on behalf of a type. Spring AOP lets you introduce new interfaces (and a corresponding implementation) to any advised object. For example, you could use an introduction to make a bean implement an IsModified interface, to simplify caching. (An introduction is known as an inter-type declaration in the AspectJ community.)

Target object: An object being advised by one or more aspects. Also referred to as the “advised object”. Since Spring AOP is implemented by using runtime proxies, this object is always a proxied object.

AOP proxy: An object created by the AOP framework in order to implement the aspect contracts (advise method executions and so on). In the Spring Framework, an AOP proxy is a JDK dynamic proxy or a CGLIB proxy.

Weaving: linking aspects with other application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime.

通知、增强处理(Advice) 就是抽取出来的通用功能,也就是安全、事物、日志等。你给先定义好,然后在需要的地方引用。

连接点(JoinPoint) 应用执行过程中能够插入切面的一个点,这个点可以是方法调用时,抛出异常时,甚至修改字段时。切面代码可以利用这些点插入到应用的正常流程之中,并添加新的行为。
在 Spring AOP 中,Join Point 总是方法的执行点, 即只有方法连接点。所以使用SpringAOP,无需使用
连接点的代码。就是spring允许你引用通知(Advice)的地方,那可就真多了,基本每个方法的前、后以及前后都包括,或抛出异常时都可以是连接点,spring只支持方法连接点。其他如AspectJ还可以让你将构造器、属性注入作为连接点。不过那不是咱们关注的,只要记住,和方法有关的前前后后都是连接点,也就是可以引入通知的地方都是连接点。

切点(Pointcut)在 Spring 中, 所有的方法都可以认为是 Join Point,但是我们并不希望在所有的方法上都添加 Advice, 而 Pointcut 的作用就是提供一组规则(使用 AspectJ pointcut expression language 来描述)来匹配Join Point,给满足规则的 Join Point 添加 Advice。 上面说的连接点的基础上,来定义切入点,你的一个类里,有15个方法,那就有十几个连接点了对吧,但是你并不想在所有方法附近都使用通知(使用叫织入),你只是想让其中几个,在调用这几个方法之前、之后或者抛出异常时干点什么,那么就用切点来定义这几个方法,让切点来筛选连接点,选中你想要引入通知的方法。

切面(Aspect)切面(Aspect)由 切点(Pointcut)和通知(Advice)组成,它既包含了横切逻辑的定义,也包括了连接点的定义。Spring AOP就是负责实施切面的框架,它将切面所定义的横切逻辑织入到切面所指定的连接点中。
在SpringAOP中,切面由容器中的Bean使用@Aspect注解实现 切面是通知和切入点的结合。连接点是为了让你好理解切点搞出来的,明白这个概念就行了。通知说明了干什么和什么时候干(什么时候通过方法名中的befor,after,around等就能知道),二切入点说明了在哪干(指定到底是哪个方法),这就是一个完整的切面定义。

引介(Introduction):引介是一种特殊的增强,它为类添加一些属性和方法。这样,即使一个业务类原本没有实现某个接口,通过引介功能,可以动态的未该业务类添加接口的实现逻辑,让业务类成为这个接口的实现类。

织入(Weaving):织入是将增强添加到目标类具体连接点上的过程,AOP有三种织入方式:①编译期织入:需要特殊的Java编译期(例如AspectJ的ajc);②装载期织入:要求使用特殊的类加载器,在装载类的时候对类进行增强;③运行时织入:在运行时为目标类生成代理实现增强。Spring采用了动态代理的方式实现了运行时织入,而AspectJ采用了编译期织入和装载期织入的方式。

目标对象:被一个或者多个切面所通知(Advice)的对象,也把它叫做被通知的对象(Advised)。既然SpringAOP是通过运行时代理实现的,那么这个对象永远是一个被代理(Proxied)对象。

SpringAOP中的实现

Spring AOP由 spring-aop 、 spring-aspects 和 spring-instrument 3 个模块组成。
SpringAOP构建在动态代理基础上,因此Spring对AOP的支持局限于方法拦截。
SpringAOP支持 JDK 和 CGLIB 方式实现动态代理。默认情况下,实现了接口的类,使用 AOP 会基于JDK 生成代理类,没有实现接口的类,会基于 CGLIB 生成代理类。

Spring对AOP的支持

基于代理的经典 AOP;
@AspectJ 注解驱动的切面;
纯 POJO 切面;
注入式 AspectJ 切面; Spring

通过在代理类中织入包裹切面,Spring 在运行期间将切面织入到 Spring 管理的 Bean 中。
代理类封装了目标类,并拦截被通知的方法调用,再将调用转发给真正的目标 Bean
当拦截到方法调用时,在调用目标 Bean 方法之前,代理会执行切面逻辑。
当真正应用需要被代理的 Bean 时,Spring 才创建代理对象。如果使用 ApplicationContext,在 ApplicationContext 从 BeanFactory 中加载所有 Bean 时,Spring 创建代理对象,因为 Spring 在运行时候创建代理对象,所以我们不需要特殊的编译器来织入 Spring AOP 的切面。

Spring 支持方法创建连接点
因为 Spring 基于动态代理,所以 Spring 只支持方法连接点。
Spring 缺失对字段连接点的支持,无法让我们更加细粒度的通知,例如拦截对象字段的修改
Spring 缺失对构造器连接点支持,在 Bean 创建时候进行通知。

示例

切点表达式、切点

在 Spring AOP 中,需要使用 AspectJ 的切点表达式来定义切点。
具体可以参考这篇

@AspectJ 支持三种通配符
* :匹配任意字符,只匹配一个元素(包,类,或方法,方法参数)
.. :匹配任意字符,可以匹配多个元素 ,在表示类时,必须和 * 联合使用。
+ :表示按照类型匹配指定类的所有类,必须跟在类名后面,如 com.cad.Car+ ,表示继承该类的所有子类包括本身
切点表达式由切点函数组成,其中 execution() 是最常用的切点函数,用来匹配方法,语法为:
execution(<修饰符><返回类型><包.类.方法(参数)><异常>)
修饰符和异常可以省略

表达式示例
execution(* com.cad.demo.User.(…)) :匹配 User 类里的所有方法
execution(
com.cad.demo.User+.(…)) :匹配该类的子类包括该类的所有方法
execution(
com.cad..(…)) :匹配 com.cad 包下的所有类的所有方法
execution(* com.cad….(…)) :匹配 com.cad 包下、子孙包下所有类的所有方法
execution(* addUser(String, int)) :匹配 addUser 方法,且第一个参数类型是 String,第二个
参数类型是 int

匹配com.example.demo.controller包下的UserController类的所有方法

切面

增强通知

前置通知
使用@Before:通知方法会在目标方法调用之前执行。
后置通知
使用@After:通知方法会在目标方法返回或者抛出异常后调用。
返回之后通知
使用@AfterReturning:通知方法会在目标方法返回后调用。
抛异常后通知
使用@AfterThrowing:通知方法会在目标方法抛出异常后调用。
环绕通知
使用@Around:通知包裹了被通知的方法,在被通知的方法通知之前和调用之后执行自定义的行
为。

//2.添加通知@Before("pointcut()")public void doBefore(){System.out.println("执行了切面的前置通知");}//3.添加后置通知@After("pointcut()")public void doAfter(){System.out.println("执行了切面的后置通知");}//添加在return之前的通知方法@AfterReturning("pointcut()")public void doAfterReturn(){System.out.println("执行了在return之前的通知");}//添加在抛出异常之前的通知方法@AfterThrowing("pointcut()")public void doThrowing(){System.out.println("执行了在抛出异常之前的通知");}//环绕通知@Around("pointcut()")public Object doAround(ProceedingJoinPoint joinPoint){System.out.println("进行了环绕通知:方法执行之前");Object res = null;//执行被代理的方法try {res = joinPoint.proceed();} catch (Throwable throwable) {throwable.printStackTrace();}System.out.println("执行l环绕通知:执行了方法调用之后");return res;}

织入

织入是把切面应用到目标对象并创建新的代理对象的过程,切面在指定的连接点被织入到目标对象中。
在目标对象的生命周期里有多个点可以进行织入:
编译期:
切面在目标类编译时被织入。这种方式需要特殊的编译器。AspectJ的织入编译器就是以这种方式织入切面的。
类加载器:
切面在目标类加载到JVM时被织入。这种方式需要特殊的类加载器(ClassLoader),它可以在目标
类被引入应用之前增强该目标类的字节码。AspectJ5的加载时织入(load-time weaving. LTW)就支持以这种方式织入切面。
运行期:
切面在应用运行的某一时刻被织入。一般情况下,在织入切面时,AOP容器会为目标对象动态创建
一个代理对象。SpringAOP就是以这种方式织入切面的。

目标对象

结果




AOP术语、SpringAOP相关推荐

  1. Aop简介 Aop术语 SpringAOP

    1.AOP简介 1.AOP (面向切面编程):是一种新的方法论,是对传统OOP(面向对象编程)的补充 2.AOP 的主要编程对象是切面(aspect),而切面模块化横切关注点 3.在应用AOP编程时, ...

  2. 有关AOP术语(织入、增强等)的个人理解

    2021.4.11 阅读<精通Spring4.x企业应用开发实战>一书做笔记 下图为本人理解的AOP术语韦恩图,由于引介跟增强在同一个位置,没在图上标出. 连接点(Joint Point) ...

  3. 跟杨春娟学Spring笔记:AOP之SpringAOP引介通知

    跟杨春娟学Spring笔记:AOP之SpringAOP引介通知 完成:第一遍 1.如何代码实现AOP之SpringAOP引介通知? IntroductionInterceptor(引介通知) 在目标类 ...

  4. 通俗易懂的Spring AOP术语

    今天写的这篇文章主要介绍Spring中的AOP术语,对于刚接触Spring AOP术语的初学者来说看书上的介绍或者其他视频讲解的对于自己理解起来很吃力,所以小编结合自己的经历总结了下所谓的AOP术语. ...

  5. spring中的aop术语和细节

    Spring中AOP的细节 说明 我们学习spring的aop,就是通过配置的方式 AOP相关术语 Joinpoint(连接点): 所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法, ...

  6. spring的AOP术语

    分析 技术分析之AOP的相关术语 1. Joinpoint(连接点) -- 所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点 2. Point ...

  7. Spring AOP术语

    1.连接点(Joinpoint) 程序执行的某个特定位置:如类开始初始化前.类初始化后.类某个方法调用前.调用后.方法抛出异常后.这些代码中的特定点,称为"连接点".Spring仅 ...

  8. 【Spring】Spring的AOP术语解释

    通知(Advice) 切面的工作被称为通知,通知定义了切面是什么以及何时使用.除了描述切面要完成的工作,通知还解决了何时执行这个工作的问题.它应该应用在某个方法被调用之前?之后?之前和之后都调用?还是 ...

  9. spring学习--AOP术语

    a)连接点:类里面哪些方法可以被增强,这些方法称为连接点 ​ b)切入点:实际被真正增强的方法称为切入点 ​ c)通知(增强):实际增强的逻辑部分称为通知,且分为以下五种类型: ​ 1)前置通知 :函 ...

最新文章

  1. mongodb 索引去重_朋友问你 MongoDB 是什么?给他看这篇就好了
  2. R函数之:apply(), lapply(), sapply(), tapply()
  3. POI之excel导入导出
  4. android 保存崩溃信息,Android保存每次运行崩溃报告的日志
  5. AIProCon在线大会笔记之张钹院士:探索第三代人工智能,需要勇闯无人区的人才!
  6. 微服务发现组件Eureka:微服务注册
  7. Bypass WAF实战总结
  8. java 集合类 *****
  9. 中国黑客常用六种工具及防御方法(转)
  10. Java 字符串处理
  11. go[x]agent在windows和ubuntu下的安装步骤
  12. 影视后期制作中AEnbsp;抠像技术浅…
  13. markdown 教程一
  14. 一,JavaScript基本语法
  15. Redis6课程大纲
  16. 可穿戴从业者必读:2014华米踩过的那些坑
  17. 硅谷之行 (19) 硅谷的雨季
  18. 中国远程心电监测仪市场研究与预测报告(2021版)
  19. WLS:cargo run 无法执行,提示 rustc -vV缺少host
  20. Hblock盘活企业级存储市场

热门文章

  1. Complementary Trilateral Decoder for Fast and Accurate Salient Object Detection(速读啊)内含与u-shape的对比
  2. null hypothesis
  3. What Is a Testable Hypothesis?
  4. 什么是公考、联考、国考、省考、选调生?
  5. python求音频的梅尔倒谱系数
  6. China Joy 还没看够?2020 谷歌游戏出海峰会带来更多精彩!
  7. 2020年—岁月静好,温暖如初
  8. 游戏服务器会遭到什么攻击,被攻击了怎么防御
  9. 游戏定制开发自建团队好吗?
  10. Dev express 通过代码添加ribbonpage