现更新地址:http://www.yiyehu.tech/archives/2019/05/23/spring-aop
首先由两个概念,AOP(Aspect Oriented Programming)面向切面编程,OOP (Object Oriented Programming)面向对象编程。
OOP引入封装、继承、多态等概念来建立一种对象层次结构,用于模拟公共行为的一个集合。不过OOP允许开发者定义纵向的关系,但并不适合定义横向的关系。正是这些分散在各处且与对象核心功能无关的代码,使得模块复用难度增加。
而AOP就是对OOP的补充,AOP将那些影响了多个类的公共行为封装到一个可重用模块,并将其命名为"Aspect",即切面(方面,不喜欢这个翻译)。“切面”,就是那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块之间的耦合度,并有利于未来的可操作性和可维护性。
AOP实现方式
预编译:AspectJ
运行期动态代理(JDK动态代理、CGLib动态代理):SpringAOP、JbossAOP
相关点
- 切面(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.
- 连接点(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.
- 通知(Advice):action taken by an aspect at a particular join point. Different types of advice include “around,” “before” and “after” advice.
- 切入点(Pointcut):a predicate that matches join points. The concept of join points as matched by pointcut expressions is central to AOP
- 引入(introduction):declaring additional methods or fields on behalf of a type.如:引入父类接口,及父类实现。
- 目标(target):advised object.
- 代理(proxy):In the Spring Framework, an AOP proxy will be a JDK dynamic proxy or a CGLIB proxy.
- 织入(weaving):linking aspects with other application types or objects to create an advised object.