欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺談spring aop的五種通知類型

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

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

前置通知[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)大的通知類型,能在方法調(diào)用前后自定義一些操作。

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

定義接口

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

編寫實(shí)現(xiàn)類

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>

編寫測(cè)試程序

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

執(zhí)行后輸出: 

這里寫圖片描述 

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

這里寫圖片描述 

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

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

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

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

添加 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,使用方法請(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,回車: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,回車: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類攔截時(shí),aspect類內(nèi)部的 advice 將按照以下的順序進(jìn)行執(zhí)行:

正常情況: 

one-ok

異常情況: 

one-exception

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

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

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

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

測(cè)試用Controller也不變

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

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

測(cè)試 正常情況

在瀏覽器直接輸入以下的URL,回車: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í)候,我不能下定論說 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ì)給出解決方案。

測(cè)試 異常情況

在瀏覽器中直接輸入以下的URL,回車: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

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

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

方法有兩種:

  1. 實(shí)現(xiàn)org.springframework.core.Ordered接口,實(shí)現(xiàn)它的getOrder()方法
  2. 給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í)行。如下圖所示: 

two-ok

注意點(diǎn)

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

對(duì)于@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í)行。

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

相關(guān)文章

  • 詳解基于MybatisPlus兩步實(shí)現(xiàn)多租戶方案

    詳解基于MybatisPlus兩步實(shí)現(xiàn)多租戶方案

    這篇文章主要介紹了詳解基于MybatisPlus兩步實(shí)現(xiàn)多租戶方案,本文分兩步,通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • list轉(zhuǎn)tree和list中查找某節(jié)點(diǎn)下的所有數(shù)據(jù)操作

    list轉(zhuǎn)tree和list中查找某節(jié)點(diǎn)下的所有數(shù)據(jù)操作

    這篇文章主要介紹了list轉(zhuǎn)tree和list中查找某節(jié)點(diǎn)下的所有數(shù)據(jù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • java多線程加鎖以及Condition類的使用實(shí)例解析

    java多線程加鎖以及Condition類的使用實(shí)例解析

    這篇文章主要介紹了java多線程加鎖以及Condition類的使用實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • JavaFX桌面應(yīng)用未響應(yīng)問題解決方案

    JavaFX桌面應(yīng)用未響應(yīng)問題解決方案

    這篇文章主要介紹了JavaFX桌面應(yīng)用未響應(yīng)問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Java協(xié)程編程之Loom項(xiàng)目實(shí)戰(zhàn)記錄

    Java協(xié)程編程之Loom項(xiàng)目實(shí)戰(zhàn)記錄

    這篇文章主要介紹了Java協(xié)程編程之Loom項(xiàng)目嘗鮮,如果用嘗鮮的角度去使用Loom項(xiàng)目,可以提前窺探JVM開發(fā)者們是如何基于協(xié)程這個(gè)重大特性進(jìn)行開發(fā)的,這對(duì)于提高學(xué)習(xí)JDK內(nèi)核代碼的興趣有不少幫助,需要的朋友可以參考下
    2021-08-08
  • springmvc實(shí)現(xiàn)導(dǎo)出數(shù)據(jù)信息為excle表格示例代碼

    springmvc實(shí)現(xiàn)導(dǎo)出數(shù)據(jù)信息為excle表格示例代碼

    本篇文章主要介紹了springmvc實(shí)現(xiàn)導(dǎo)出數(shù)據(jù)信息為excle表格,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧。
    2017-01-01
  • Java基礎(chǔ)總結(jié)之Thymeleaf詳解

    Java基礎(chǔ)總結(jié)之Thymeleaf詳解

    Thymeleaf是一種現(xiàn)代的基于服務(wù)器端的Java模板引擎技術(shù),也是一個(gè)優(yōu)秀的面向Java的XML、XHTML、HTML5頁面模板,它具有豐富的標(biāo)簽語言、函數(shù)和表達(dá)式,在使用Spring Boot框架進(jìn)行頁面設(shè)計(jì)時(shí),一般會(huì)選擇Thymeleaf模板,需要的朋友可以參考下
    2021-05-05
  • 解決Spring配置文件中bean的property屬性中的name出錯(cuò)問題

    解決Spring配置文件中bean的property屬性中的name出錯(cuò)問題

    這篇文章主要介紹了解決Spring配置文件中bean的property屬性中的name出錯(cuò)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • SpringBoot中使用Ehcache的詳細(xì)教程

    SpringBoot中使用Ehcache的詳細(xì)教程

    EhCache 是一個(gè)純 Java 的進(jìn)程內(nèi)緩存框架,具有快速、精干等特點(diǎn),是 Hibernate 中默認(rèn)的 CacheProvider。這篇文章主要介紹了SpringBoot中使用Ehcache的相關(guān)知識(shí),需要的朋友可以參考下
    2020-08-08
  • 淺談java中的訪問修飾符

    淺談java中的訪問修飾符

    這篇文章介紹了java中的訪問修飾符,有需要的朋友可以參考一下
    2013-10-10

最新評(píng)論