亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringAop源碼及調用過程概述

 更新時間:2023年10月21日 09:16:44   作者:tobebetter9527  
這篇文章主要介紹了SpringAop源碼及調用過程概述,Spring AOP(面向切面編程)是Spring框架的一個重要特性,它提供了一種在程序運行期間動態(tài)地將額外的行為織入到代碼中的方式,需要的朋友可以參考下

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() + "方法開始執(zhí)行:參數是" + 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() + "方法執(zhí)行結束,結果是:" + 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() + "方法執(zhí)行結束。。。。。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() + "方法開始執(zhí)行,參數為:" + Arrays.asList(args));
      //通過反射的方式調用目標的方法,相當于執(zhí)行method.invoke(),可以自己修改結果值
      result = pjp.proceed(args);
      //            result=100;
      System.out.println("log---Around advice end: " + signature.getName() + "方法執(zhí)行結束");
    } 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.

在這里插入圖片描述

獲得執(zhí)行鏈

在這里插入圖片描述

進入調用鏈

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

進入實際的方法

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

到此這篇關于SpringAop源碼及調用過程概述的文章就介紹到這了,更多相關SpringAop調用過程內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java面試題沖刺第二十三天--分布式

    Java面試題沖刺第二十三天--分布式

    這篇文章主要為大家分享了最有價值的三道關于分布式的面試題,涵蓋內容全面,包括數據結構和算法相關的題目、經典面試編程題等,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 單機redis分布式鎖實現原理解析

    單機redis分布式鎖實現原理解析

    這篇文章主要介紹了單機redis分布式鎖實現原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • Java運算符解密之位運算、移位運算舉例詳解

    Java運算符解密之位運算、移位運算舉例詳解

    這篇文章主要介紹了Java運算符解密之位運算、移位運算的相關資料,Java中的位運算符包括按位與&、按位或|、按位取反~和按位異或^,用于對數據的二進制位進行操作,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2025-04-04
  • Spring?Security中自定義cors配置及原理解析

    Spring?Security中自定義cors配置及原理解析

    在Spring框架中,通過自定義CORS配置可根據實際情況調整URL的協議、主機、端口等,以適應"同源安全策略",配置原理涉及CorsConfigurer和CorsFilter,自定義配置需要注意@Configuration注解、方法名以及可能的@Autowired注解
    2024-10-10
  • 深入分析Java內存區(qū)域的使用詳解

    深入分析Java內存區(qū)域的使用詳解

    本篇文章對Java內存區(qū)域的使用進行了詳細的介紹。需要的朋友參考下
    2013-05-05
  • 詳解java中spring里的三大攔截器

    詳解java中spring里的三大攔截器

    在本篇文章里我們給大家詳細講述了java中spring里的三大攔截器相關知識點以及用法代碼,需要的朋友們學習下。
    2018-10-10
  • Java輸入字母來判斷星期幾的實現代碼

    Java輸入字母來判斷星期幾的實現代碼

    這篇文章主要介紹了Java輸入字母來判斷星期幾的實現代碼,用情況語句比較好,如果第一個字母一樣,則判斷用情況語句或if語句判斷第二個字母需要的朋友可以參考下
    2017-02-02
  • Spring?Bean注冊與注入實現方法詳解

    Spring?Bean注冊與注入實現方法詳解

    首先,要學習Spring中的Bean的注入方式,就要先了解什么是依賴注入。依賴注入是指:讓調用類對某一接口的實現類的實現類的依賴關系由第三方注入,以此來消除調用類對某一接口實現類的依賴。Spring容器中支持的依賴注入方式主要有屬性注入、構造函數注入、工廠方法注入
    2022-10-10
  • java定時任務實現的4種方式小結

    java定時任務實現的4種方式小結

    這篇文章主要介紹了java定時任務實現的4種方式小結,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • 基于springMVC web.xml中的配置加載順序

    基于springMVC web.xml中的配置加載順序

    這篇文章主要介紹了springMVC web.xml中的配置加載順序,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09

最新評論