Spring 源碼(14)Spring Bean 的創建過程(5)

来源:https://www.cnblogs.com/redwinter/archive/2022/05/13/16268667.html
-Advertisement-
Play Games

到目前為止,我們知道Spring創建Bean對象有5中方法,分別是: 使用FactoryBean的getObject方法創建 使用BeanPostProcessor的子介面InstantiationAwareBeanPostProcessor的postProcessBeforeInstantiati ...


到目前為止,我們知道Spring創建Bean對象有5中方法,分別是:

  • 使用FactoryBeangetObject方法創建
  • 使用BeanPostProcessor的子介面InstantiationAwareBeanPostProcessorpostProcessBeforeInstantiation方法創建
  • 設置BeanDefinitionSupplier屬性進行創建
  • 設置BeanDefinitionfactory-method進行創建
  • 使用全過程:getBean-->doGetBean-->createBean-->doCreateBean 反射進行創建

前面4中已經介紹,接下來介紹第5種,我們知道如果使用反射創建,那麼必然要知道使用構造函數進行實例化,因為使用構造函數能夠將帶有參數的設置進去。

SmartInstantiationAwareBeanPostProcessor 介面

在前面講過InstantiationAwareBeanPostProcessor 是用來提前實例化對象的,而SmartInstantiationAwareBeanPostProcessorInstantiationAwareBeanPostProcessor 的子介面,他是用來幹啥呢?

createBeanInstance方法中的源碼:

// 省略代碼....
// 明確構造器從BeanPostProcessor中,對應的是 AutowiredAnnotationBeanPostProcessor
// 他是 SmartInstantiationAwareBeanPostProcessor 的子類,使用determineCandidateConstructors進行
// 解析構造函數
Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||
    mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
  return autowireConstructor(beanName, mbd, ctors, args);
}
// 省略代碼....

點進去:

protected Constructor<?>[] determineConstructorsFromBeanPostProcessors(@Nullable Class<?> beanClass, String beanName)
  throws BeansException {

  if (beanClass != null && hasInstantiationAwareBeanPostProcessors()) {
    for (BeanPostProcessor bp : getBeanPostProcessors()) {
      if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {
        SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp;
        // 決定候選的構造函數
        Constructor<?>[] ctors = ibp.determineCandidateConstructors(beanClass, beanName);
        if (ctors != null) {
          return ctors;
        }
      }
    }
  }
  return null;
}

可以看到這個介面是用來解析BeanClass的構造函數的,SmartInstantiationAwareBeanPostProcessor的實現類AutowiredAnnotationBeanPostProcessor,這個類是用來解析確定合適的構造函數,重點解析了@Autowired註解,並且還解析了@Value註解和@Lookup註解。

當解析出來構造函數之後,那麼就調用autowireConstructor方法進行實例化,解析時會new一個構造器解析器ConstructorResolver ,在解析factoryMehod時也是使用的這個類使用的是instantiateUsingFactoryMethod這個方法,並且解析factoryMethod更加複雜,需要判斷是否是靜態的工廠創建還是實例工廠創建,而自動裝配的構造解析相對來說簡單一些,使用autowireConstructor方法進行解析。

最終解析出構造方法和構造參數之後進行實例化:

// 使用合適的構造方法和構造參數進行實例化
bw.setBeanInstance(instantiate(beanName, mbd, constructorToUse, argsToUse));

實例化:

private Object instantiate(
  String beanName, RootBeanDefinition mbd, Constructor<?> constructorToUse, Object[] argsToUse) {

  try {
    // 獲取實例化策略,一般使用 CglibSubClassingInstantiationStrategy
    InstantiationStrategy strategy = this.beanFactory.getInstantiationStrategy();
    if (System.getSecurityManager() != null) {
      return AccessController.doPrivileged((PrivilegedAction<Object>) () ->
                                           strategy.instantiate(mbd, beanName, this.beanFactory, constructorToUse, argsToUse),
                                           this.beanFactory.getAccessControlContext());
    }
    else {
      // 開始實例化
      return strategy.instantiate(mbd, beanName, this.beanFactory, constructorToUse, argsToUse);
    }
  }
  catch (Throwable ex) {
    throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                    "Bean instantiation via constructor failed", ex);
  }
}

public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner,
                          final Constructor<?> ctor, Object... args) {
  if (!bd.hasMethodOverrides()) {
    if (System.getSecurityManager() != null) {
      // use own privileged to change accessibility (when security is on)
      AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
        ReflectionUtils.makeAccessible(ctor);
        return null;
      });
    }
    // 實例化類,反射調用
    return BeanUtils.instantiateClass(ctor, args);
  }
  else {
    // 如果方法被覆蓋,lookup-method 和 replace-method
    return instantiateWithMethodInjection(bd, beanName, owner, ctor, args);
  }
}

如果前面的解析都沒有到Bean,那麼就會使用無參構造函數進行解析:

// 省略代碼....
// Preferred constructors for default construction?
// 首選的構造器為預設的創建方式,使用了@Primary註解的為首選的創建對象方式
ctors = mbd.getPreferredConstructors();
if (ctors != null) {
  return autowireConstructor(beanName, mbd, ctors, null);
}

// No special handling: simply use no-arg constructor.
// 調用無參構造函數實例化對象
return instantiateBean(beanName, mbd);

實例化Bean

protected BeanWrapper instantiateBean(String beanName, RootBeanDefinition mbd) {
  try {
    Object beanInstance;
    if (System.getSecurityManager() != null) {
      beanInstance = AccessController.doPrivileged(
        (PrivilegedAction<Object>) () -> getInstantiationStrategy().instantiate(mbd, beanName, this),
        getAccessControlContext());
    }
    else {
      // 實例化對象,使用反射進行創建
      beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, this);
    }
    // 創建一個Bean的包裝器
    BeanWrapper bw = new BeanWrapperImpl(beanInstance);
    // 初始化Bean的包裝器
    initBeanWrapper(bw);
    return bw;
  }
  catch (Throwable ex) {
    throw new BeanCreationException(
      mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);
  }
}

這裡可以看到前面使用factoryMethodautowireConstructor 解析構造函數進行實例化還是使用無參構造函數進行實例化都是將Bean進行了包裝,那這個包裝有啥作用呢?

BeanWrapper的作用

我們先來看下前面的方法是怎麼創建BeanWrapper的:

factory-method 解析,ConstructorResolver#instantiateUsingFactoryMethod 方法:

public BeanWrapper instantiateUsingFactoryMethod(
			String beanName, RootBeanDefinition mbd, @Nullable Object[] explicitArgs) {
		// 創建一個Bean的包裝器
		BeanWrapperImpl bw = new BeanWrapperImpl();
		this.beanFactory.initBeanWrapper(bw);
		// factoryBean
		Object factoryBean;
		// factory 工廠類
		Class<?> factoryClass;
		// 標識是否是靜態的工廠
		boolean isStatic;
        // 省略代碼....
}  

SmartInstantiationAwareBeanPostProcessor子類AutowiredAnnotationBeanPostProcessor 解析出構造函數,然後使用ConstructorResolver#autowireConstructor 執行:

public BeanWrapper autowireConstructor(String beanName, RootBeanDefinition mbd,
			@Nullable Constructor<?>[] chosenCtors, @Nullable Object[] explicitArgs) {
		// 創建一個包裝器
		BeanWrapperImpl bw = new BeanWrapperImpl();
		// 初始化包裝器
		this.beanFactory.initBeanWrapper(bw);
		// 構造函數
		Constructor<?> constructorToUse = null;
		// 構造參數
		ArgumentsHolder argsHolderToUse = null;
		// 需要使用的構造參數
		Object[] argsToUse = null;
		// 明確的構造參數不為空,則賦值給將要執行實例化的構造參數
		if (explicitArgs != null) {
			argsToUse = explicitArgs;
		}
        // 省略代碼....
}

最終都是會進行轉換服務ConversionServicePropertyEditorRegistry的註冊,一個是用來進行屬性類型轉換的,一個是用來屬性值解析的:

protected void initBeanWrapper(BeanWrapper bw) {
  // 獲取轉換服務放到bean的包裝器中
  bw.setConversionService(getConversionService());
  // 註冊定製的屬性編輯器
  registerCustomEditors(bw);
}

在前面的文章中,介紹了這兩個如何使用,而且還自定義了屬性編輯器和類型轉換,需要的小伙伴可以去看看:

https://www.cnblogs.com/redwinter/p/16167214.htmlhttps://www.cnblogs.com/redwinter/p/16241328.html

到這裡Bean的實例化就完成了,接著往下看源碼:

protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
  throws BeanCreationException {

  // Instantiate the bean.
  BeanWrapper instanceWrapper = null;
  // 從緩存中獲取FactoryBean的Bean對象
  if (mbd.isSingleton()) {
    instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
  }
  if (instanceWrapper == null) {
    // 實例化對象
    instanceWrapper = createBeanInstance(beanName, mbd, args);
  }
  // 從包裝器中獲取Bean對象
  Object bean = instanceWrapper.getWrappedInstance();
  // 從包裝器中獲取Bean類型
  Class<?> beanType = instanceWrapper.getWrappedClass();
  if (beanType != NullBean.class) {
    mbd.resolvedTargetType = beanType;
  }

  // Allow post-processors to modify the merged bean definition.
  synchronized (mbd.postProcessingLock) {
    if (!mbd.postProcessed) {
      try {
        // 合併Bean
        applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
      }
      catch (Throwable ex) {
        throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                        "Post-processing of merged bean definition failed", ex);
      }
      mbd.postProcessed = true;
    }
  }
}

點進去:

protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class<?> beanType, String beanName) {
  for (BeanPostProcessor bp : getBeanPostProcessors()) {
    if (bp instanceof MergedBeanDefinitionPostProcessor) {
      MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp;
      // 執行合併BeanDefinition
      bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName);
    }
  }
}

可以看到這裡出現了一個介面MergedBeanDefinitionPostProcessor,這個介面也是BeanPostProcessor的子介面,那他到底是幹啥用的呢?

MergedBeanDefinitionPostProcessor 介面

點擊發現這個介面的實現類全是跟註解相關的,而最重要的是CommonAnnotationBeanPostProcessor實現類,在構造函數中設置了兩個註解:@PostConstruct@PreDestroy ,一個是在初始化完之後調用,一個是容器銷毀時調用。

未完待續.....


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

-Advertisement-
Play Games
更多相關文章
  • 一、引言 CTE(Common Table Expression) 公用表達式,它是在單個語句的執行範圍內定義的臨時結果集,只在查詢期間有效。它可以自引用,也可在同一查詢中多次引用,實現了代碼段的重覆利用。 CTE最大的好處是提升T-Sql代碼的可讀性,可以以更加優雅簡潔的方式實現遞歸等複雜的查詢。 ...
  • 本文帶你瞭解蘋果 AppStore 的財年和賬單周期,關於 AppStore 開發者賬單和收入,相信很多開發者不一定有接觸,或者接觸時還是有很多疑問沒有時間來學習。另外,還會有一些財年的詭計問題,比如為什麼阿裡巴巴財年是從4月1號到次年的3月31號呢?蘋果財年為什麼這麼奇怪,本文一一為你解答~ ...
  • 5月12日晚上19點,知識賦能第五期第四節課《OpenHarmony標準系統多媒體子系統之音頻解讀》,在OpenHarmony開發者成長計劃社群內成功舉行。 ...
  • 大家好,我是半夏👴,一個剛剛開始寫文的沙雕程式員.如果喜歡我的文章,可以關註➕ 點贊 👍 加我微信:frontendpicker,一起學習交流前端,成為更優秀的工程師~關註公眾號:搞前端的半夏,瞭解更多前端知識! 點我探索新世界! 原文鏈接 ==>http://sylblog.xin/archi ...
  • 最近碰到了非同步編程的問題,決定從原理開始重新擼一遍,徹底弄懂非同步編程。 1.非同步編程思想 非同步編程是為瞭解決同步模式的一些痛點,同步模式中任務是依次執行,後一個任務必須要等待前一個任務結束後才能開始執行,當某個函數耗時過長時就可能造成頁面的假死和卡頓,而非同步編程中,後一個任務不會去等待前一個任務結束 ...
  • 《聲生不息》 是芒果TV、香港電視廣播有限公司和湖南衛視聯合推出的港樂競唱獻禮節目,聽著音樂仿佛回到了那個令人懷念的港風席卷整個亞洲的年代。該節目 Logo 採用經典紅藍配色,無限符號 ∞ 造型,滿滿的設計感。本文在僅採用原生 CSS 的情況下,儘量還原實現該 Logo 造型,本文內容雖然非常簡單,... ...
  • 翻譯自 Tim Sneath 2022年5月12日的文章 《Introducing Flutter 3》 作者 : Tim Sneath 翻譯 : 沙漠盡頭的狼(谷歌翻譯加持) 鏈接 : Introducing Flutter 3(英文原文) 我們在手機、桌面和網站開發上進行多平臺UI開發的歷程達到 ...
  • 1.“new”有什麼不對勁? 在我們沒有接觸到工廠模式(簡單工廠、工廠方法模式、抽象工廠模式)之前,我們實例化對象唯一的方法就是通過“new”關鍵字來完成。但是,大量的使用“new”關鍵字來實例化對象會違背一些設計原則,因為代碼與具體的類型綁在一起,從而導致過多的依賴於細節而非抽象,這樣代碼就很難適 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 JSON Web Token(JWT)是一個非常輕巧的規範。這個規範允許我們使用 JWT 在用戶和伺服器之間傳遞安全可靠的信息。一個 JWT 實際上就是一個字元串,它由三部分組成,頭部、載荷與簽名。前兩部分需要經過 Base64 編碼,後一部分通過前兩部分 Base64 編碼後再加密而成。針對 ...
  • 一:背景 1. 講故事 今天本來想寫一篇 非托管泄露 的生產事故分析,但想著昨天就上了一篇非托管文章,連著寫也沒什麼意思,換個口味吧,剛好前些天有位朋友也找到我,說他們的拍攝監控軟體卡死了,讓我幫忙分析下為什麼會卡死,聽到這種軟體,讓我不禁想起了前些天 在程式員桌子上安裝監控 的新聞,參考如下: 我 ...
  • 文章目錄 介紹 ABP的依賴註入系統是基於Microsoft的依賴註入擴展庫(Microsoft.Extensions.DependencyInjection nuget包)開發的。所以我們採用dotnet自帶的註入方式也是支持的。 由於ABP是一個模塊化框架,因此每個模塊都定義它自己的服務併在它自 ...
  • 前言 外觀模式,英文名稱是:Facade Pattern。我們先從名字上來理解一下“外觀模式”。我看到了“外觀”這個詞語,就想到了“外表”這個詞語,兩者有著很相近的意思。就拿談戀愛來說,“外表”很重要,如果第一眼看著很舒服、有眼緣,那就有交往下去的可能。如果長的“三寸釘、枯樹皮”,估計就夠嗆了。在這 ...
  • 模擬.NET實際應用場景,綜合應用三個主要知識點:一是使用dnSpy反編譯第三庫及調試,二是使用Lib.Harmony庫實現第三庫攔截、偽造,三是實現同一個庫支持多版本同時引用。 ...
  • 通過strimzi部署的kafka集群,如何部署prometheus+grafana去監控呢?官方文檔信息量太大,即便照著做也可能失敗,這裡有一份詳細的保姆級操作指南,助您成功部署監控服務 ...
  • 在工具類中封裝getBean,使用哪個介面來實現 實事上,在工具類中,實現BeanFactoryPostProcessor和ApplicationContextAware介面後,使用它們構造方法里的對象ConfigurableListableBeanFactory和ApplicationContex ...
  • 1章:系統基礎信息模塊詳解 通過第三方模塊獲取伺服器的基本性能、塊設備、網卡介面、網路地址庫等信息。 1.1 系統性能模塊psutil:獲取系統性能信息、記憶體信息、磁碟信息、網路信息、用戶信息等。 1.2 IP地址處理模塊IPy: 處理IP地址,網段等。 1.3 DNS處理模塊dnspython: ...
  • EasyExcel動態表頭導出(支持多級表頭) 在很多業務場景中,都會應用到動態表頭的導出,也會涉及到多級表頭的導出,如下圖所示 通過EasyExcel,我們可以快速實現這一需求,具體代碼如下 DynamicHeader import java.util.List; /** *@Author: <a ...
  • 基於java線上婚紗定製系統設計與實現,可適用於線上婚紗攝影預定系統,基於web的婚紗影樓管理系統設計,基於web的婚紗影樓管理系統設計,婚紗攝影網系統,婚紗攝影網站系統,婚紗攝影網站系統,婚紗系統,婚紗管理系統等等; ...