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

淺談spring aop的五種通知類(lèi)型

 更新時(shí)間:2017年12月05日 11:19:04   作者:C__joy  
這篇文章主要介紹了淺談spring aop的五種通知類(lèi)型,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

spring aop通知(advice)分成五類(lèi): 

前置通知[Before advice]:在連接點(diǎn)前面執(zhí)行,前置通知不會(huì)影響連接點(diǎn)的執(zhí)行,除非此處拋出異常。 

正常返回通知[After returning advice]:在連接點(diǎn)正常執(zhí)行完成后執(zhí)行,如果連接點(diǎn)拋出異常,則不會(huì)執(zhí)行。 

異常返回通知[After throwing advice]:在連接點(diǎn)拋出異常后執(zhí)行。 

返回通知[After (finally) advice]:在連接點(diǎn)執(zhí)行完成后執(zhí)行,不管是正常執(zhí)行完成,還是拋出異常,都會(huì)執(zhí)行返回通知中的內(nèi)容。 

環(huán)繞通知[Around advice]:環(huán)繞通知圍繞在連接點(diǎn)前后,比如一個(gè)方法調(diào)用的前后。這是最強(qiáng)大的通知類(lèi)型,能在方法調(diào)用前后自定義一些操作。

環(huán)繞通知還需要負(fù)責(zé)決定是繼續(xù)處理join point(調(diào)用ProceedingJoinPoint的proceed方法)還是中斷執(zhí)行。 
接下來(lái)通過(guò)編寫(xiě)示例程序來(lái)測(cè)試一下五種通知類(lèi)型:

定義接口

package com.chenqa.springaop.example.service;

public interface BankService {

  /**
   * 模擬的銀行轉(zhuǎn)賬
   * @param from 出賬人
   * @param to 入賬人
   * @param account 轉(zhuǎn)賬金額
   * @return
   */
  public boolean transfer(String form, String to, double account);
}

編寫(xiě)實(shí)現(xiàn)類(lèi)

package com.chenqa.springaop.example.service.impl;

import com.chenqa.springaop.example.service.BankService;

public class BCMBankServiceImpl implements BankService {

  public boolean transfer(String form, String to, double account) {
    if(account<100) {
      throw new IllegalArgumentException("最低轉(zhuǎn)賬金額不能低于100元");
    }
    System.out.println(form+"向"+to+"交行賬戶轉(zhuǎn)賬"+account+"元");
    return false;
  }

}

修改spring配置文件,添加以下內(nèi)容:

<!-- bankService bean -->  
  <bean id="bankService" class="com.chenqa.springaop.example.service.impl.BCMBankServiceImpl"/>
  <!-- 切面 -->
  <bean id="myAspect" class="com.chenqa.springaop.example.aspect.MyAspect"/>
  <!-- aop配置 -->
  <aop:config>
    <aop:aspect ref="myAspect">
      <aop:pointcut expression="execution(* com.chenqa.springaop.example.service.impl.*.*(..))" id="pointcut"/>
      <aop:before method="before" pointcut-ref="pointcut"/>
      <aop:after method="after" pointcut-ref="pointcut"/>
      <aop:after-returning method="afterReturning" pointcut-ref="pointcut"/>
      <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut"/>
      <aop:around method="around" pointcut-ref="pointcut"/>
    </aop:aspect>
  </aop:config>

編寫(xiě)測(cè)試程序

ApplicationContext context = new ClassPathXmlApplicationContext("spring-aop.xml");
    BankService bankService = context.getBean("bankService", BankService.class);
    bankService.transfer("張三", "李四", 200);

執(zhí)行后輸出: 

這里寫(xiě)圖片描述 

將測(cè)試程序中的200改成50,再執(zhí)行后輸出: 

這里寫(xiě)圖片描述 

通過(guò)測(cè)試結(jié)果可以看出,五種通知的執(zhí)行順序?yàn)椋?/p>

前置通知→環(huán)繞通知→正常返回通知/異常返回通知→返回通知,可以多次執(zhí)行來(lái)查看。

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

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

添加 PointCut類(lèi)

該pointcut用來(lái)攔截test包下的所有類(lèi)中的所有方法。

package test;

import org.aspectj.lang.annotation.Pointcut;

public class PointCuts {
  @Pointcut(value = "within(test.*)")
  public void aopDemo() {

  }
}

添加Aspect類(lèi)

該類(lèi)中的advice將會(huì)用到上面的pointcut,使用方法請(qǐng)看各個(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");
  }
}

添加測(cè)試用Controller

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

測(cè)試 正常情況

在瀏覽器直接輸入以下的URL,回車(chē):http://192.168.142.8:7070/aoptest/v1/aop/test?throwException=false1

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

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

測(cè)試 異常情況

在瀏覽器中直接輸入以下的URL,回車(chē):http://192.168.142.8:7070/aoptest/v1/aop/test?throwException=true1

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

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

結(jié)論

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

正常情況: 

one-ok

異常情況: 

one-exception

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

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

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

pointcut類(lèi)保持不變

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

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");
  }
}

測(cè)試用Controller也不變

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

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

測(cè)試 正常情況

在瀏覽器直接輸入以下的URL,回車(chē):http://192.168.142.8:7070/aoptest/v1/aop/test?throwException=false1

我們會(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í)候,我不能下定論說(shuō) aspect2 肯定就比 aspect1 先執(zhí)行。 

不信?你把服務(wù)務(wù)器重新啟動(dòng)一下,再試試,說(shuō)不定你就會(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

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

測(cè)試 異常情況

在瀏覽器中直接輸入以下的URL,回車(chē):http://192.168.142.8:7070/aoptest/v1/aop/test?throwException=true1

我們會(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ù)器重啟,然后再測(cè)試的話,就可能會(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

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

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

方法有兩種:

  1. 實(shí)現(xiàn)org.springframework.core.Ordered接口,實(shí)現(xiàn)它的getOrder()方法
  2. 給aspect添加@Order注解,該注解全稱(chēng)為: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í)行。如下圖所示: 

two-ok

注意點(diǎn)

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

對(duì)于@Around這個(gè)advice,不管它有沒(méi)有返回值,但是必須要方法內(nèi)部,調(diào)用一下 pjp.proceed();否則,Controller 中的接口將沒(méi)有機(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í)行。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring Cloud Gateway全局通用異常處理的實(shí)現(xiàn)

    Spring Cloud Gateway全局通用異常處理的實(shí)現(xiàn)

    這篇文章主要介紹了Spring Cloud Gateway全局通用異常處理的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • 解決IDEA報(bào)錯(cuò)Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded

    解決IDEA報(bào)錯(cuò)Caused by: org.springframework.boot.web.se

    遇到IDEA啟動(dòng)報(bào)錯(cuò),可嘗試以下方法:打開(kāi)項(xiàng)目設(shè)置(Ctrl+Shift+Alt+S),將JDK版本修改為1.8;或者檢查T(mén)omCat依賴(lài),若有問(wèn)題可嘗試刪除,此外,確保每次拉取項(xiàng)目后,maven地址設(shè)置為本地,并且JDK版本設(shè)置為1.8,以上為個(gè)人解決經(jīng)驗(yàn),希望對(duì)大家有所幫助
    2024-09-09
  • Java刪除文件、目錄及目錄下所有文件的方法實(shí)例

    Java刪除文件、目錄及目錄下所有文件的方法實(shí)例

    這篇文章主要給大家介紹了關(guān)于利用Java刪除文件、目錄及目錄下所有文件的方法,文中給出了詳細(xì)的示例代碼與注解,有需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2016-12-12
  • IDEA創(chuàng)建Maven工程Servlet的詳細(xì)教程

    IDEA創(chuàng)建Maven工程Servlet的詳細(xì)教程

    這篇文章主要介紹了IDEA創(chuàng)建Maven工程Servlet的詳細(xì)教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • 詳解Java實(shí)現(xiàn)多種方式的http數(shù)據(jù)抓取

    詳解Java實(shí)現(xiàn)多種方式的http數(shù)據(jù)抓取

    本篇文章主要介紹了Java實(shí)現(xiàn)多種方式的http數(shù)據(jù)抓取,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。
    2016-12-12
  • Java超詳細(xì)講解多線程中的Process與Thread

    Java超詳細(xì)講解多線程中的Process與Thread

    進(jìn)程process:在一定的環(huán)境下,把靜態(tài)的程序代碼運(yùn)行起來(lái),通過(guò)使用不同的資源,來(lái)完成一定的任務(wù);線程thread:是程序中一個(gè)單一的順序控制流程。在單個(gè)進(jìn)程中同時(shí)運(yùn)行多個(gè)線程完成不同的工作,稱(chēng)為多線程
    2022-05-05
  • JVM方法調(diào)用invokevirtual詳解

    JVM方法調(diào)用invokevirtual詳解

    JVM調(diào)用方法有五條指令,分別是invokestatic,invokespecial,invokevirtual,invokeinterface,invokedynamic,這篇文章主要說(shuō)明invokevirtual方法的調(diào)用問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2022-03-03
  • Java生成驗(yàn)證碼功能實(shí)例代碼

    Java生成驗(yàn)證碼功能實(shí)例代碼

    頁(yè)面上輸入驗(yàn)證碼是比較常見(jiàn)的一個(gè)功能,實(shí)現(xiàn)起來(lái)也很簡(jiǎn)單.給大家寫(xiě)一個(gè)簡(jiǎn)單的生成驗(yàn)證碼的示例程序,需要的朋友可以借鑒一下
    2017-05-05
  • Spring?Boot?實(shí)現(xiàn)Redis分布式鎖原理

    Spring?Boot?實(shí)現(xiàn)Redis分布式鎖原理

    這篇文章主要介紹了Spring?Boot實(shí)現(xiàn)Redis分布式鎖原理,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-08-08
  • 詳解Java動(dòng)態(tài)加載數(shù)據(jù)庫(kù)驅(qū)動(dòng)

    詳解Java動(dòng)態(tài)加載數(shù)據(jù)庫(kù)驅(qū)動(dòng)

    本篇文章主要介紹了詳解Java動(dòng)態(tài)加載數(shù)據(jù)庫(kù)驅(qū)動(dòng),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05

最新評(píng)論

曲沃县| 巴里| 渑池县| 毕节市| 东海县| 嘉义市| 任丘市| 肃南| 彩票| 邵东县| 荣昌县| 盐源县| 揭阳市| 嘉峪关市| 东丰县| 金塔县| 丹凤县| 玉溪市| 古交市| 武夷山市| 拉孜县| 墨竹工卡县| 三门县| 子长县| 嘉定区| 金湖县| 宁强县| 贡嘎县| 家居| 濉溪县| 松潘县| 平泉县| 衡阳市| 抚宁县| 城步| 百色市| 湘西| 北安市| 九江市| 正定县| 澄江县|