SpringBoot基礎篇AOP之基本使用姿勢小結
原文: 190301-SpringBoot基礎篇AOP之基本使用姿勢小結
一般來講,談到Spring的特性,繞不過去的就是DI(依賴注入)和AOP(切面),在將bean的系列中,說了DI的多種使用姿勢;接下來看一下AOP的玩法
<!-- more -->
I. 背景知識
在實際使用之前有必要了解一下什麼是AOP,以及AOP的幾個基本概念
1. advice
- before: 在方法執行之前被呼叫
- after: 在方法執行之後呼叫
- after returning: 方法執行成功之後
- after throwing: 方法丟擲異常之後
- around: 環繞,自己在內部決定方法的執行時機,因此可以在之前之後做一些業務邏輯
2. join point
連線點,比如方法呼叫,方法執行,欄位設定/獲取、異常處理執行、類初始化、甚至是 for 迴圈中的某個點
但 Spring AOP 目前僅支援方法執行 (method execution)
簡單來說,Spring AOP中,PointCut就是那個被攔截的方法
3. pointcut
切點,用來描述滿足什麼規則的方法會被攔截
@Before("execution(public * com.git.hui.demo.base.bean.*.*(..))") @Around("@annotation(parameterCheck)")
4. aspect
切面是切點和通知的結合。通知和切點共同定義了關於切面的全部內容,它是什麼時候,在何時和何處完成功能
5. introduction
引入允許我們向現有的類新增新的方法或者屬性
6. weaving
組裝方面來建立一個被通知物件。這可以在編譯時完成(例如使用AspectJ編譯器),也可以在執行時完成。Spring和其他純Java AOP框架一樣,在執行時完成織入。
簡單來講就是生成一個代理類,在呼叫被攔截的方法時,實際上執行的是代理類,這個代理類內部執行切面邏輯
II. 使用說明
1. 基本配置
首先是基本環境的搭建, 先貼上必要的xml配置, 使用aop需要引入包: spring-boot-starter-aop
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </pluginManagement> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
2. 程式碼準備
首先建立一個被攔截的bean: com.git.hui.boot.aop.demo.DemoBean
,如下
@Component public class DemoBean { /** * 返回隨機的字串 * * @param time * @return */ public String randUUID(long time) { try { System.out.println("in randUUID before process!"); return UUID.randomUUID() + "|" + time; } finally { System.out.println("in randUUID finally!"); } } }
接著在啟動類中,執行
@SpringBootApplication public class Application { public Application(DemoBean demoBean) { String ans = demoBean.randUUID(System.currentTimeMillis()); System.out.println("----- ans: " + ans + "---------"); } public static void main(String[] args) { SpringApplication.run(Application.class); } }
3. AOP使用
在實際使用之前,需要建立一個切面,用 @Aspect
宣告,其次切面也需要作為bean託付給Spring容器管理
@Aspect @Component public class AnoAspcet { }
a. before
在方法呼叫之前,需要執行一些操作,這個時候可以使用 @Before
註解來宣告 before advice
一種可使用姿勢如下,我們的切點直接在註解中進行定義,使用正則表示式的方式
@Before("execution(public * com.git.hui.boot.aop.demo.*.*(*))") public void doBefore(JoinPoint joinPoint) { System.out.println("do in Aspect before method called! args: " + JSON.toJSONString(joinPoint.getArgs())); }
b. after
在方法呼叫完畢之後,再執行一些操作,這個時候after就可以派上用場,為了考慮切點的通用性,我們可以考慮宣告一個切點,使用 @Pointcut
註解
@Pointcut("execution(public * com.git.hui.boot.aop.demo.*.*(*))") public void point() { }
使用pointcut的方式也比較簡單,如下
@After("point()") public void doAfter(JoinPoint joinPoint) { System.out.println("do in Aspect after method called! args: " + JSON.toJSONString(joinPoint.getArgs())); }
c. after returning
在正常返回結果之後,再次執行,這個也挺有意思的,通常使用這個advice時,一般希望獲取返回結果,那麼應該怎麼處理呢?
org.aspectj.lang.annotation.AfterReturning#returning
/** * 執行完畢之後,通過 args指定引數;通過 returning 指定返回的結果,要求返回值型別匹配 * * @param time * @param result */ @AfterReturning(value = "point() && args(time)", returning = "result") public void doAfterReturning(long time, String result) { System.out.println("do in Aspect after method return! args: " + time + " ans: " + result); }
d. around
這個也比較常見,在方法執行前後幹一些事情,比如常見的耗時統計,日誌列印,安全控制等,很多都是基於around advice實現的
使用這個advice需要注意的是傳入引數型別為 ProceedingJoinPoint
,需要在方法內部顯示執行 org.aspectj.lang.ProceedingJoinPoint#proceed()
來表示呼叫方法
@Around("point()") public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("do in Aspect around ------ before"); Object ans = joinPoint.proceed(); System.out.println("do in Aspect around ------- over! ans: " + ans); return ans; }
e. 輸出
執行之後輸出如下
do in Aspect around ------ before do in Aspect before method called! args: [1551433188205] in randUUID before process! in randUUID finally! do in Aspect around ------- over! ans: 6849544b-160e-464c-80bd-641f2651c6c1|1551433188205 do in Aspect after method called! args: [1551433188205] do in Aspect after method return! args: 1551433188205 ans: 6849544b-160e-464c-80bd-641f2651c6c1|1551433188205 ----- ans: 6849544b-160e-464c-80bd-641f2651c6c1|1551433188205---------
從輸出結果上,可以看到每個advice的使用範圍,當然也帶來了一些疑問
- 可以存在多個同類型的advice,攔截同一個目標嗎?(如兩個around都攔截methodA方法,那麼methodA方法被呼叫時,兩個around advice是否都會執行)
- 多個advice之間的優先順序怎麼定義?
- aop攔截的目標方法有沒有限制(對非public的方法可以攔截麼?)
- 被攔截的方法中存在相互呼叫的時候,會怎樣?(如methodA,methodB都可以被攔截,且methodA中呼叫了methodB,那麼在執行methodA時,methodB的各種advice是否會被觸發?)
- 基於註解的aop方式可以怎樣用
以上這些問題留在下一篇進行介紹
III. 其他
0. 專案
- 工程: https://github.com/liuyueyi/spring-boot-demo
- 專案: https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/010-aop
1. 一灰灰Blog
- 一灰灰Blog個人部落格 https://blog.hhui.top
- 一灰灰Blog-Spring專題部落格 http://spring.hhui.top
一灰灰的個人部落格,記錄所有學習和工作中的博文,歡迎大家前去逛逛
2. 宣告
盡信書則不如,以上內容,純屬一家之言,因個人能力有限,難免有疏漏和錯誤之處,如發現bug或者有更好的建議,歡迎批評指正,不吝感激
- 微博地址:小灰灰Blog
- QQ: 一灰灰/3302797840
3. 掃描關注
一灰灰blog
知識星球