最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringAop源碼及調(diào)用過(guò)程概述

 更新時(shí)間:2023年10月21日 09:16:44   作者:tobebetter9527  
這篇文章主要介紹了SpringAop源碼及調(diào)用過(guò)程概述,Spring AOP(面向切面編程)是Spring框架的一個(gè)重要特性,它提供了一種在程序運(yùn)行期間動(dòng)態(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();
    //獲取參數(shù)信息
    Object[] args = joinPoint.getArgs();
    System.out.println("log---Before advice: " + signature.getName() + "方法開(kāi)始執(zhí)行:參數(shù)是" + 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í)行結(jié)束,結(jié)果是:" + 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í)行結(jié)束。。。。。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() + "方法開(kāi)始執(zhí)行,參數(shù)為:" + Arrays.asList(args));
      //通過(guò)反射的方式調(diào)用目標(biāo)的方法,相當(dāng)于執(zhí)行method.invoke(),可以自己修改結(jié)果值
      result = pjp.proceed(args);
      //            result=100;
      System.out.println("log---Around advice end: " + signature.getName() + "方法執(zhí)行結(jié)束");
    } catch (Throwable throwable) {
      System.out.println("log---Around advice error:" + signature.getName() + "出現(xiàn)異常");
      throw throwable;
    } finally {
      System.out.println("log---Around advice finally:" + signature.getName() + "方法返回結(jié)果是:" + 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過(guò)程

生成的源碼:

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;
    }
    // 從這里進(jìn)入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對(duì)象的targetSource有一個(gè)普通對(duì)象MyCalculate.

在這里插入圖片描述

獲得執(zhí)行鏈

在這里插入圖片描述

進(jìn)入調(diào)用鏈

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

進(jìn)入實(shí)際的方法

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

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

相關(guān)文章

  • Spring Boot集成mongodb數(shù)據(jù)庫(kù)過(guò)程解析

    Spring Boot集成mongodb數(shù)據(jù)庫(kù)過(guò)程解析

    這篇文章主要介紹了Spring Boot集成mongodb數(shù)據(jù)庫(kù)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Spring Boot項(xiàng)目如何使用外部application.yml配置文件啟動(dòng)JAR包

    Spring Boot項(xiàng)目如何使用外部application.yml配置文件啟動(dòng)JAR包

    文章介紹了SpringBoot項(xiàng)目通過(guò)指定外部application.yml配置文件啟動(dòng)JAR包的方法,包括設(shè)置spring.config.location參數(shù)、打包注意事項(xiàng)及路徑格式要求,強(qiáng)調(diào)外部配置優(yōu)先級(jí)和覆蓋機(jī)制,提供Linux/Windows啟動(dòng)示例,需要的朋友跟隨小編一起學(xué)習(xí)吧
    2025-08-08
  • Spring MVC注解式開(kāi)發(fā)使用詳解

    Spring MVC注解式開(kāi)發(fā)使用詳解

    本篇文章主要介紹了Spring MVC注解式開(kāi)發(fā)使用詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • JAVA中尋找最大的K個(gè)數(shù)解法

    JAVA中尋找最大的K個(gè)數(shù)解法

    尋找最大的K個(gè)數(shù),這個(gè)是面試中比較常見(jiàn)的一道題,網(wǎng)上也有很多例子,在這里是比較傳統(tǒng)的解法
    2014-04-04
  • 淺談java獲取服務(wù)器基本信息

    淺談java獲取服務(wù)器基本信息

    這篇文章主要介紹了java獲取服務(wù)器基本信息,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Java 四種基本加密算法分析

    Java 四種基本加密算法分析

    這篇文章主要介紹了Java 四種基本加密算法分析的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • java設(shè)置日期返回格式的4種方式小結(jié)

    java設(shè)置日期返回格式的4種方式小結(jié)

    這篇文章主要為大家詳細(xì)介紹了java設(shè)置日期返回格式的4種方式,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-12-12
  • spring XML配置文件標(biāo)簽詳解

    spring XML配置文件標(biāo)簽詳解

    這篇文章主要介紹了spring XML配置文件標(biāo)簽詳解,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2024-12-12
  • 一鍵清除maven倉(cāng)庫(kù)中下載失敗的jar包的實(shí)現(xiàn)方法

    一鍵清除maven倉(cāng)庫(kù)中下載失敗的jar包的實(shí)現(xiàn)方法

    這篇文章主要介紹了一鍵清除maven倉(cāng)庫(kù)中下載失敗的jar包的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 淺談spring-boot-rabbitmq動(dòng)態(tài)管理的方法

    淺談spring-boot-rabbitmq動(dòng)態(tài)管理的方法

    這篇文章主要介紹了淺談spring-boot-rabbitmq動(dòng)態(tài)管理的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12

最新評(píng)論

贞丰县| 克什克腾旗| 隆德县| 沾益县| 西畴县| 平罗县| 中西区| 峨山| 会昌县| 都江堰市| 金沙县| 洪湖市| 泰安市| 乡宁县| 正阳县| 玛纳斯县| 玛沁县| 宝山区| 阳城县| 安康市| 辛集市| 简阳市| 潼关县| 富裕县| 谷城县| 砀山县| 米泉市| 普兰县| 武定县| 隆安县| 南陵县| 澄城县| 天门市| 任丘市| 阿荣旗| 邹城市| 霍山县| 彩票| 宁远县| 合江县| 重庆市|