AOP的复习整理

 初识AOP:

Aspect-oriented Programming (AOP) complements Object-oriented Programming (OOP) by providing another way of thinking about program structure. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. Aspects enable the modularization of concerns (such as transaction management) that cut across multiple types and objects. (Such concerns are often termed “crosscutting” concerns in AOP literature.)

面向方面编程(AOP)通过提供关于程序结构的另一种思考方式来补充面向对象编程(OOP)。OOP中模块化的关键单元是类,而在AOP中模块化单元是切面。AOP实现了跨多个类型和对象的关注点(如事务管理)的模块化。(在AOP文献中,这种关注点通常被称为“横切”关注点。)

 AOP作用:

使用"横切"技术,AOP 把软件系统分为两个部分:核心关注点和横切关注点。业务处理的主要流程是核心关注点,与之关系不大的部分是横切关注点。横切关注点的一个特点是,他们经常发生在核心关注点的多处,而各处基本相似,比如权限认证、日志、事物。AOP 的作用在于分离系统中的各种关注点,将核心关注点和横切关注点分离开来。

使程序员可以更加注重业务代码的编写或者横切业务代码的编写。

 业务场景

  •  Authentication 权限
  • Caching 缓存
  • Context passing 内容传递
  • Error handling 错误处理
  • Lazy loading 懒加载
  • Debugging 调试
  • logging, tracing, profiling and monitoring 记录跟踪 优化 校准
  •  Performance optimization 性能优化
  • Persistence 持久化
  •  Resource pooling 资源池
  • Synchronization 同步
  • Transactions 事务

 AOP中各个概念:

1. JoinPoint(连接点)

  • 官方文档: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.
  • 自我理解:代码运行过程中,任意一个点,例如代码执行前,代码处理中,代码执行后,处理异常等等,所有的方法都可以是连接点

2.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. 
  • 自我理解:增强的代码

3.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.
  • 自我理解:感觉就像是连接点的子集,它可以连接在被增强代码的多个连接点上,也可以连接在被增强代码的单个连接点,已经被增强的连接点

4.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.
  • 自我理解:被代理的对象,用JDK动态代理(同个接口)或CGLIB(继承)

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 annotation (the @AspectJ style).@Aspect 
  • 自我理解: 在类上添加@Aspect 来标识该类是个切面类,在该类中可以编写增强的代码逻辑

AOP通知类型

1.Before

前置通知:简单可以理解为 在增强代码(代理对象所要执行的方法)执行前执行的通知

2.AfterReturning

返回通知:从名字我们可以理解为 在方法执行后会执行该通知

3.After

后置通知: 和上面作对比 可以更好地区分开来 上面是方法执行后执行,这个我们可以简单理解为 在连接点退出的时候执行,无论代码是否有抛出异常 总会执行的

4.AfterThrowing

异常通知: 在方法抛出异常退出时执行的通知

5.Around

环绕通知: 环绕通知分为前和后两部分,即在方法调用前,方法调用后。我们可以将它的执行顺序用一个‘巨’来形容,里面的‘口’即为代理对象执行的方法和其他通知,而上下两横线即为它的执行顺序。

AOP执行顺序:

正常执行

1. @Around  环绕通知的前段代码执行

2. @Before 前置通知代码执行

3. 被增强的方法执行(即切入点)

4. @AfterReturnning 返回通知代码执行

5. @After 最终通知执行

6.@Around环绕通知后端代码执行

执行过程中出现异常

1. @Around  环绕通知的前段代码执行

2. @Before 前置通知代码执行

3. 被增强的方法执行(即切入点)

4. @AfterThrowing 异常通知代码执行

5. @After 最终通知执行

6. @Around 环绕通知的后段代码执行  (如果Around中有catch异常的话会执行,如果没有catch则会往上抛出异常)


如对上述执行过程有疑问  可以移步到关于@Around、@Before、@After、@AfterReturning、@AfterThrowing执行顺序以及执行结果总结_@around @before_XiaoAJieAi的博客-CSDN博客 观看代码片段(我也是搬运了这位博主的结论以及观看过其博客)

如果我理解的内容有误,烦请评论区留言或者私聊,欢迎各位来探讨~

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值