Spring AOP 的簡單應用

因為工作需求,自己去了解一下aop并做下的記錄,當然大部分都是參考他人博客以及官方文檔。

目錄

<a name="關于 AOP"></a>

關于 AOP

大家都知道Spring框架有兩大重要特性,IOC 控制反轉 (Inversion of Control,IoC) 以及AOP 面向切面編程(Aspect Oriented Program, AOP)。今天主要是來一起了解一下AOP。

其主要作用是,在不修改源代碼的情況下給某個或者一組操作添加額外的功能。像日志記錄,事務處理,權限控制等功能,都可以用AOP來“優雅”地實現,使這些額外功能和真正的業務邏輯分離開來,軟件的結構將更加清晰。

簡單來說在運行時,動態地將代碼切入到類的指定方法、指定位置上的編程思想就是面向切面的編程。

<a name = "相關術語"></a>

相關術語

Aspect(切面)

aspectpointcutadvice組成,它即包含了橫切的定義,也包含了鏈接點的定義。由Spring AOP負責實施切面,它將切面所定義的橫切邏輯織入到切面所指定的鏈接點中。

簡單來說,只要在類上有 @Aspect 注解的類就是切面。

Join point(鏈接點/記錄點)

程序運行中的一個點,例如一個運行方法或者異常處理。

在Spring AOP中,一個join point總是一個運行方法,即只有方法才是連接點。

advice (增強/通知)

在join point(即滿足 point cut 規則的join point)上特定的時刻執行的操作,Advice有幾種不同類型,下文將會討論(通俗地來講就是起作用的內容和時間點)。

Pointcut(切點)

匹配 join point 的謂詞(a predicate that matches join points).

advice 與 pointcut 表達式相關聯,并在與 pointcut 匹配的任意 joinpoint 運行(例如,執行具有特定名稱的方法)。

簡單來說 pointcut 是一個joinpoint 的過濾器,只有滿足 pointcut 的規則的 joinpoint 才會執行 advice。

Introduction

為一個類型添加額外的方法或字段. Spring AOP 允許我們為 目標對象 引入新的接口(和對應的實現). 例如我們可以使用 introduction 來為一個 bean 實現 IsModified 接口, 并以此來簡化 caching 的實現.

Target object

織入一個或多個 advice 的目標對象. 目標對象也被稱為 advised object.

因為 Spring AOP 使用運行時代理的方式來實現 aspect, 因此 adviced object 總是一個代理對象(proxied object)
注意, adviced object 指的不是原來的類, 而是織入 advice 后所產生的代理類.

AOP proxy

一個類被 AOP 織入 advice, 就會產生一個結果類, 它是融合了原類和增強邏輯的代理類.在 Spring AOP 中, AOP 代理將是一個 JDK 動態代理對象或 CGLIB 代理對象.

Weaving (織入)

將 aspect 和其他對象連接起來, 并創建 adviced object 的過程.根據不同的實現技術, AOP織入有三種方式:

  • 編譯器織入, 這要求有特殊的Java編譯器.
  • 類裝載期織入, 這需要有特殊的類裝載器.
  • 動態代理織入, 在運行期為目標類添加增強(Advice)生成子類的方式.

與其他純Java AOP框架一樣,Spring AOP在運行時執行編織。

advice 的類型

  • before advice, 在 join point 前被執行的 advice. 雖然 before advice 是在 join point 前被執行, 但是它并不能夠阻止 join point 的執行, 除非發生了異常(即我們在 before advice 代碼中, 不能人為地決定是否繼續執行 join point 中的代碼)
  • after return advice, 在一個 join point 正常返回后執行的 advice
  • after throwing advice, 當一個 join point 拋出異常后執行的 advice
  • after(final) advice, 無論一個 join point 是正常退出還是發生了異常, 都會被執行的 advice.
  • around advice, 在 join point 前和 joint point 退出后都執行的 advice. 這個是最常用的 advice.

Pointcut expression

Pointcut通過pointcut expression來描述,有若干種限定詞。具體可以參考Spring文檔7.2.3 Declaring a pointcutaspectj-cheat-sheet.

<a name="Spring AOP的使用"></a>

Spring AOP的使用

我們可以通過三種方式來使用Spring AOP,他們分別是:@Aspect-based(Annotation),Schema-based(XML),以及底層的Spring AOP API。我們后續講解的主要是基于注解的實現。

使用xml的實現原理與使用注解基本一致,基本上只是把注解包含的配置信息都轉移到了xml配置文件中。

至于AOP API則是Spring1.2提供的歷史用法,現在的Sping4也仍然支持,注解與xml也是基于其上的使用。它是SpringAOP的基礎,有興趣的可以去參考鏈接4查看。

<a name="基于注解的使用"></a>

基于注解的使用

添加 @AspectJ 支持

@AspectJ 是一種使用 Java 注解來實現 AOP 的編碼風格.
@AspectJ 風格的 AOP 是 AspectJ Project 在 AspectJ 5 中引入的, 并且 Spring 也支持@AspectJ 的 AOP 風格.

<a name="添加依賴"></a>

添加依賴

 <!-- 5)Spring AOP -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>${spring.version}</version>
  </dependency>
  <!-- aspectj依賴開始 -->
  <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>${aspectj.version}</version>
  </dependency>
  <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>${aspectj.version}</version>
  </dependency>
  <!-- aspectj依賴結束 -->

<a name="開啟 @Aspect 注解"></a>

開啟 @Aspect 注解

    在 spring-mvc.xml 中添加一下語句 用于啟用@Aspect注解
    <aop:aspectj-autoproxy/> //jdk 代理
    或
    <aop:aspectj-autoproxy proxy-target-class="true" /> //cglib 代理

    
    并且需要在xml中加上 aop 的 namespace
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 
            <!-- bean definitions here -->

    </beans>

jdk 代理 與 CGlib 代理
JDK動態代理只能對實現了接口的類生成代理,而不能針對類 。
CGLIB是針對類實現代理,主要是對指定的類生成一個子類,覆蓋其中的 方法 。
因為是繼承,所以該類或方法最好不要聲明成final ,final可以阻止繼承和多態。

<a name="定義 aspect(切面)"></a>

定義 aspect(切面)

當使用注解 @Aspect 標注一個 Bean 后, 那么 Spring 框架會自動收集這些 Bean, 并添加到 Spring AOP 中, 例如:

@Aspect
@Component
public class TestAspect {
}

請注意,@Aspect 不能被 Spring 自動識別并注冊為 Bean;為此,您需要添加一個單獨的 @Component 注釋

<a name="聲明 pointcut"></a>

聲明 pointcut

pointcut 聲明由兩部分組成:

Pointcut簽名(signature) 包括方法名和相關參數

Pointcut表示式(expression) 用來指定哪些方法執行是我們感興趣的(即因此可以織入 advice).


pointcut expression
pointcut expression

這個方法必須無返回值.
這個方法本身就是 pointcut signature, pointcut 表達式使用@Pointcut 注解指定.
上圖定義了一個 pointcut,它所描述的是:匹配在項目路徑 aspects.trace.demo 下的所有方法的執行

<a name="切點標志符"></a>

切點標志符(designator)

具體使用請參考案例代碼

  • execution - for matching method execution join points, this is the primary pointcut designator you will use when working with Spring AOP
  • within - limits matching to join points within certain types (simply the execution of a method declared within a matching type when using Spring AOP)
  • this - limits matching to join points (the execution of methods when using Spring AOP) where the bean reference (Spring AOP proxy) is an instance of the given type
  • target - limits matching to join points (the execution of methods when using Spring AOP) where the target object (application object being proxied) is an instance of the given type
  • args - limits matching to join points (the execution of methods when using Spring AOP) where the arguments are instances of the given types
  • bean - limit the matching of join points to a particular named Spring bean, or to a set of named Spring beans (when using wildcards).
  • @target - limits matching to join points (the execution of methods when using Spring AOP) where the class of the executing object has an annotation of the given type
  • @args - limits matching to join points (the execution of methods when using Spring AOP) where the runtime type of the actual arguments passed have annotations of the given type(s)
  • @within - limits matching to join points within types that have the given annotation (the execution of methods declared in types with the given annotation when using Spring AOP)
  • @annotation - limits matching to join points where the subject of the join point (method being executed in Spring AOP) has the given annotation

<a name="聲明 advice"></a>

聲明 advice

advice 是和一個 pointcut 表達式關聯在一起的, 并且會在匹配的 join point 的方法執行的前/后/周圍 運行. pointcut 表達式可以是簡單的一個 pointcut 名字的引用, 或者是完整的 pointcut 表達式.

* Before

 Before Advice 由切面中的 @Before 注解聲明

 代表 advice 在 joinpoint 之前執行
* After returning

 當匹配的方法正常執行并返回時運行 After returning Advice。

 由 @AfterReturning 注解聲明

* After throwing 
 當匹配的方法執行并拋出異常退出時,運行。
 
 由 @AfterThorwing 注解聲明。

* After (finally)
 當匹配的方法執行完成并退出后執行。
 
 通過 @After 聲明
 
        try{
            try{
                //@Before
                method.invoke(..);
            }finally{
                //@After
            }
            //@AfterReturning
        }catch(){
            //@AfterThrowing
        }

* Around
 它可以在一個方法的之前之前和之后添加不同的操作, 并且甚至可以決定何時, 如何, 是否調用匹配到的方法.
 
 通過 @Around 聲明  

<a name = "代碼地址">

代碼地址

https://github.com/Kcyfrank/SpringMVC-example

<a name = "參考鏈接">

參考鏈接:

  1. 官方文檔 Aspect Oriented Programming with Spring
  2. 徹底征服 Spring AOP 之 理論篇
  3. aspectj-cheat-sheet
    <a name = "參考4">
  4. Spring AOP API介紹
  5. spring-aop-target-vs-this
  6. Spring 之AOP AspectJ切入點語法詳解(最全了,不需要再去其他地找了)
  7. AOP 那點事兒 & 續
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,197評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,415評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,104評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,884評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,647評論 6 408
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,130評論 1 323
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,208評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,366評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,887評論 1 334
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,737評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,939評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,478評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,174評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,586評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,827評論 1 283
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,608評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,914評論 2 372

推薦閱讀更多精彩內容

  • 基本知識 其實, 接觸了這么久的 AOP, 我感覺, AOP 給人難以理解的一個關鍵點是它的概念比較多, 而且坑爹...
    永順閱讀 8,291評論 5 114
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,696評論 0 23
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,781評論 18 139
  • What? As we all know,在進行項目構建時,追求各模塊高內聚,模塊間低耦合。然而現實并不總是如此美...
    MasterNeo閱讀 2,092評論 0 17
  • 幾番冷雨過后春風蕩漾不息拂過山崗湖泊聽過無數故事后在這如水的夜里穿過萬水和千山 我遙望北方燈火心上的人可無恙你也知...
    茶石閱讀 360評論 0 2