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

spring aop之鏈?zhǔn)秸{(diào)用的實現(xiàn)

 更新時間:2019年02月20日 10:18:19   作者:niocoder  
這篇文章主要介紹了spring aop之鏈?zhǔn)秸{(diào)用的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

概述

AOPAspect Orient Programming),我們一般稱為面向方面(切面)編程,作為面向?qū)ο蟮囊环N補充,用于處理系統(tǒng)中分布于各個模塊的橫切關(guān)注點,比如事務(wù)管理、日志、緩存等等。 Spring AOP采用的是動態(tài)代理,在運行期間對業(yè)務(wù)方法進行增強,所以不會生成新類,Spring AOP提供了對JDK動態(tài)代理的支持以及CGLib的支持。本章我們不關(guān)注aop代理類的實現(xiàn),我簡單實現(xiàn)一個指定次序的鏈?zhǔn)秸{(diào)用。

實現(xiàn)鏈?zhǔn)秸{(diào)用的

MethodInterceptor定義攔截器鏈,MethodInvocation 遞歸進入下一個攔截器鏈中。類圖如下:

MethodInterceptor

public interface MethodInterceptor {

  Object invoke(MethodInvocation invocation) throws Throwable;
}

MethodInvocation

public interface MethodInvocation {

  Object proceed() throws Throwable;
}

AbstractAspectJAdvice

抽象類,實現(xiàn)MethodInterceptor

public abstract class AbstractAspectJAdvice implements MethodInterceptor{

  private Method adviceMethod;

  private Object adviceObject;

  public AbstractAspectJAdvice(Method adviceMethod, Object adviceObject) {
    this.adviceMethod = adviceMethod;
    this.adviceObject = adviceObject;
  }

  public Method getAdviceMethod() {
    return this.adviceMethod;
  }

  public void invokeAdviceMethod() throws Throwable {
    adviceMethod.invoke(adviceObject);
  }
}

AspectJBeforeAdvice

前置通知

public class AspectJBeforeAdvice extends AbstractAspectJAdvice {

  public AspectJBeforeAdvice(Method method, Object adviceObject) {
    super(method, adviceObject);
  }

  @Override
  public Object invoke(MethodInvocation invocation) throws Throwable{
    this.invokeAdviceMethod();
    Object o = invocation.proceed();
    return o;
  }
}

AspectJAfterReturningAdvice

后置通知

public class AspectJAfterReturningAdvice extends AbstractAspectJAdvice {

  public AspectJAfterReturningAdvice(Method method, Object adviceObject) {
    super(method, adviceObject);
  }

  @Override
  public Object invoke(MethodInvocation invocation) throws Throwable{
    Object o = invocation.proceed();
    this.invokeAdviceMethod();
    return o;
  }
}

ReflectiveMethodInvocation

實現(xiàn)MethodInvocationproceed()方法遞歸實現(xiàn)鏈?zhǔn)秸{(diào)用。

public class ReflectiveMethodInvocation implements MethodInvocation {

  private final Object targetObject;

  private final Method targetMethod;

  private final List<MethodInterceptor> interceptorList;

  private int currentInterceptorIndex = -1;

  public ReflectiveMethodInvocation(Object targetObject, Method targetMethod, List<MethodInterceptor> interceptorList) {
    this.targetObject = targetObject;
    this.targetMethod = targetMethod;
    this.interceptorList = interceptorList;
  }

  @Override
  public Object proceed() throws Throwable {

    if (this.currentInterceptorIndex == this.interceptorList.size() - 1) {
      return invokeJoinPoint();
    }

    this.currentInterceptorIndex++;

    MethodInterceptor interceptor =
        this.interceptorList.get(this.currentInterceptorIndex);
    return interceptor.invoke(this);
  }

  private Object invokeJoinPoint() throws Throwable {

    return this.targetMethod.invoke(this.targetObject);
  }
}

NioCoderService

模擬service

public class NioCoderService {

  public void testAop() {
    System.out.println("http://niocoder.com/");
  }
}

TransactionManager

模擬通知類

public class TransactionManager {
  public void start() {
    System.out.println("start tx");
  }

  public void commit() {
    System.out.println("commit tx");
  }

  public void rollback() {
    System.out.println("rollback tx");
  }

}

ReflectiveMethodInvocationTest

beforeAdvice->afterReturningAdvice

測試類,測試通知

public class ReflectiveMethodInvocationTest {

  private AspectJBeforeAdvice beforeAdvice = null;

  private AspectJAfterReturningAdvice afterReturningAdvice = null;

  private NioCoderService nioCoderService;

  private TransactionManager tx;

  public void setUp() throws Exception {
    nioCoderService = new NioCoderService();
    tx = new TransactionManager();
    beforeAdvice = new AspectJBeforeAdvice(TransactionManager.class.getMethod("start"), tx);
    afterReturningAdvice = new AspectJAfterReturningAdvice(TransactionManager.class.getMethod("commit"), tx);
  }

  public void testMethodInvocation() throws Throwable {
    Method method = NioCoderService.class.getMethod("testAop");
    List<MethodInterceptor> interceptorList = new ArrayList<>();
    interceptorList.add(beforeAdvice);
    interceptorList.add(afterReturningAdvice);    

    ReflectiveMethodInvocation mi = new ReflectiveMethodInvocation(nioCoderService, method, interceptorList);

    mi.proceed();
  }


  public static void main(String[] args) throws Throwable {
    ReflectiveMethodInvocationTest reflectiveMethodInvocationTest = new ReflectiveMethodInvocationTest();
    reflectiveMethodInvocationTest.setUp();
    reflectiveMethodInvocationTest.testMethodInvocation();
  }
}

輸出:

start tx
http://niocoder.com/
commit tx

時序圖 beforeAdvice->afterReturningAdvice

afterReturningAdvice->beforeAdvice

修改interceptorList的順序

public void testMethodInvocation() throws Throwable {
    Method method = NioCoderService.class.getMethod("testAop");
    List<MethodInterceptor> interceptorList = new ArrayList<>();
    interceptorList.add(afterReturningAdvice);
     interceptorList.add(beforeAdvice);

    ReflectiveMethodInvocation mi = new ReflectiveMethodInvocation(nioCoderService, method, interceptorList);

    mi.proceed();
  }

輸出:

start tx
http://niocoder.com/
commit tx

時序圖 afterReturningAdvice->beforeAdvice

代碼下載

github:https://github.com/longfeizheng/data-structure-java/blob/master/src/main/java/cn/merryyou/aop

代碼下載

github:https://github.com/longfeizheng/data-structure-java

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

相關(guān)文章

  • Spring boot實現(xiàn)文件上傳功能

    Spring boot實現(xiàn)文件上傳功能

    這篇文章主要為大家詳細(xì)介紹了Spring boot實現(xiàn)文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • 在jmeter的beanshell中用java獲取系統(tǒng)當(dāng)前時間的簡單實例

    在jmeter的beanshell中用java獲取系統(tǒng)當(dāng)前時間的簡單實例

    這篇文章介紹了在jmeter的beanshell中用java獲取系統(tǒng)當(dāng)前時間的簡單實例,有需要的朋友可以參考一下
    2013-09-09
  • 基于Spring實現(xiàn)文件上傳功能

    基于Spring實現(xiàn)文件上傳功能

    這篇文章主要為大家詳細(xì)介紹了Spring實現(xiàn)文件上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • springAop實現(xiàn)講解(看這篇夠了)

    springAop實現(xiàn)講解(看這篇夠了)

    AOP面向切面編程是一種編程范式,它通過將通用的橫切關(guān)注點(如日志、事務(wù)、權(quán)限控制等)與業(yè)務(wù)邏輯分離,使得代碼更加清晰、簡潔、易于維護,這篇文章主要介紹了springAop實現(xiàn)講解(看這篇夠了),需要的朋友可以參考下
    2024-02-02
  • java中靜態(tài)變量和實例變量的區(qū)別詳細(xì)介紹

    java中靜態(tài)變量和實例變量的區(qū)別詳細(xì)介紹

    本篇文章介紹了,java中靜態(tài)變量和實例變量的區(qū)別。需要的朋友參考下
    2013-05-05
  • Java使用easyExcel批量導(dǎo)入數(shù)據(jù)詳解

    Java使用easyExcel批量導(dǎo)入數(shù)據(jù)詳解

    這篇文章主要介紹了Java使用easyExcel批量導(dǎo)入數(shù)據(jù)詳解,通常我們會提供一個模板,此模塊我們可以使用easyExcel導(dǎo)出數(shù)據(jù)生成的一個Excel文件當(dāng)作模板,提供下載鏈接,用戶在該文件內(nèi)填入規(guī)定的數(shù)據(jù)格式以后可以批量導(dǎo)入數(shù)據(jù)到數(shù)據(jù)庫中,需要的朋友可以參考下
    2023-08-08
  • mybatis 連接mysql數(shù)據(jù)庫 tinyint 為boolean類型詳解

    mybatis 連接mysql數(shù)據(jù)庫 tinyint 為boolean類型詳解

    這篇文章主要介紹了mybatis 連接mysql數(shù)據(jù)庫 tinyint 為boolean類型詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • SpringBoot容器的主要組件詳解

    SpringBoot容器的主要組件詳解

    這篇文章主要介紹了SpringBoot容器的主要組件詳解,SpringBoot?是基于?Spring?Framework?的一種快速開發(fā)框架,它可以幫助開發(fā)者快速地構(gòu)建獨立的、生產(chǎn)級別的、可部署的應(yīng)用程序,需要的朋友可以參考下
    2023-09-09
  • java 實現(xiàn)取int型的第二個字節(jié)的數(shù)

    java 實現(xiàn)取int型的第二個字節(jié)的數(shù)

    這篇文章主要介紹了java 實現(xiàn)取int型的第二個字節(jié)的數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 詳解Java中的JDK、JRE、JVM

    詳解Java中的JDK、JRE、JVM

    本文主要介紹了Java中的JDK、JRE、JVM的相關(guān)知識。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-01-01

最新評論