最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

聊聊Spring AOP @Before @Around @After等advice的執(zhí)行順序

 更新時(shí)間:2021年02月19日 12:01:31   作者:rainbow702  
這篇文章主要介紹了聊聊Spring AOP @Before @Around @After等advice的執(zhí)行順序,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

用過spring框架進(jìn)行開發(fā)的人,多多少少會(huì)使用過它的AOP功能,都知道有@Before、@Around和@After等advice。

最近,為了實(shí)現(xiàn)項(xiàng)目中的輸出日志和權(quán)限控制這兩個(gè)需求,我也使用到了AOP功能。

我使用到了@Before、@Around這兩個(gè)advice。但在,使用過程中,卻對它們的執(zhí)行順序并不清楚。

為了弄清楚在不同情況下,這些advice到底是以怎么樣的一個(gè)順序進(jìn)行執(zhí)行的,我作了個(gè)測試,在此將其記錄下來,以供以后查看。

前提

對于AOP相關(guān)類(aspect、pointcut等)的概念,本文不作說明。

對于如何讓spring框架掃描到AOP,本文也不作說明。

情況一: 一個(gè)方法只被一個(gè)Aspect類攔截

當(dāng)一個(gè)方法只被一個(gè)Aspect攔截時(shí),這個(gè)Aspect中的不同advice是按照怎樣的順序進(jìn)行執(zhí)行的呢?請看:

添加 PointCut類

該pointcut用來攔截test包下的所有類中的所有方法。

package test;
import org.aspectj.lang.annotation.Pointcut;
public class PointCuts {
 @Pointcut(value = "within(test.*)")
 public void aopDemo() {
 }
}

添加Aspect類

該類中的advice將會(huì)用到上面的pointcut,使用方法請看各個(gè)advice的value屬性。

package test;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class Aspect1 {
 @Before(value = "test.PointCuts.aopDemo()")
 public void before(JoinPoint joinPoint) {
  System.out.println("[Aspect1] before advise");
 }
 @Around(value = "test.PointCuts.aopDemo()")
 public void around(ProceedingJoinPoint pjp) throws Throwable{
  System.out.println("[Aspect1] around advise 1");
  pjp.proceed();
  System.out.println("[Aspect1] around advise2");
 }
 @AfterReturning(value = "test.PointCuts.aopDemo()")
 public void afterReturning(JoinPoint joinPoint) {
  System.out.println("[Aspect1] afterReturning advise");
 }
 @AfterThrowing(value = "test.PointCuts.aopDemo()")
 public void afterThrowing(JoinPoint joinPoint) {
  System.out.println("[Aspect1] afterThrowing advise");
 }
 @After(value = "test.PointCuts.aopDemo()")
 public void after(JoinPoint joinPoint) {
  System.out.println("[Aspect1] after advise");
 }
}

添加測試用Controller

添加一個(gè)用于測試的controller,這個(gè)controller中只有一個(gè)方法,但是它會(huì)根據(jù)參數(shù)值的不同,會(huì)作出不同的處理:一種是正常返回一個(gè)對象,一種是拋出異常(因?yàn)槲覀円獪y試@AfterThrowing這個(gè)advice)

package test;
import test.exception.TestException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping(value = "/aop")
public class AopTestController {
 @ResponseStatus(HttpStatus.OK)
 @RequestMapping(value = "/test", method = RequestMethod.GET)
 public Result test(@RequestParam boolean throwException) {
  // case 1
  if (throwException) {
   System.out.println("throw an exception");
   throw new TestException("mock a server exception");
  }
  // case 2
  System.out.println("test OK");
  return new Result() {{
   this.setId(111);
   this.setName("mock a Result");
  }};
 }
 public static class Result {
  private int id;
  private String name;
  public int getId() {
   return id;
  }
  public void setId(int id) {
   this.id = id;
  }
  public String getName() {
   return name;
  }
  public void setName(String name) {
   this.name = name;
  }
 }
}

測試 正常情況

在瀏覽器直接輸入以下的URL,回車:

http://192.168.142.8:7070/aoptest/v1/aop/test?throwException=false

我們會(huì)看到輸出的結(jié)果是:

[Aspect1] around advise 1
[Aspect1] before advise
test OK
[Aspect1] around advise2
[Aspect1] after advise
[Aspect1] afterReturning advise

測試 異常情況

在瀏覽器中直接輸入以下的URL,回車:

http://192.168.142.8:7070/aoptest/v1/aop/test?throwException=true

我們會(huì)看到輸出的結(jié)果是:

[Aspect1] around advise 1
[Aspect1] before advise
throw an exception
[Aspect1] after advise
[Aspect1] afterThrowing advise

結(jié)論

在一個(gè)方法只被一個(gè)aspect類攔截時(shí),aspect類內(nèi)部的 advice 將按照以下的順序進(jìn)行執(zhí)行:

正常情況:

異常情況:

情況二: 同一個(gè)方法被多個(gè)Aspect類攔截

此處舉例為被兩個(gè)aspect類攔截。

有些情況下,對于兩個(gè)不同的aspect類,不管它們的advice使用的是同一個(gè)pointcut,還是不同的pointcut,都有可能導(dǎo)致同一個(gè)方法被多個(gè)aspect類攔截。那么,在這種情況下,這多個(gè)Aspect類中的advice又是按照怎樣的順序進(jìn)行執(zhí)行的呢?請看:

pointcut類保持不變

添加一個(gè)新的aspect類

package test;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class Aspect2 {
 @Before(value = "test.PointCuts.aopDemo()")
 public void before(JoinPoint joinPoint) {
  System.out.println("[Aspect2] before advise");
 }
 @Around(value = "test.PointCuts.aopDemo()")
 public void around(ProceedingJoinPoint pjp) throws Throwable{
  System.out.println("[Aspect2] around advise 1");
  pjp.proceed();
  System.out.println("[Aspect2] around advise2");
 }
 @AfterReturning(value = "test.PointCuts.aopDemo()")
 public void afterReturning(JoinPoint joinPoint) {
  System.out.println("[Aspect2] afterReturning advise");
 }
 @AfterThrowing(value = "test.PointCuts.aopDemo()")
 public void afterThrowing(JoinPoint joinPoint) {
  System.out.println("[Aspect2] afterThrowing advise");
 }
 @After(value = "test.PointCuts.aopDemo()")
 public void after(JoinPoint joinPoint) {
  System.out.println("[Aspect2] after advise");
 }
}

測試用Controller也不變

還是使用上面的那個(gè)Controller。但是現(xiàn)在 aspect1 和 aspect2 都會(huì)攔截該controller中的方法。

下面繼續(xù)進(jìn)行測試!

測試 正常情況

在瀏覽器直接輸入以下的URL,回車:

http://192.168.142.8:7070/aoptest/v1/aop/test?throwException=false

我們會(huì)看到輸出的結(jié)果是:

[Aspect2] around advise 1
[Aspect2] before advise
[Aspect1] around advise 1
[Aspect1] before advise
test OK
[Aspect1] around advise2
[Aspect1] after advise
[Aspect1] afterReturning advise
[Aspect2] around advise2
[Aspect2] after advise
[Aspect2] afterReturning advise

但是這個(gè)時(shí)候,我不能下定論說 aspect2 肯定就比 aspect1 先執(zhí)行。

不信?你把服務(wù)務(wù)器重新啟動(dòng)一下,再試試,說不定你就會(huì)看到如下的執(zhí)行結(jié)果:

[Aspect1] around advise 1
[Aspect1] before advise
[Aspect2] around advise 1
[Aspect2] before advise
test OK
[Aspect2] around advise2
[Aspect2] after advise
[Aspect2] afterReturning advise
[Aspect1] around advise2
[Aspect1] after advise
[Aspect1] afterReturning advise

也就是說,這種情況下, aspect1 和 aspect2 的執(zhí)行順序是未知的。那怎么解決呢?不急,下面會(huì)給出解決方案。

測試 異常情況

在瀏覽器中直接輸入以下的URL,回車:

http://192.168.142.8:7070/aoptest/v1/aop/test?throwException=true

我們會(huì)看到輸出的結(jié)果是:

[Aspect2] around advise 1
[Aspect2] before advise
[Aspect1] around advise 1
[Aspect1] before advise
throw an exception
[Aspect1] after advise
[Aspect1] afterThrowing advise
[Aspect2] after advise
[Aspect2] afterThrowing advise

同樣地,如果把服務(wù)器重啟,然后再測試的話,就可能會(huì)看到如下的結(jié)果:

[Aspect1] around advise 1
[Aspect1] before advise
[Aspect2] around advise 1
[Aspect2] before advise
throw an exception
[Aspect2] after advise
[Aspect2] afterThrowing advise
[Aspect1] after advise
[Aspect1] afterThrowing advise

也就是說,同樣地,異常情況下, aspect1 和 aspect2 的執(zhí)行順序也是未定的。

那么在 情況二 下,如何指定每個(gè) aspect 的執(zhí)行順序呢?

方法有兩種:

實(shí)現(xiàn)org.springframework.core.Ordered接口,實(shí)現(xiàn)它的getOrder()方法

給aspect添加@Order注解,該注解全稱為:org.springframework.core.annotation.Order

不管采用上面的哪種方法,都是值越小的 aspect 越先執(zhí)行。

比如,我們?yōu)?apsect1 和 aspect2 分別添加 @Order 注解,如下:

@Order(5)
@Component
@Aspect
public class Aspect1 {
 // ...
}
@Order(6)
@Component
@Aspect
public class Aspect2 {
 // ...
}

這樣修改之后,可保證不管在任何情況下, aspect1 中的 advice 總是比 aspect2 中的 advice 先執(zhí)行。

如下圖所示:

注意點(diǎn)

如果在同一個(gè) aspect 類中,針對同一個(gè) pointcut,定義了兩個(gè)相同的 advice(比如,定義了兩個(gè) @Before),那么這兩個(gè) advice 的執(zhí)行順序是無法確定的,哪怕你給這兩個(gè) advice 添加了 @Order 這個(gè)注解,也不行。這點(diǎn)切記。

對于@Around這個(gè)advice,不管它有沒有返回值,但是必須要方法內(nèi)部,調(diào)用一下 pjp.proceed();否則,Controller 中的接口將沒有機(jī)會(huì)被執(zhí)行,從而也導(dǎo)致了 @Before這個(gè)advice不會(huì)被觸發(fā)。

比如,我們假設(shè)正常情況下,執(zhí)行順序?yàn)椤盿spect2 -> apsect1 -> controller”,如果,我們把 aspect1中的@Around中的 pjp.proceed();給刪掉,那么,我們看到的輸出結(jié)果將是:

[Aspect2] around advise 1
[Aspect2] before advise
[Aspect1] around advise 1
[Aspect1] around advise2
[Aspect1] after advise
[Aspect1] afterReturning advise
[Aspect2] around advise2
[Aspect2] after advise
[Aspect2] afterReturning advise

從結(jié)果可以發(fā)現(xiàn), Controller 中的 接口 未被執(zhí)行,aspect1 中的 @Before advice 也未被執(zhí)行。

參考資料

Spring 4.3.2.RELEASE 官方資料:http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/

其中,AOP的執(zhí)行順序章節(jié)為:http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#aop-ataspectj-advice-ordering

Advice ordering

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).

When two pieces of advice defined in different aspects both need to run at the same join point, unless you specify otherwise the order of execution is undefined.

You can control the order of execution by specifying precedence.

This is done in the normal Spring way by either implementing the org.springframework.core.Ordered interface in the aspect class or annotating it with the Order annotation.

Given two aspects, the aspect returning the lower value from Ordered.getValue() (or the annotation value) has the higher precedence.

When two pieces of advice defined in the same aspect both need to run at the same join point, the ordering is undefined (since there is no way to retrieve the declaration order via reflection for javac-compiled classes).

Consider collapsing such advice methods into one advice method per join point in each aspect class, or refactor the pieces of advice into separate aspect classes - which can be ordered at the aspect level.

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • SpringBoot自定義Starter的教程指南

    SpringBoot自定義Starter的教程指南

    SpringBoot的Starter自動(dòng)配置機(jī)制極大地簡化了依賴管理和應(yīng)用配置,使得開發(fā)者可以以最少的配置快速啟動(dòng)和運(yùn)行Spring應(yīng)用,有時(shí),標(biāo)準(zhǔn)的Starter可能無法滿足特定需求,需要?jiǎng)?chuàng)建自定義Starter,所以本文給大家介紹了SpringBoot自定義Starter的教程指南
    2024-11-11
  • SpringAop中的Advice通知實(shí)例

    SpringAop中的Advice通知實(shí)例

    這篇文章主要介紹了SpringAop中的Advice通知詳解,Spring的AOP功能中一個(gè)關(guān)鍵概念是通知Advice與切點(diǎn)Pointcut表達(dá)式相關(guān)聯(lián)在特定節(jié)點(diǎn)織入一些邏輯,Spring提供了五種類型的通知,需要的朋友可以參考下
    2023-09-09
  • Java實(shí)現(xiàn)XML與JSON的互相轉(zhuǎn)換詳解

    Java實(shí)現(xiàn)XML與JSON的互相轉(zhuǎn)換詳解

    這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)XML與JSON的互相轉(zhuǎn)換,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-03-03
  • IDEA創(chuàng)建Java?Web項(xiàng)目的超詳細(xì)圖文教學(xué)

    IDEA創(chuàng)建Java?Web項(xiàng)目的超詳細(xì)圖文教學(xué)

    IDEA是程序員們常用的java集成開發(fā)環(huán)境,也是被公認(rèn)為最好用的java開發(fā)工具,下面這篇文章主要給大家介紹了關(guān)于IDEA創(chuàng)建Java?Web項(xiàng)目的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12
  • SpringBoot下載文件的正確解法方式

    SpringBoot下載文件的正確解法方式

    這篇文章主要給大家介紹了關(guān)于SpringBoot下載文件的正確解法方式,SpringBoot是一款流行的框架,用于開發(fā)Web應(yīng)用程序,在使用SpringBoot構(gòu)建Web應(yīng)用程序時(shí),可能需要實(shí)現(xiàn)文件下載的功能,需要的朋友可以參考下
    2023-08-08
  • Java 網(wǎng)絡(luò)編程總結(jié)

    Java 網(wǎng)絡(luò)編程總結(jié)

    這篇文章主要給大家分享Java 網(wǎng)絡(luò)編程的一個(gè)總結(jié),說到網(wǎng)絡(luò)編程肯定都會(huì)想到IP地址、端口、通信協(xié)議等一些必不可少的元素,下面來看看文章的詳細(xì)介紹吧
    2021-11-11
  • springboot參數(shù)傳中文亂碼的解決方案

    springboot參數(shù)傳中文亂碼的解決方案

    這篇文章主要介紹了springboot參數(shù)傳中文亂碼的解決方案,幫助大家更好的理解和學(xué)習(xí)使用springboot,感興趣的朋友可以了解下
    2021-03-03
  • Flowable中定時(shí)器的玩法詳解

    Flowable中定時(shí)器的玩法詳解

    這篇文章主要為大家詳細(xì)介紹了Flowable中定時(shí)器的各種玩法,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起了解一下
    2022-11-11
  • spring事務(wù)的propagation傳播屬性示例詳解

    spring事務(wù)的propagation傳播屬性示例詳解

    這篇文章主要為大家介紹了spring事務(wù)的propagation傳播屬性示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • Spring中Bean掃描原理詳情

    Spring中Bean掃描原理詳情

    這篇文章主要介紹了Spring中Bean掃描原理詳情,文章為榮啊主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-07-07

最新評論

阿拉善右旗| 临澧县| 遵化市| 铜陵市| 孝义市| 定南县| 南召县| 泌阳县| 镇坪县| 司法| 乐至县| 四川省| 筠连县| 聊城市| 会理县| 漯河市| 驻马店市| 霍邱县| 临西县| 城市| 西充县| 南汇区| 那曲县| 万年县| 峨边| 西峡县| 三原县| 三门县| 亚东县| 安远县| 三亚市| 滨州市| 铜梁县| 五寨县| 登封市| 高雄市| 册亨县| 连南| 绥德县| 富川| 虎林市|