AOP 总结

AOP即Aspect oriented Programing, 面向切面编程。

相关术语:

通知(Advice):

Advice defineds when to execute what action.
通知定义了切面要执行的内容以及在什么时候执行该内容。

Spring支持的5种通知类型:

  • Before - org.springframework.aop.MethodBeforeAdvice
  • After-returning - org.springframework.aop.AfterRetruningAdvice
  • After-throwing - org.springframework.aop.ThrowsAdvice
  • Arount - org.aopaliance.intercept.MethodInterceptor
  • Introduction - org.springframework.aop.IntroductionInterceptor

连接点(JoinPoint):

The time to execute advice.
连接点指明了应用通知的时机,比如方法执行时,异常抛出时等。

切入点(PointCut):

Where to execute the action.
定义了在什么地方去执行织入的操作, 比如某个类名或者方法名。可以使用正则表达式表示。

切面(Aspect):

The advice and pointcut makes up the aspect to specify where and when to execute the action.
通知和切入点共同组成了切面,即为动作要执行的时间,内容和地点。

引入(Introduction):

Allows us to add new methods or properties to a class.
引入允许向现有的类添加新的方法和属性(Spring引入类方法注入的功能)

目标(Target):

The target got adviced. If there's no AOP, the target need to execute other kind of logics like recording log, transaction controlle, and with AOP, it can focus on its own business logic.
被通知的对象。不使用AOP的时候,目标的逻辑需要交叉其它的诸如日志记录,事务控制等事务逻辑,使用AOP的时候,则只需要关注自己的业务逻辑就可以了。

代理(Proxy):

The object to apply advice.
应用通知的对象。

织入(Weaving):

Apply the aspect to the target to generate the proxy.
把切面应用到目标对象来创建代理对象的过程,织入一般发生在如下几个时机:

  • 编译时: 当一个类文件被编译时织入,需要特殊的编译器才能实现,比如AspectJ的编译器
  • 类加载时: 当使用特殊的类加载器在目标被加载到虚拟机之前增强类的字节代码
  • 运行时: 切面在运行的某个时刻被织入,SpringAOP就是以此种方式织入的,原理是是用了JDK的动态代理技术。

实现方式

  1. 基于代理
  2. 通过@AspectJ注解驱动
  3. 纯POJO切面
  4. 注入式AspectJ切面

posted @ 2017-06-27 12:11  安果果  阅读(212)  评论(0编辑  收藏  举报