因為工作需求,自己去了解一下aop并做下的記錄,當然大部分都是參考他人博客以及官方文檔。
目錄
<a name="關于 AOP"></a>
關于 AOP
大家都知道Spring框架有兩大重要特性,IOC 控制反轉 (Inversion of Control,IoC) 以及AOP 面向切面編程(Aspect Oriented Program, AOP)。今天主要是來一起了解一下AOP。
其主要作用是,在不修改源代碼的情況下給某個或者一組操作添加額外的功能。像日志記錄,事務處理,權限控制等功能,都可以用AOP來“優雅”地實現,使這些額外功能和真正的業務邏輯分離開來,軟件的結構將更加清晰。
簡單來說在運行時,動態地將代碼切入到類的指定方法、指定位置上的編程思想就是面向切面的編程。
<a name = "相關術語"></a>
相關術語
Aspect(切面)
aspect
由pointcut
和advice
組成,它即包含了橫切的定義,也包含了鏈接點的定義。由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 pointcut 或 aspectj-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 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 = "參考鏈接">