SpringAop源码及调用过程概述

 更新时间:2023年10月21日 09:16:44   作者:tobebetter9527  
这篇文章主要介绍了SpringAop源码及调用过程概述,Spring AOP(面向切面编程)是Spring框架的一个重要特性,它提供了一种在程序运行期间动态地将额外的行为织入到代码中的方式,需要的朋友可以参考下

1.源码

public class TestAop {

  public static void main(String[] args) throws Exception {
    saveGeneratedCGlibProxyFiles(System.getProperty("user.dir") + "/proxy");
    ApplicationContext ac = new ClassPathXmlApplicationContext("META-INF/aop.xml");
    MyCalculator bean = ac.getBean(MyCalculator.class);
    System.out.println(bean.toString());
    bean.add(1, 1);
    bean.sub(1, 1);

  }

  public static void saveGeneratedCGlibProxyFiles(String dir) throws Exception {
    Field field = System.class.getDeclaredField("props");
    field.setAccessible(true);
    Properties props = (Properties) field.get(null);
    //dir为保存文件路径
    System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, dir);
    props.put("net.sf.cglib.core.DebuggingClassWriter.traceEnabled", "true");
  }
}

public class MyCalculator /*implements Calculator */ {

  public Integer add(Integer i, Integer j) throws NoSuchMethodException {
    Integer result = i + j;
    System.out.println("MyCalculator add method invoked");
    return result;
  }

  public Integer sub(Integer i, Integer j) throws NoSuchMethodException {
    Integer result = i - j;
    return result;
  }

  public Integer mul(Integer i, Integer j) throws NoSuchMethodException {
    Integer result = i * j;
    return result;
  }

  public Integer div(Integer i, Integer j) throws NoSuchMethodException {
    Integer result = i / j;
    return result;
  }

  public Integer show(Integer i) {
    System.out.println("show .....");
    return i;
  }

  @Override
  public String toString() {
    return "super.toString()";
  }
}

public class LogUtil {

  private int start(JoinPoint joinPoint) {
    //获取方法签名
    Signature signature = joinPoint.getSignature();
    //获取参数信息
    Object[] args = joinPoint.getArgs();
    System.out.println("log---Before advice: " + signature.getName() + "方法开始执行:参数是" + Arrays.asList(args));
    return 100;
  }

  public static void stop(JoinPoint joinPoint, Object result) {
    Signature signature = joinPoint.getSignature();
    System.out.println("log---AfterReturning advice: " + signature.getName() + "方法执行结束,结果是:" + result);
  }

  public static void logException(JoinPoint joinPoint, Exception e) {
    Signature signature = joinPoint.getSignature();
    System.out.println("log--- AfterThrowing advice: " + signature.getName() + "方法抛出异常:" + e.getMessage());
  }

  public static void logFinally(JoinPoint joinPoint) {
    Signature signature = joinPoint.getSignature();
    System.out.println("log---After advice: " + signature.getName() + "方法执行结束。。。。。over");

  }

  public Object around(ProceedingJoinPoint pjp) throws Throwable {
    Signature signature = pjp.getSignature();
    Object[] args = pjp.getArgs();
    Object result = null;
    try {
      System.out.println("log---Around advice start:" + signature.getName() + "方法开始执行,参数为:" + Arrays.asList(args));
      //通过反射的方式调用目标的方法,相当于执行method.invoke(),可以自己修改结果值
      result = pjp.proceed(args);
      //            result=100;
      System.out.println("log---Around advice end: " + signature.getName() + "方法执行结束");
    } catch (Throwable throwable) {
      System.out.println("log---Around advice error:" + signature.getName() + "出现异常");
      throw throwable;
    } finally {
      System.out.println("log---Around advice finally:" + signature.getName() + "方法返回结果是:" + result);
    }
    return result;
  }
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
">

  <!--    <bean class="com.mashibing.MyBeanFactoryPostProcessorBySelf" ></bean>-->
  <bean id="logUtil" class="org.geekbang.thinking.in.spring.ioc.overview.aop.xml.util.LogUtil"></bean>
  <bean id="myCalculator" class="org.geekbang.thinking.in.spring.ioc.overview.aop.xml.service.MyCalculator"></bean>
  <aop:config>
    <aop:aspect ref="logUtil">
      <aop:pointcut id="myPoint"
        expression="execution( Integer org.geekbang.thinking.in.spring.ioc.overview.aop.xml.service.MyCalculator.*  (..))"/>
      <aop:around method="around" pointcut-ref="myPoint"></aop:around>
     <aop:after method="logFinally" pointcut-ref="myPoint"></aop:after>
      <aop:before method="start" pointcut-ref="myPoint"></aop:before>
      <aop:after-returning method="stop" pointcut-ref="myPoint" returning="result"></aop:after-returning>
      <aop:after-throwing method="logException" pointcut-ref="myPoint" throwing="e"></aop:after-throwing>
    </aop:aspect>
  </aop:config>
  <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

2. debug过程

生成的源码:

public class MyCalculator$$EnhancerBySpringCGLIB$$3f411fc extends MyCalculator implements SpringProxy, Advised, Factory {
  private boolean CGLIB$BOUND;
  public static Object CGLIB$FACTORY_DATA;
  private static final ThreadLocal CGLIB$THREAD_CALLBACKS;
  private static final Callback[] CGLIB$STATIC_CALLBACKS;
  private MethodInterceptor CGLIB$CALLBACK_0;
  private MethodInterceptor CGLIB$CALLBACK_1;
  private NoOp CGLIB$CALLBACK_2;
  private Dispatcher CGLIB$CALLBACK_3;
  private Dispatcher CGLIB$CALLBACK_4;
  private MethodInterceptor CGLIB$CALLBACK_5;
  private MethodInterceptor CGLIB$CALLBACK_6;
  private static Object CGLIB$CALLBACK_FILTER;
  private static final Method CGLIB$add$0$Method;
  private static final MethodProxy CGLIB$add$0$Proxy;
  private static final Object[] CGLIB$emptyArgs;
  private static final Method CGLIB$toString$1$Method;
  private static final MethodProxy CGLIB$toString$1$Proxy;
  private static final Method CGLIB$sub$2$Method;
  private static final MethodProxy CGLIB$sub$2$Proxy;
  private static final Method CGLIB$mul$3$Method;
  private static final MethodProxy CGLIB$mul$3$Proxy;
  private static final Method CGLIB$show$4$Method;
  private static final MethodProxy CGLIB$show$4$Proxy;
  private static final Method CGLIB$div$5$Method;
  private static final MethodProxy CGLIB$div$5$Proxy;
  private static final Method CGLIB$equals$6$Method;
  private static final MethodProxy CGLIB$equals$6$Proxy;
  private static final Method CGLIB$hashCode$7$Method;
  private static final MethodProxy CGLIB$hashCode$7$Proxy;
  private static final Method CGLIB$clone$8$Method;
  private static final MethodProxy CGLIB$clone$8$Proxy;

  static void CGLIB$STATICHOOK1() {
    CGLIB$THREAD_CALLBACKS = new ThreadLocal();
    CGLIB$emptyArgs = new Object[0];
    Class var0 = Class.forName("org.geekbang.thinking.in.spring.ioc.overview.aop.xml.service.MyCalculator$$EnhancerBySpringCGLIB$$3f411fc");
    Class var1;
    Method[] var10000 = ReflectUtils.findMethods(new String[]{"equals", "(Ljava/lang/Object;)Z", "hashCode", "()I", "clone", "()Ljava/lang/Object;"}, (var1 = Class.forName("java.lang.Object")).getDeclaredMethods());
    CGLIB$equals$6$Method = var10000[0];
    CGLIB$equals$6$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/Object;)Z", "equals", "CGLIB$equals$6");
    CGLIB$hashCode$7$Method = var10000[1];
    CGLIB$hashCode$7$Proxy = MethodProxy.create(var1, var0, "()I", "hashCode", "CGLIB$hashCode$7");
    CGLIB$clone$8$Method = var10000[2];
    CGLIB$clone$8$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/Object;", "clone", "CGLIB$clone$8");
    var10000 = ReflectUtils.findMethods(new String[]{"add", "(Ljava/lang/Integer;Ljava/lang/Integer;)Ljava/lang/Integer;", "toString", "()Ljava/lang/String;", "sub", "(Ljava/lang/Integer;Ljava/lang/Integer;)Ljava/lang/Integer;", "mul", "(Ljava/lang/Integer;Ljava/lang/Integer;)Ljava/lang/Integer;", "show", "(Ljava/lang/Integer;)Ljava/lang/Integer;", "div", "(Ljava/lang/Integer;Ljava/lang/Integer;)Ljava/lang/Integer;"}, (var1 = Class.forName("org.geekbang.thinking.in.spring.ioc.overview.aop.xml.service.MyCalculator")).getDeclaredMethods());
    CGLIB$add$0$Method = var10000[0];
    CGLIB$add$0$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/Integer;Ljava/lang/Integer;)Ljava/lang/Integer;", "add", "CGLIB$add$0");
    CGLIB$toString$1$Method = var10000[1];
    CGLIB$toString$1$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/String;", "toString", "CGLIB$toString$1");
    CGLIB$sub$2$Method = var10000[2];
    CGLIB$sub$2$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/Integer;Ljava/lang/Integer;)Ljava/lang/Integer;", "sub", "CGLIB$sub$2");
    CGLIB$mul$3$Method = var10000[3];
    CGLIB$mul$3$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/Integer;Ljava/lang/Integer;)Ljava/lang/Integer;", "mul", "CGLIB$mul$3");
    CGLIB$show$4$Method = var10000[4];
    CGLIB$show$4$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/Integer;)Ljava/lang/Integer;", "show", "CGLIB$show$4");
    CGLIB$div$5$Method = var10000[5];
    CGLIB$div$5$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/Integer;Ljava/lang/Integer;)Ljava/lang/Integer;", "div", "CGLIB$div$5");
  }

  final Integer CGLIB$add$0(Integer var1, Integer var2) throws NoSuchMethodException {
    return super.add(var1, var2);
  }

  public final Integer add(Integer var1, Integer var2) throws NoSuchMethodException {
    MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
    if (var10000 == null) {
      CGLIB$BIND_CALLBACKS(this);
      var10000 = this.CGLIB$CALLBACK_0;
    }
    // 从这里进入MethodInterceptor的接口
    return var10000 != null ? (Integer)var10000.intercept(this, CGLIB$add$0$Method, new Object[]{var1, var2}, CGLIB$add$0$Proxy) : super.add(var1, var2);
  }
}

在这里插入图片描述

CGLIB$CALLBACK_0的advised对象的targetSource有一个普通对象MyCalculate.

在这里插入图片描述

获得执行链

在这里插入图片描述

进入调用链

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

进入实际的方法

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

到此这篇关于SpringAop源码及调用过程概述的文章就介绍到这了,更多相关SpringAop调用过程内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • SpringBoot服务器端解决跨域问题

    SpringBoot服务器端解决跨域问题

    这篇文章主要介绍了SpringBoot服务器端解决跨域问题,帮助大家更好的理解和使用springboot框架,感兴趣的朋友可以了解下
    2020-11-11
  • Java选择结构与循环结构的使用详解

    Java选择结构与循环结构的使用详解

    循环结构是指在程序中需要反复执行某个功能而设置的一种程序结构。它由循环体中的条件,判断继续执行某个功能还是退出循环,选择结构用于判断给定的条件,根据判断的结果判断某些条件,根据判断的结果来控制程序的流程
    2022-03-03
  • 注解@CrossOrigin解决跨域的问题

    注解@CrossOrigin解决跨域的问题

    这篇文章主要介绍了注解@CrossOrigin解决跨域的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • java 记录一个子串在整串中出现的次数实例

    java 记录一个子串在整串中出现的次数实例

    今天小编就为大家分享一篇java 记录一个子串在整串中出现的次数实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • SpringBoot默认包扫描机制及@ComponentScan指定扫描路径详解

    SpringBoot默认包扫描机制及@ComponentScan指定扫描路径详解

    这篇文章主要介绍了SpringBoot默认包扫描机制及@ComponentScan指定扫描路径详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • Java有效处理异常的三个原则

    Java有效处理异常的三个原则

    Java中异常提供了一种识别及响应错误情况的一致性机制,有效地异常处理能使程序更加健壮、易于调试。那么这篇文章总结了Java有效处理异常的三个原则,有需要的朋友们可以参考借鉴。
    2016-09-09
  • Java 静态数据初始化的示例代码

    Java 静态数据初始化的示例代码

    这篇文章主要介绍了Java 静态数据初始化的示例代码,帮助大家更好的理解和学习Java,感兴趣的朋友可以了解下
    2020-09-09
  • Java Annotation详解及实例代码

    Java Annotation详解及实例代码

    这篇文章主要介绍了Java Annotation详解及实例代码的相关资料,需要的朋友可以参考下
    2017-03-03
  • Java easyExcel的复杂表头多级表头导入

    Java easyExcel的复杂表头多级表头导入

    最近在项目开发中遇到的一个excel复杂表头的导入数据库操作,下面这篇文章主要给大家介绍了关于Java easyExcel的复杂表头多级表头导入的相关资料,需要的朋友可以参考下
    2022-06-06
  • 基于序列化存取实现java对象深度克隆的方法详解

    基于序列化存取实现java对象深度克隆的方法详解

    本篇文章是对序列化存取实现java对象深度克隆的方法进行了详细的分析介绍,需要的朋友参考下
    2013-05-05

最新评论