Spring源碼核心剖析

来源:https://www.cnblogs.com/jingdongkeji/archive/2023/06/21/17495777.html
-Advertisement-
Play Games

SpringAOP作為Spring最核心的能力之一,其重要性不言而喻。然後需要知道的是AOP並不只是Spring特有的功能,而是一種思想,一種通用的功能。而SpringAOP只是在AOP的基礎上將能力集成到SpringIOC中,使其作為bean的一種,從而我們能夠很方便的進行使用。 ...


前言

SpringAOP作為Spring最核心的能力之一,其重要性不言而喻。然後需要知道的是AOP並不只是Spring特有的功能,而是一種思想,一種通用的功能。而SpringAOP只是在AOP的基礎上將能力集成到SpringIOC中,使其作為bean的一種,從而我們能夠很方便的進行使用。

一、SpringAOP的使用方式

1.1 使用場景

當我們在日常業務開發中,例如有些功能模塊是通用的(日誌、許可權等),或者我們需要在某些功能前後去做一些增強,例如在某些方法執行後發送一條mq消息等。

如果我們將這些通用模塊代碼與業務代碼放在一塊,那麼每個業務代碼都要寫這些通用模塊,維護成本與耦合情況都十分嚴重。

因此,我們可以將此模塊抽象出來,就有了”切麵“的概念。

1.2 常用方式

AOP的使用方式相對比較簡單,首先我們需要完成業務代碼

@Service
public class AopDemo implements AopInterface{

    public Student start(String name) {
        System.out.println("執行業務邏輯代碼.....");
        return new Student(name);
    }

}

業務邏輯比較簡單,接收一個name參數。

接下來我們需要創建其對應的切麵

//將該切麵加入spring容器
@Service
//聲明該類為一個切麵
@Aspect
class AopAspect {

    //聲明要進行代理的方法
    @Pointcut("execution(* com.example.demo.aop.AopInterface.start(..))")
    public void startAspect() {
    }

    //在方法執行之前的邏輯
    @Before(value = "startAspect()")
    public void beforeAspect() {
        System.out.println("業務邏輯前代碼.....");
    }

    //在方法執行之後的邏輯
    @After(value = "startAspect()")
    public void afterAspect() {
        System.out.println("業務邏輯後代碼.....");
    }

    //圍繞方法前後的邏輯
    @Around("startAspect()")
    public Object aroundAspect(ProceedingJoinPoint point) throws Throwable {
        Object[] requestParams = point.getArgs();
        String name = requestParams[0].toString();
        System.out.println("傳入參數:" + name);
        requestParams[0] = "bob";
        return point.proceed(requestParams);
    }

}

可以看到,首先需要我們指明要代理的對象及方法,然後根據需要選擇不同的註解即可實現代理對象。

傳入參數:tom
業務邏輯前代碼.....
執行業務邏輯代碼.....
業務邏輯後代碼.....

二、SpringAOP源碼解析

2.1 被代理對象的開始initializeBean

根據上面的使用情況,我們知道只需要聲明對應的註解即可,不需要其他額外的配置,然後我們獲得的bean對象就已經是被代理的了,那麼我們可以推斷代理對象的過程一定是發生在bean創建的過程的。

我們回顧一下創建bean的流程

  1. 實例化bean
  2. 裝配屬性
  3. 初始化bean

只有第三步初始化bean的時候才會有機會進行代理。

找到對應的代碼位置:

protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
   Object wrappedBean = bean;
   if (mbd == null || !mbd.isSynthetic()) {
      //前置處理器
      wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
   }
	 //...
   try {
      //對象的初始化方法
      invokeInitMethods(beanName, wrappedBean, mbd);
   }
   if (mbd == null || !mbd.isSynthetic()) {
      //後置處理器,AOP開始的地方
      wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
   }

   return wrappedBean;
}

2.2 後置處理器applyBeanPostProcessorsAfterInitialization

後置處理器會執行那些實現了後置處理器介面的代碼:

public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
      throws BeansException {

   Object result = existingBean;
   //獲取所有的後置處理器
   for (BeanPostProcessor processor : getBeanPostProcessors()) {
      //實現其要執行的方法
      Object current = processor.postProcessAfterInitialization(result, beanName);
      if (current == null) {
         return result;
      }
      result = current;
   }
   return result;
}

而AOP的後置處理器就是其中的一個: AbstractAutoProxyCreator

其對應的方法為(以下代碼不為同一個類,而是對應的執行順序):

public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) {
   if (bean != null) {
      Object cacheKey = getCacheKey(bean.getClass(), beanName);
      if (this.earlyProxyReferences.remove(cacheKey) != bean) {
         //執行到下麵方法
         return wrapIfNecessary(bean, beanName, cacheKey);
      }
   }
   return bean;
}

protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
		// Create proxy if we have advice.
		Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
		if (specificInterceptors != DO_NOT_PROXY) {
			this.advisedBeans.put(cacheKey, Boolean.TRUE);
      //創建代理對象
			Object proxy = createProxy(
					bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
			this.proxyTypes.put(cacheKey, proxy.getClass());
			return proxy;
		}

		this.advisedBeans.put(cacheKey, Boolean.FALSE);
		return bean;
}

protected Object createProxy(Class beanClass, @Nullable String beanName,
			@Nullable Object[] specificInterceptors, TargetSource targetSource) {

		//獲取advisors
		Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
		proxyFactory.addAdvisors(advisors);
		proxyFactory.setTargetSource(targetSource);
		customizeProxyFactory(proxyFactory);

		proxyFactory.setFrozen(this.freezeProxy);
		if (advisorsPreFiltered()) {
			proxyFactory.setPreFiltered(true);
		}

		// Use original ClassLoader if bean class not locally loaded in overriding class loader
		ClassLoader classLoader = getProxyClassLoader();
		if (classLoader instanceof SmartClassLoader && classLoader != beanClass.getClassLoader()) {
			classLoader = ((SmartClassLoader) classLoader).getOriginalClassLoader();
		}
    //通過代理工廠創建代理對象
		return proxyFactory.getProxy(classLoader);
}

public Object getProxy(@Nullable ClassLoader classLoader) {
    //首先獲取對應的代理
		return createAopProxy().getProxy(classLoader);
}

//該方法根據要被代理的類選擇使用jdk代理還是cglib代理
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
		if (!NativeDetector.inNativeImage() &&
				(config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config))) {
			Class targetClass = config.getTargetClass();
      //如果被代理的類是一個介面則使用jdk代理
			if (targetClass.isInterface() || Proxy.isProxyClass(targetClass) || ClassUtils.isLambdaClass(targetClass)) 			{
				return new JdkDynamicAopProxy(config);
			}
      //否則使用cglib代理
			return new ObjenesisCglibAopProxy(config);
		}
		else {
      //根據配置選擇強制使用jdk代理
			return new JdkDynamicAopProxy(config);
		}
}

我們知道,代理方式有jdk動態代理與cglib動態代理兩種方式,而我們一個bean使用那種代理方式則由上述的方法決定。

至此,我們已經確定了使用那種代理方式獲取代理對象。

2.3 獲取代理對象

從上文中,我們已經確定了選用何種方式構建代理對象。接下來就是通過不同的方式是如何獲取代理對象的。

看懂本章需要實現瞭解jdk動態代理或者cglib動態代理的方式。

2.3.1 JDK代理

首先在獲取代理對象時選擇 JdkDynamicAopProxy

public Object getProxy(@Nullable ClassLoader classLoader) {
   if (logger.isTraceEnabled()) {
      logger.trace("Creating JDK dynamic proxy: " + this.advised.getTargetSource());
   }
   //這裡通過反射創建代理對象
   return Proxy.newProxyInstance(classLoader, this.proxiedInterfaces, this);
}

當被代理對象執行被代理的方法時,會進入到此方法。(jdk動態代理的概念)

JDK通過反射創建對象,效率上來說相對低一些。

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

		try {
			// 獲取被代理對象的所有切入點
			List chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);

			// 如果調用鏈路為空說明沒有需要執行的切入點,直接執行對應的方法即可
			if (chain.isEmpty()) {
				// We can skip creating a MethodInvocation: just invoke the target directly
				// Note that the final invoker must be an InvokerInterceptor so we know it does
				// nothing but a reflective operation on the target, and no hot swapping or fancy proxying.
				Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
				retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse);
			}
			else {
				// 如果有切入點的話則按照切入點順序開始執行
				MethodInvocation invocation =
						new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
				// Proceed to the joinpoint through the interceptor chain.
				retVal = invocation.proceed();
			}
			
			return retVal;
		}
}

invocation.proceed();這個方法就是通過遞歸的方式執行所有的調用鏈路。

public Object proceed() throws Throwable {
   // We start with an index of -1 and increment early.
   if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
      return invokeJoinpoint();
   }

   Object interceptorOrInterceptionAdvice =
         this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
   if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
      InterceptorAndDynamicMethodMatcher dm =
            (InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice;
      Class targetClass = (this.targetClass != null ? this.targetClass : this.method.getDeclaringClass());
      if (dm.methodMatcher.matches(this.method, targetClass, this.arguments)) {
         return dm.interceptor.invoke(this);
      }
      else {
         // 繼續執行
         return proceed();
      }
   }
   else {
      // 如果調用鏈路還持續的話,下一個方法仍會調用proceed()
      return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
   }
}

2.3.2 cglib代理

public Object getProxy(@Nullable ClassLoader classLoader) {

   try {
      //配置CGLIB Enhancer...
      Enhancer enhancer = createEnhancer();
      if (classLoader != null) {
         enhancer.setClassLoader(classLoader);
         if (classLoader instanceof SmartClassLoader &&
               ((SmartClassLoader) classLoader).isClassReloadable(proxySuperClass)) {
            enhancer.setUseCache(false);
         }
      }
      enhancer.setSuperclass(proxySuperClass);
      enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised));
      enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
      enhancer.setStrategy(new ClassLoaderAwareGeneratorStrategy(classLoader));

      //1.獲取回調函數,對於代理類上所有方法的調用,都會調用CallBack,而Callback則需要實現intercept()方法
      Callback[] callbacks = getCallbacks(rootClass);
      Class[] types = new Class[callbacks.length];
      for (int x = 0; x < types.length; x++) {
         types[x] = callbacks[x].getClass();
      }
      // fixedInterceptorMap only populated at this point, after getCallbacks call above
      enhancer.setCallbackFilter(new ProxyCallbackFilter(
            this.advised.getConfigurationOnlyCopy(), this.fixedInterceptorMap, this.fixedInterceptorOffset));
      enhancer.setCallbackTypes(types);

      //2.創建代理對象
      return createProxyClassAndInstance(enhancer, callbacks);
   }
   catch (CodeGenerationException | IllegalArgumentException ex) {
      throw new AopConfigException("Could not generate CGLIB subclass of " + this.advised.getTargetClass() +
            ": Common causes of this problem include using a final class or a non-visible class",
            ex);
   }
   catch (Throwable ex) {
      // TargetSource.getTarget() failed
      throw new AopConfigException("Unexpected AOP exception", ex);
   }
}

可以看到我們在創建代理對象前會先獲取代理對象的所有回調函數:

首先可以看到我們一共有7個回調方法,其中第一個為AOP相關的方法,其他的為spring相關。

在第一個對調對象中持有的 advised 對象中有 advisors 屬性,就是對應我們的代理類中四個切片,@Before等等。

然後我們看一下 createProxyClassAndInstance()都做了什麼。

//CglibAopProxy類的創建代理對象方法
protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) {
   enhancer.setInterceptDuringConstruction(false);
   enhancer.setCallbacks(callbacks);
   return (this.constructorArgs != null && this.constructorArgTypes != null ?
         enhancer.create(this.constructorArgTypes, this.constructorArgs) :
         enhancer.create());
}

//ObjenesisCglibAopProxy繼承了CglibAopProxy類,並覆寫了其方法
protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) {
		Class proxyClass = enhancer.createClass();
		Object proxyInstance = null;

    //1.嘗試使用objenesis創建對象
		if (objenesis.isWorthTrying()) {
			try {
				proxyInstance = objenesis.newInstance(proxyClass, enhancer.getUseCache());
			}
			catch (Throwable ex) {
				logger.debug("Unable to instantiate proxy using Objenesis, " +
						"falling back to regular proxy construction", ex);
			}
		}

    //2.根據commit的提交記錄發現,objenesis有可能創建對象失敗,如果失敗的話則選用放射的方式創建對象
		if (proxyInstance == null) {
			// Regular instantiation via default constructor...
			try {
				Constructor ctor = (this.constructorArgs != null ?
						proxyClass.getDeclaredConstructor(this.constructorArgTypes) :
						proxyClass.getDeclaredConstructor());
				ReflectionUtils.makeAccessible(ctor);
				proxyInstance = (this.constructorArgs != null ?
						ctor.newInstance(this.constructorArgs) : ctor.newInstance());
			}
			catch (Throwable ex) {
				throw new AopConfigException("Unable to instantiate proxy using Objenesis, " +
						"and regular proxy instantiation via default constructor fails as well", ex);
			}
		}

    //
		((Factory) proxyInstance).setCallbacks(callbacks);
		return proxyInstance;
	}

2.3.3 cglib

此處有個遇到的問題,當我在debug的時候,發現怎麼都進不去 createProxyClassAndInstance(),百思不得其解,然後看到IDEA旁邊有一個向下的箭頭,代表該方法可能其子類被覆寫了。然後在其子類處打斷點果然發現是其子類的實現。

此處在2.2中也可看到:

可以看到返回的是其子類的對象,而不是CglibAopProxy本身的對象。

作者:京東科技 南韓凱

來源:京東雲開發者社區


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • ## 1、簡介 SpringBoot不僅繼承了Spring框架原有的優秀特性,而且還通過簡化配置來進一步簡化了Spring應用的整個搭建和開發過程。 在Spring-Boot項目開發中,存在著本模塊的代碼需要訪問外面模塊介面,或外部url鏈接的需求, 比如在apaas開發過程中需要封裝介面在介面中調 ...
  • ## 前言 在C語言中,指針函數和函數指針是強大且常用的工具。它們允許我們以更靈活的方式處理函數和數據,進而擴展程式的功能。 本文將介紹指針函數和函數指針的概念,並講解一些常見的應用示例。 ## 一、人物簡介 - 第一位閃亮登場,有請今後會一直教我們C語言的老師 —— 自在。 ![](https:/ ...
  • 基於java的資源博客論壇系統設計與實現,可適用於java個人博客系統,個人資源博客管理系統,java博客系統,java論壇系統,類似於交友微博,新浪微博,發表動態,筆記博客,個人筆記系統。 ...
  • 引言 在日常使用的有些APP中,想什麼微信,百度地圖,可以可以搜尋附近的人,距離自己多遠,以及在地圖上我們可以搜索附近的某個地點,距離自己的位置。針對這種類似的功能,我們可以通過redis就能實現。 redis在3.2版本之後也提供了地理位置的能力,使用redis可以輕鬆實現查找附近的人 一:附近的 ...
  • # 概念 在Java中,`CountDownLatch`是一個線程同步的輔助類,用於等待其他線程完成操作。如果`CountDownLatch`實例被丟失或無法訪問,可能會導致無法正常使用該對象。這可能會導致等待線程永遠處於等待狀態,無法繼續執行。 如果意外丟失了`CountDownLatch`對象, ...
  • 毋庸諱言,密碼是極其偉大的發明,但拜病毒和黑客所賜,一旦密碼泄露,我們就得絞盡腦汁再想另外一個密碼,但記憶力並不是一個靠譜的東西,一旦遺忘密碼,也會造成嚴重的後果,2023年業界巨頭Google已經率先支持了Passkeys登錄方式,只須在設備上利用PIN碼解鎖、指紋或面部辨識等生物識別方式,即可驗 ...
  • `numpy`提供了簡單靈活的介面,用於優化數據數組的計算。 通用計算最大的優勢在於通過向量化操作,將迴圈推送至`numpy`之下的編譯層,從而取得更快的執行效率。 `numpy`的通用計算讓我們計算數組時就像計算單獨一個變數一樣, 不用寫迴圈去遍曆數組中的各個元素。 比如,對於一般的`python ...
  • 在SpringBoot中,EnableAutoConfiguration註解用於開啟自動裝配功能。 本文將詳細分析該註解的工作流程。 # EnableAutoConfiguration註解 啟用SpringBoot自動裝配功能,嘗試猜測和配置可能需要的組件Bean。 自動裝配類通常是根據類路徑和定義 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...