你如何理解AOP中的連接點(Joinpoint)、切點(Pointcut)、增強(Advice)、引介(Introduction)、織入(Weaving)、切面(Aspect)這些概念?

你如何理解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框架一樣,傾向於在運行時織入。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章