Spring AOP 的簡(jiǎn)單應(yīng)用

因?yàn)楣ぷ餍枨?,自己去了解一下aop并做下的記錄,當(dāng)然大部分都是參考他人博客以及官方文檔。

目錄

<a name="關(guān)于 AOP"></a>

關(guān)于 AOP

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

其主要作用是,在不修改源代碼的情況下給某個(gè)或者一組操作添加額外的功能。像日志記錄,事務(wù)處理,權(quán)限控制等功能,都可以用AOP來(lái)“優(yōu)雅”地實(shí)現(xiàn),使這些額外功能和真正的業(yè)務(wù)邏輯分離開來(lái),軟件的結(jié)構(gòu)將更加清晰。

簡(jiǎn)單來(lái)說(shuō)在運(yùn)行時(shí),動(dòng)態(tài)地將代碼切入到類的指定方法、指定位置上的編程思想就是面向切面的編程。

<a name = "相關(guān)術(shù)語(yǔ)"></a>

相關(guān)術(shù)語(yǔ)

Aspect(切面)

aspectpointcutadvice組成,它即包含了橫切的定義,也包含了鏈接點(diǎn)的定義。由Spring AOP負(fù)責(zé)實(shí)施切面,它將切面所定義的橫切邏輯織入到切面所指定的鏈接點(diǎn)中。

簡(jiǎn)單來(lái)說(shuō),只要在類上有 @Aspect 注解的類就是切面。

Join point(鏈接點(diǎn)/記錄點(diǎn))

程序運(yùn)行中的一個(gè)點(diǎn),例如一個(gè)運(yùn)行方法或者異常處理。

在Spring AOP中,一個(gè)join point總是一個(gè)運(yùn)行方法,即只有方法才是連接點(diǎn)。

advice (增強(qiáng)/通知)

在join point(即滿足 point cut 規(guī)則的join point)上特定的時(shí)刻執(zhí)行的操作,Advice有幾種不同類型,下文將會(huì)討論(通俗地來(lái)講就是起作用的內(nèi)容和時(shí)間點(diǎn))。

Pointcut(切點(diǎn))

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

advice 與 pointcut 表達(dá)式相關(guān)聯(lián),并在與 pointcut 匹配的任意 joinpoint 運(yùn)行(例如,執(zhí)行具有特定名稱的方法)。

簡(jiǎn)單來(lái)說(shuō) pointcut 是一個(gè)joinpoint 的過(guò)濾器,只有滿足 pointcut 的規(guī)則的 joinpoint 才會(huì)執(zhí)行 advice。

Introduction

為一個(gè)類型添加額外的方法或字段. Spring AOP 允許我們?yōu)?目標(biāo)對(duì)象 引入新的接口(和對(duì)應(yīng)的實(shí)現(xiàn)). 例如我們可以使用 introduction 來(lái)為一個(gè) bean 實(shí)現(xiàn) IsModified 接口, 并以此來(lái)簡(jiǎn)化 caching 的實(shí)現(xiàn).

Target object

織入一個(gè)或多個(gè) advice 的目標(biāo)對(duì)象. 目標(biāo)對(duì)象也被稱為 advised object.

因?yàn)?Spring AOP 使用運(yùn)行時(shí)代理的方式來(lái)實(shí)現(xiàn) aspect, 因此 adviced object 總是一個(gè)代理對(duì)象(proxied object)
注意, adviced object 指的不是原來(lái)的類, 而是織入 advice 后所產(chǎn)生的代理類.

AOP proxy

一個(gè)類被 AOP 織入 advice, 就會(huì)產(chǎn)生一個(gè)結(jié)果類, 它是融合了原類和增強(qiáng)邏輯的代理類.在 Spring AOP 中, AOP 代理將是一個(gè) JDK 動(dòng)態(tài)代理對(duì)象或 CGLIB 代理對(duì)象.

Weaving (織入)

將 aspect 和其他對(duì)象連接起來(lái), 并創(chuàng)建 adviced object 的過(guò)程.根據(jù)不同的實(shí)現(xiàn)技術(shù), AOP織入有三種方式:

  • 編譯器織入, 這要求有特殊的Java編譯器.
  • 類裝載期織入, 這需要有特殊的類裝載器.
  • 動(dòng)態(tài)代理織入, 在運(yùn)行期為目標(biāo)類添加增強(qiáng)(Advice)生成子類的方式.

與其他純Java AOP框架一樣,Spring AOP在運(yùn)行時(shí)執(zhí)行編織。

advice 的類型

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

Pointcut expression

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

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

Spring AOP的使用

我們可以通過(guò)三種方式來(lái)使用Spring AOP,他們分別是:@Aspect-based(Annotation),Schema-based(XML),以及底層的Spring AOP API。我們后續(xù)講解的主要是基于注解的實(shí)現(xiàn)。

使用xml的實(shí)現(xiàn)原理與使用注解基本一致,基本上只是把注解包含的配置信息都轉(zhuǎn)移到了xml配置文件中。

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

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

基于注解的使用

添加 @AspectJ 支持

@AspectJ 是一種使用 Java 注解來(lái)實(shí)現(xiàn) AOP 的編碼風(fēng)格.
@AspectJ 風(fēng)格的 AOP 是 AspectJ Project 在 AspectJ 5 中引入的, 并且 Spring 也支持@AspectJ 的 AOP 風(fēng)格.

<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依賴結(jié)束 -->

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

開啟 @Aspect 注解

    在 spring-mvc.xml 中添加一下語(yǔ)句 用于啟用@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動(dòng)態(tài)代理只能對(duì)實(shí)現(xiàn)了接口的類生成代理,而不能針對(duì)類 。
CGLIB是針對(duì)類實(shí)現(xiàn)代理,主要是對(duì)指定的類生成一個(gè)子類,覆蓋其中的 方法 。
因?yàn)槭抢^承,所以該類或方法最好不要聲明成final ,final可以阻止繼承和多態(tài)。

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

定義 aspect(切面)

當(dāng)使用注解 @Aspect 標(biāo)注一個(gè) Bean 后, 那么 Spring 框架會(huì)自動(dòng)收集這些 Bean, 并添加到 Spring AOP 中, 例如:

@Aspect
@Component
public class TestAspect {
}

請(qǐng)注意,@Aspect 不能被 Spring 自動(dòng)識(shí)別并注冊(cè)為 Bean;為此,您需要添加一個(gè)單獨(dú)的 @Component 注釋

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

聲明 pointcut

pointcut 聲明由兩部分組成:

Pointcut簽名(signature) 包括方法名和相關(guān)參數(shù)

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


pointcut expression
pointcut expression

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

<a name="切點(diǎn)標(biāo)志符"></a>

切點(diǎn)標(biāo)志符(designator)

具體使用請(qǐng)參考案例代碼

  • 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 是和一個(gè) pointcut 表達(dá)式關(guān)聯(lián)在一起的, 并且會(huì)在匹配的 join point 的方法執(zhí)行的前/后/周圍 運(yùn)行. pointcut 表達(dá)式可以是簡(jiǎn)單的一個(gè) pointcut 名字的引用, 或者是完整的 pointcut 表達(dá)式.

* Before

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

 代表 advice 在 joinpoint 之前執(zhí)行
* After returning

 當(dāng)匹配的方法正常執(zhí)行并返回時(shí)運(yùn)行 After returning Advice。

 由 @AfterReturning 注解聲明

* After throwing 
 當(dāng)匹配的方法執(zhí)行并拋出異常退出時(shí),運(yùn)行。
 
 由 @AfterThorwing 注解聲明。

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

* Around
 它可以在一個(gè)方法的之前之前和之后添加不同的操作, 并且甚至可以決定何時(shí), 如何, 是否調(diào)用匹配到的方法.
 
 通過(guò) @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切入點(diǎn)語(yǔ)法詳解(最全了,不需要再去其他地找了)
  7. AOP 那點(diǎn)事兒 & 續(xù)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容