你如何理解AOP中的连接点(Joinpoint)、切点(Pointcut)、增强(Advice)、引介(Introduction)、织入(Weaving)、切面(Aspect)这些概念?

7 篇文章 0 订阅
1 篇文章 0 订阅

你如何理解AOP中的连接点(Joinpoint)、切点(Pointcut)、增强(Advice)、引介(Introduction)、织入(Weaving)、切面(Aspect)这些概念?

  • 切面(Aspect)

    比较大的概念, 是指连接点的集合可以形成。所有功能的总称叫切面。

  • 连接点(Joinpoint)

    在Spring AOP中代表一个方法的执行程序执行的某个特定位置(如:某个方法调用前、调用后,方法抛出异常后)。一个类或一段程序代码拥有一些具有边界性质的特定点,这些代码中的特定点就是连接点。Spring仅支持方法的连接点。

  • 切点(Pointcut)
    一些特定的连接点(根据切点表达式进行匹配的连接点)。如果连接点相当于数据中的记录,那么切点相当于查询条件,一个切点可以匹配多个连接点。Spring AOP的规则解析引擎负责解析切点所设定的查询条件,找到对应的连接点。

  • 建议/增强(Advice)

    针对特定连接点采取的操作。增强是织入到目标类连接点上的一段程序代码。Spring提供的增强接口都是带方位名的,如:BeforeAdvice、AfterReturningAdvice、ThrowsAdvice等。

  • 引介(Introduction)

    (为某个类)声明另外的方法或字段的代表类型

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

  • 织入(Weaving)

    连接切面与其他应用类型或者对象来创建一个被建议过的对象(形成一个有Advice的原有对象的代理对象)

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

Let us begin by defining some central AOP concepts and terminology. These terms are not Spring-specific. Unfortunately, AOP terminology is not particularly intuitive. However, it would be even more confusing if Spring used its own terminology.

让我们由定义一些AOP的核心概念与术语开始。这些用于并不是Spring特有的,不幸的是AOP的术语不是十分直观。然而,如果Spring使用自己的术语将会更加混乱。

  • 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).

    Apect(切面):横切多个类的模块化概念。在Java企业级应用中事务管理是横切概念一个很好的例子。在Spring AOP中,切面是通过特定的类( schema-based approach)或者特定的类注解(@Aspect)实现的。

  • 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.

    Join point(连接点):程序执行期间的一个点,例如执行一个方法或者处理一个异常。在Spring AOP中一个连接点总是代表着一个方法的执行。

  • 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.

    Advice(建议):由一个切面在一个特定的连接点采取的操作。不同类型的建议包括around,before和after。许多AOP框架包括Spring,将一个Advice建模为一个拦截器,并在连接点周围维护一系列的拦截器(一个拦截器链)。

  • 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.

    Pointcut(切点):匹配连接点的中的谓词(特定的一些连接点)。一个Advice与一个Pointcut表达式关联并且运行在与该切点匹配的任何连接点处(例如,带有特定名称的方法的执行)。切入点表达式匹配连接点的概念是AOP的核心。默认情况下Spring使用AspectJ切入点表达式语言。

  • 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.)

    Introduction(引介):(为某个类)声明另外的方法或字段的代表类型。Spring AOP允许您向任何被建议的对象引入新的接口(和相应的实现)。例如,你可以使用Introduction使一个bean实现一个IsModified接口以简化缓存。(在AspectJ社区中一个Introduction被称为inter-type声明)

  • 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.

    -目标对象:一个或多个方面建议的对象。也称为“建议对象”。由于Spring AOP是使用运行时代理实现的,因此该对象始终是代理对象。

  • 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.
    -AOP代理:由AOP框架创建的对象,用于实施方面协定(建议方法执行等)。在Spring Framework中,AOP代理是JDK动态代理或CGLIB代理。

  • 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.

    Weaving(织入):连接切面与其他应用类型或者对象来创建一个被建议过的对象(形成一个有Advice的原有对象的代理对象)。这可以在编译时(例如使用AspectJ编译器),加载时,或者运行时完成。Spring AOP,像其他纯JavaAOP框架一样,倾向于在运行时织入。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值