成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

你真的確定Spring AOP的執行順序嗎

開發 前端
公司新項目需要搭建一個新的前后分離HTTP服務,我選擇了目前比較熟悉的SpringBoot Web來快速搭建一個可用的系統。

 [[346655]]

前言

公司這兩個月啟動了全新的項目,項目排期滿滿當當,不過該學習還是要學習。這不,給公司搭項目的時候,。

本文內容重點:

  • 問題描述
  • Spring AOP執行順序
  • 探究順序錯誤的真相
  • 代碼驗證
  • 結論

問題描述

公司新項目需要搭建一個新的前后分離HTTP服務,我選擇了目前比較熟悉的SpringBoot Web來快速搭建一個可用的系統。

魯迅說過,不要隨便升級已經穩定使用的版本。我偏不信這個邪,仗著自己用了這么久Spring,怎么能不沖呢。不說了,直接引入了最新的SprinBoot 2.3.4.RELEASE版本,開始給項目搭架子。

起初,大多數的組件引入都一切順利,本以為就要大功告成了,沒想到在搭建日志切面時栽了跟頭。

作為一個接口服務,為了方便查詢接口調用情況和定位問題,一般都會將請求日志打印出來,而Spring的AOP作為切面支持,完美的切合了日志記錄的需求。

之前的項目中,運行正確的切面日志記錄效果如下圖:

 

可以看到圖內的一次方法調用,會輸出請求url,出入參,以及請求IP等等,之前為了好看,還加入了分割線。

我把這個實現類放入新項目中,執行出來卻是這樣的:

 

我揉了揉眼睛,仔細看了看復制過來的老代碼,精簡版如下:

  1. /** 
  2.  * 在切點之前織入 
  3.  * @param joinPoint 
  4.  * @throws Throwable 
  5.  */ 
  6. @Before("webLog()"
  7. public void doBefore(JoinPoint joinPoint) throws Throwable { 
  8.     // 開始打印請求日志 
  9.     ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); 
  10.     HttpServletRequest request = attributes.getRequest(); 
  11.  
  12.     // 初始化traceId 
  13.     initTraceId(request); 
  14.  
  15.     // 打印請求相關參數 
  16.     LOGGER.info("========================================== Start =========================================="); 
  17.     // 打印請求 url 
  18.     LOGGER.info("URL            : {}", request.getRequestURL().toString()); 
  19.     // 打印 Http method 
  20.     LOGGER.info("HTTP Method    : {}", request.getMethod()); 
  21.     // 打印調用 controller 的全路徑以及執行方法 
  22.     LOGGER.info("Class Method   : {}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName()); 
  23.     // 打印請求的 IP 
  24.     LOGGER.info("IP             : {}", IPAddressUtil.getIpAdrress(request)); 
  25.     // 打印請求入參 
  26.     LOGGER.info("Request Args   : {}", joinPoint.getArgs()); 
  27.  
  28. /** 
  29.  * 在切點之后織入 
  30.  * @throws Throwable 
  31.  */ 
  32. @After("webLog()"
  33. public void doAfter() throws Throwable { 
  34.     LOGGER.info("=========================================== End ==========================================="); 
  35.  
  36. /** 
  37.  * 環繞 
  38.  * @param proceedingJoinPoint 
  39.  * @return 
  40.  * @throws Throwable 
  41.  */ 
  42. @Around("webLog()"
  43. public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { 
  44.     long startTime = System.currentTimeMillis(); 
  45.     Object result = proceedingJoinPoint.proceed(); 
  46.     // 打印出參 
  47.     LOGGER.info("Response Args  : {}", result); 
  48.     // 執行耗時 
  49.     LOGGER.info("Time-Consuming : {} ms", System.currentTimeMillis() - startTime); 
  50.     return result; 

代碼感覺完全沒有問題,難道新版本的SpringBoot出Bug了。

顯然,成熟的框架不會在這種大方向上犯錯誤,那會不會是新版本的SpringBoot把@After和@Around的順序反過來了?

其實事情也沒有那么簡單。

Spring AOP執行順序

我們先來回顧下Spring AOP執行順序。

我們在網上查找關于SpringAop執行順序的的資料,大多數時候,你會查到如下的答案:

正常情況

 

異常情況

 

多個切面的情況

 

所以@Around理應在@After之前,但是在SprinBoot 2.3.4.RELEASE版本中,@Around切切實實執行在了@After之后。

當我嘗試切換回2.2.5.RELEASE版本后,執行順序又回到了@Around-->@After

探究順序錯誤的真相

既然知道了是SpringBoot版本升級導致的問題(或者說順序變化),那么就要來看看究竟是哪個庫對AOP執行的順序進行了變動,畢竟,SpringBoot只是“形”,真正的內核在Spring。

我們打開pom.xml文件,使用插件查看spring-aop的版本,發現SpringBoot 2.3.4.RELEASE 版本使用的AOP是spring-aop-5.2.9.RELEASE。

而2.2.5.RELEASE對應的是spring-aop-5.2.4.RELEASE

于是我去官網搜索文檔,不得不說Spring由于過于龐大,官網的文檔已經到了冗雜的地步,不過最終還是找到了:

https://docs.spring.io/spring-framework/docs/5.2.9.RELEASE/spring-framework-reference/core.html#aop-ataspectj-advice-ordering

 

As of Spring Framework 5.2.7, advice methods defined in the same @Aspect class that need to run at the same join point are assigned precedence based on their advice type in the following order, from highest to lowest precedence: @Around, @Before, @After, @AfterReturning, @AfterThrowing.

我粗淺的翻譯一下重點:

從Spring5.2.7開始,在相同@Aspect類中,通知方法將根據其類型按照從高到低的優先級進行執行:@Around,@Before ,@After,@AfterReturning,@AfterThrowing。

這樣看其實對比不明顯,我們再回到老版本,也就是2.2.5.RELEASE對應的spring-aop-5.2.4.RELEASE,當時的文檔是這么寫的:

What happens when multiple pieces of advice all want to run at the same join point? Spring AOP follows the same precedence rules as AspectJ to determine the order of advice execution. The highest precedence advice runs first "on the way in" (so, given two pieces of before advice, the one with highest precedence runs first). "On the way out" from a join point, the highest precedence advice runs last (so, given two pieces of after advice, the one with the highest precedence will run second).

簡單翻譯:在相同@Aspect類中Spring AOP遵循與AspectJ相同的優先級規則來確定advice執行的順序。

再挖深一點,那么AspectJ的優先級規則是什么樣的?

我找了AspectJ的文檔:

https://www.eclipse.org/aspectj/doc/next/progguide/semantics-advice.html

 

At a particular join point, advice is ordered by precedence.

A piece of around advice controls whether advice of lower precedence will run by calling proceed. The call to proceed will run the advice with next precedence, or the computation under the join point if there is no further advice.

A piece of before advice can prevent advice of lower precedence from running by throwing an exception. If it returns normally, however, then the advice of the next precedence, or the computation under the join pint if there is no further advice, will run.

Running after returning advice will run the advice of next precedence, or the computation under the join point if there is no further advice. Then, if that computation returned normally, the body of the advice will run.

Running after throwing advice will run the advice of next precedence, or the computation under the join point if there is no further advice. Then, if that computation threw an exception of an appropriate type, the body of the advice will run.

Running after advice will run the advice of next precedence, or the computation under the join point if there is no further advice. Then the body of the advice will run.

大伙又要說了,哎呀太長不看!簡短地說,Aspectj的規則就是上面我們能夠在網上查閱到的順序圖展示的那樣,依舊是老的順序。

代碼驗證

我把業務邏輯從代碼中刪除,只驗證下這幾個advice的執行順序:

  1. /** 
  2.  * 日志切面 
  3.  */ 
  4. @Aspect 
  5. @Component 
  6. public class WebLogAspect { 
  7.  
  8.     private final static Logger LOGGER = LoggerFactory.getLogger(WebLogAspect.class); 
  9.  
  10.     /** 以 controller 包下定義的所有請求為切入點 */ 
  11.     @Pointcut("execution(public * com.xx.xxx.xxx.controller..*.*(..))"
  12.     public void webLog() {} 
  13.  
  14.     /** 
  15.      * 在切點之前織入 
  16.      * @param joinPoint 
  17.      * @throws Throwable 
  18.      */ 
  19.     @Before("webLog()"
  20.     public void doBefore(JoinPoint joinPoint) throws Throwable { 
  21.         LOGGER.info("-------------doBefore-------------"); 
  22.     } 
  23.  
  24.     @AfterReturning("webLog()"
  25.     public void afterReturning() { 
  26.         LOGGER.info("-------------afterReturning-------------"); 
  27.     } 
  28.     @AfterThrowing("webLog()"
  29.     public void afterThrowing() { 
  30.         LOGGER.info("-------------afterThrowing-------------"); 
  31.     } 
  32.  
  33.     /** 
  34.      * 在切點之后織入 
  35.      * @throws Throwable 
  36.      */ 
  37.     @After("webLog()"
  38.     public void doAfter() throws Throwable { 
  39.         LOGGER.info("-------------doAfter-------------"); 
  40.     } 
  41.  
  42.     /** 
  43.      * 環繞 
  44.      * @param proceedingJoinPoint 
  45.      * @return 
  46.      * @throws Throwable 
  47.      */ 
  48.     @Around("webLog()"
  49.     public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { 
  50.         long startTime = System.currentTimeMillis(); 
  51.         LOGGER.info("-------------doAround before proceed-------------"); 
  52.         Object result = proceedingJoinPoint.proceed(); 
  53.         LOGGER.info("-------------doAround after proceed-------------"); 
  54.         return result; 
  55.     } 

我們將版本改為2.2.5.RELEASE,結果如圖:

 

我們將版本改為2.3.4.RELEASE,結果如圖:

 

結論

經過上面的資料文檔查閱,我能給出的結論是:

從Spring5.2.7開始,Spring AOP不再嚴格按照AspectJ定義的規則來執行advice,而是根據其類型按照從高到低的優先級進行執行:@Around,@Before ,@After,@AfterReturning,@AfterThrowing。

這次的研究思考十分倉促,如果結論有誤請大家踴躍指正,也歡迎大家自己嘗試,畢竟口說無憑,實驗室檢驗真理的唯一標準!

參考

https://www.cnblogs.com/dennyLee2025/p/13724981.html

 

https://segmentfault.com/a/1190000011283029

本文轉載自微信公眾號「后端技術漫談」,可以通過以下二維碼關注。轉載本文請聯系后端技術漫談公眾號。

 

責任編輯:武曉燕 來源: 后端技術漫談
相關推薦

2012-03-01 11:20:45

2016-04-20 09:49:25

網絡測試網絡優化

2022-02-16 13:46:40

Spring Aop代碼注解

2016-01-07 11:18:50

用戶畫像

2024-04-19 13:17:40

PostgreSQLSQL數據庫

2025-01-07 09:16:16

2016-06-01 15:42:58

Hadoop數據管理分布式

2020-04-17 14:25:22

Kubernetes應用程序軟件開發

2014-04-17 16:42:03

DevOps

2022-07-26 00:00:22

HTAP系統數據庫

2021-02-01 13:59:47

比特幣區塊鏈安全

2023-12-27 14:04:00

Spring框架參數

2025-01-03 08:09:15

2022-09-22 14:55:31

前端JavaScripthis

2022-09-26 13:10:17

JavaScriptthis

2024-01-08 08:45:07

Spring容器Bean

2021-06-08 11:15:10

Redis數據庫命令

2021-11-09 09:48:13

Logging python模塊

2022-08-15 10:42:50

千兆網絡千兆光纖

2021-01-15 07:44:21

SQL注入攻擊黑客
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 99久久99| 成人免费视频网站在线观看 | 精品一区二区三区中文字幕 | 欧美aaaa视频 | 久久r久久 | 五月激情六月婷婷 | 女女百合av大片一区二区三区九县 | 欧美一级久久 | 人人性人人性碰国产 | 91视频国产精品 | 国产香蕉视频 | 成人av观看| 色天天综合 | 一区二区三区精品视频 | 婷婷综合五月天 | 日韩av美女电影 | 好姑娘影视在线观看高清 | 日韩视频在线一区二区 | 日韩a| 国产精品日韩在线观看一区二区 | av黄色网| 午夜视频免费 | 欧美xxxx在线 | 日韩av成人 | 国产成人在线看 | 日韩欧美一区二区在线播放 | 日韩中文字幕 | 久草在线高清 | 国产一区二区自拍 | 鸳鸯谱在线观看高清 | 国产内谢| 91精品国产一区二区在线观看 | 91在线一区二区 | 国产一区二区在线91 | wwwxx在线观看 | 久久精品视频播放 | 91精品国产高清一区二区三区 | 99自拍视频 | 国产丝袜人妖cd露出 | 亚洲在线久久 | 久久91精品国产 |