spring aop之链式调用的实现

 更新时间:2019年02月20日 10:18:19   作者:niocoder  
这篇文章主要介绍了spring aop之链式调用的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

概述

AOPAspect Orient Programming),我们一般称为面向方面(切面)编程,作为面向对象的一种补充,用于处理系统中分布于各个模块的横切关注点,比如事务管理、日志、缓存等等。 Spring AOP采用的是动态代理,在运行期间对业务方法进行增强,所以不会生成新类,Spring AOP提供了对JDK动态代理的支持以及CGLib的支持。本章我们不关注aop代理类的实现,我简单实现一个指定次序的链式调用。

实现链式调用的

MethodInterceptor定义拦截器链,MethodInvocation 递归进入下一个拦截器链中。类图如下:

MethodInterceptor

public interface MethodInterceptor {

  Object invoke(MethodInvocation invocation) throws Throwable;
}

MethodInvocation

public interface MethodInvocation {

  Object proceed() throws Throwable;
}

AbstractAspectJAdvice

抽象类,实现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

实现MethodInvocationproceed()方法递归实现链式调用。

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 一分钟掌握Java ElasticJob分布式定时任务

    一分钟掌握Java ElasticJob分布式定时任务

    ElasticJob 是面向互联网生态和海量任务的分布式调度解决方案,本文主要通过简单的示例带大家深入了解ElasticJob分布式定时任务的相关知识,需要的可以参考一下
    2023-05-05
  • Spring中为bean指定InitMethod和DestroyMethod的执行方法

    Spring中为bean指定InitMethod和DestroyMethod的执行方法

    在Spring中,那些组成应用程序的主体及由Spring IoC容器所管理的对象,被称之为bean,接下来通过本文给大家介绍Spring中为bean指定InitMethod和DestroyMethod的执行方法,感兴趣的朋友一起看看吧
    2021-11-11
  • MyBatis中的模糊查询语句

    MyBatis中的模糊查询语句

    这篇文章主要介绍了MyBatis中的模糊查询语句的相关资料,需要的朋友可以参考下
    2017-03-03
  • 如何将Java枚举名称作为注解的属性值实现详解

    如何将Java枚举名称作为注解的属性值实现详解

    这篇文章主要为大家介绍了如何将Java枚举名称作为注解的属性值实现详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • Java函数式编程(十二):监控文件修改

    Java函数式编程(十二):监控文件修改

    这篇文章主要介绍了Java函数式编程(十二):监控文件修改,本文是系列文章的第12篇,其它文章请参阅本文底部的相关文章,需要的朋友可以参考下
    2014-09-09
  • Java通过Timer与TimerTask实现定时任务调度方式

    Java通过Timer与TimerTask实现定时任务调度方式

    本文介绍了如何在Java中使用`Timer`和`TimerTask`类来实现定时任务调度,`Timer`类用于创建计时器并安排任务,而`TimerTask`类用于定义具体的任务,文章详细介绍了这两个类的方法和使用示例,包括创建任务、安排任务、取消任务等操作,通过一个简单的例子
    2024-12-12
  • 全面了解java异常

    全面了解java异常

    本文非常详细的介绍了java异常,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们可以学习一下这篇文章
    2021-08-08
  • Java 同步工具与组合类的线程安全性解析

    Java 同步工具与组合类的线程安全性解析

    这篇文章主要介绍了Java 同步工具与组合类的线程安全性解析,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-09-09
  • 浅谈SpringBoot如何正确拦截thymeleaf异常

    浅谈SpringBoot如何正确拦截thymeleaf异常

    Thymeleaf是一个模板引擎工具,主要用于页面渲染操作,本文主要介绍了浅谈SpringBoot如何正确拦截thymeleaf异常,具有一定的参考价值,感兴趣的可以了解一下
    2023-09-09
  • java阶乘计算获得结果末尾0的个数代码实现

    java阶乘计算获得结果末尾0的个数代码实现

    今天偶然看到一个要求,求1000~10000之间的数n的阶乘并计算所得的数n!末尾有多少个0?要求: 不计算 只要得到末尾有多少个0就可以了,看下面的代码吧
    2013-12-12

最新评论