Spring源碼學習(-)別怕,老外點中餐與AbstractBeanFactory.getBean的主流程差很少

引言

AbstractBeanFactory.getBean的流程,有點像老外點餐,不信我們往下看。。spring

入口

AbstractBeanFactory中有getBean的通用邏輯緩存

//AbstractBeanFactory 中getBean方法第源碼
@Override
public Object getBean(String name, Object... args) throws BeansException {
   return doGetBean(name, null, args, false);
}
複製代碼

核心邏輯在:doGetBean中。bash

邏輯說明:

  1. 獲取BeanName,BeanFactory去掉&,根據別名找到對應的BeanName。(就是老闆翻譯老外說的)。
  2. 嘗試從緩存中獲取單例,若是存在就返回 (就是餐館老闆看看有沒有作好的,有就端出來給老外)。
  3. 檢查是不是原型類型Bean在建立中,若是是,假設存在循環引用,拋出異常 (就是老闆看看是否是正在作,若是說是就無論了)
  4. 找到定義bean對應的BeanFactory(老闆找到張大廚)
  5. 將多個GernericBeanDefinition合併爲RootBeanDefinition,若是Bean有父Bean,時會合並父類的相關屬性。(老闆記錄客人的忌口)
  6. 保證bean的依賴先初始化,對DependOn註解的支持(完成作這道菜的必須工做,例如洗菜)
  7. 按不一樣做用域建立Bean (張大廚作飯)
  8. 若是須要進行類型裝換

玩笑事後讓咱們看點實在的。app

源碼註釋:

/**
 * Return an instance, which may be shared or independent, of 
 the specified bean.
 * 得到一個實例,能夠是共享(原型的),獨立(單例的),指定的
 * @param name the name of the bean to retrieve
 * @param requiredType the required type of the bean to retrieve
 * @param args arguments to use when creating a bean instance 
 * using explicit arguments
 * (only applied when creating a new instance as opposed to 
 retrieving an existing one)
 *              (僅在建立一個新實例時使用,而不是獲取一個已經存在的bean)
 * @param typeCheckOnly whether the instance is obtained 
   for a type check,not for actual use
 *          是否僅僅是類型檢查
 * @return an instance of the bean
 * @throws BeansException if the bean could not be created
 */
@SuppressWarnings("unchecked")
protected <T> T doGetBean(
final String name, @Nullable final Class<T> requiredType,
      @Nullable final Object[] args, boolean typeCheckOnly) 
      throws BeansException {
   //1.獲取對應的beanName ,BeanFactory去掉&,別名找到對應的BeanName
   final String beanName = transformedBeanName(name);
   Object bean;

   // Eagerly check singleton cache for manually registered singletons.
   //2. 嘗試從緩存中獲取單例 Spring會,在Bean未建立完成的狀況下,建立Bean的ObjectFactory對象,提前曝光,以方便解決循環依賴。
   Object sharedInstance = getSingleton(beanName);
   if (sharedInstance != null && args == null) {
      if (logger.isTraceEnabled()) {
         if (isSingletonCurrentlyInCreation(beanName)) {
            logger.trace("Returning eagerly cached instance of singleton bean '" + beanName +
                  "' that is not fully initialized yet - a consequence of a circular reference");
         }
         else {
            logger.trace("Returning cached instance of singleton bean '" + beanName + "'");
         }
      }
      //根據bean實例獲取對象,若是是FactoryBean 獲取它建立的對象
      bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
   }
   else {
      // Fail if we're already creating this bean instance: // We're assumably within a circular reference.
      //3.原型依賴的檢查,若是是原型,假設存在循環引用,拋出異常
      if (isPrototypeCurrentlyInCreation(beanName)) {
         throw new BeanCurrentlyInCreationException(beanName);
      }
      // Check if bean definition exists in this factory.
      // 4.檢查bean,是否認義在了BeanFactory中,
      BeanFactory parentBeanFactory = getParentBeanFactory();
      // 若是父BeanFactory不爲空&&當前並無beanDefinitionMap不包含,
      // 委託給父BeanFactory
      if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
         // Not found -> check parent.
         String nameToLookup = originalBeanName(name);
         if (parentBeanFactory instanceof AbstractBeanFactory) {
            return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
                  nameToLookup, requiredType, args, typeCheckOnly);
         }
         else if (args != null) {
            // Delegation to parent with explicit args.
            //委託給,須要參數的 getBean方法
            return (T) parentBeanFactory.getBean(nameToLookup, args);
         }
         else if (requiredType != null) {
            // No args -> delegate to standard getBean method.
            // 委託給標準的getBean方法
            return parentBeanFactory.getBean(nameToLookup, requiredType);
         }
         else {
            return (T) parentBeanFactory.getBean(nameToLookup);
         }
      }

      if (!typeCheckOnly) {
         //不只是,類型檢查,標記bean爲建立
         // 容許bean能夠從新合併
         markBeanAsCreated(beanName);
      }
      try {
         //5.將GernericBeanDefinition轉換爲RootBeanDefinition,
         //若是Bean有父Bean,時會合並父類的相關屬性。
         final RootBeanDefinition mbd = 
                       getMergedLocalBeanDefinition(beanName);
         checkMergedBeanDefinition(mbd, beanName, args);

         // Guarantee initialization of beans that 
         //  the current bean depends on.
         // 6.保證bean的依賴先初始化了
         String[] dependsOn = mbd.getDependsOn();
         if (dependsOn != null) {
            for (String dep : dependsOn) {
                 // 對應@DependsOn 註解,
                  /**
                   * 檢測是否存在 depends-on 循環依賴,若存在則拋異常。
                   * 好比 A 依賴 B,
                   * B 又依賴 A,他們的配置以下:
                  @Bean(name="a")
                 @DependsOn("b")public A a () {
                          return new A();}

                      @Bean(name = "b")
                      @DependsOn("a")
                     public B b () {
                          return new B();
                      }

                   * a 要求 b 在其以前被建立,但 b 又要求 a 先於它
                   * 建立。這個時候造成了循環,對於 depends-on 循環,Spring 會直接
                   * 拋出異常
                   *
                   *@see org.springframework.context.annotation.ClassPathBeanDefinitionScanner#doScan
                   */
                
               if (isDependent(beanName, dep)) {
                  throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                        "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
               }
               //註冊,Dependent和Bean的關係
               registerDependentBean(dep, beanName);
               try {
                  //先建立Dependent
                  getBean(dep);
               }
               catch (NoSuchBeanDefinitionException ex) {
                  throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                        "'" + beanName + "' depends on missing bean '" + dep + "'", ex);
               }
            }
         }

         // Create bean instance.
         //7.建立Bean的實例
         if (mbd.isSingleton()) {
            sharedInstance = getSingleton(beanName, () -> {
               try {
                  return createBean(beanName, mbd, args);
               }
               catch (BeansException ex) {
                  // Explicitly remove instance from singleton cache: It might have been put there
                  // eagerly by the creation process, to allow for circular reference resolution.
                  // Also remove any beans that received a temporary reference to the bean.
                  destroySingleton(beanName);
                  throw ex;
               }
            });
            bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
         }
         else if (mbd.isPrototype()) {
            // It's a prototype -> create a new instance. Object prototypeInstance = null; try { beforePrototypeCreation(beanName); prototypeInstance = createBean(beanName, mbd, args); } finally { afterPrototypeCreation(beanName); } bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd); } else { String scopeName = mbd.getScope(); final Scope scope = this.scopes.get(scopeName); if (scope == null) { throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'"); } try { Object scopedInstance = scope.get(beanName, () -> { beforePrototypeCreation(beanName); try { return createBean(beanName, mbd, args); } finally { afterPrototypeCreation(beanName); } }); bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd); } catch (IllegalStateException ex) { throw new BeanCreationException(beanName, "Scope '" + scopeName + "' is not active for the current thread; consider " + "defining a scoped proxy for this bean if you intend to refer to it from a singleton", ex); } } } catch (BeansException ex) { cleanupAfterBeanCreationFailure(beanName); throw ex; } } // Check if required type matches the type of the actual bean instance. // 8.進行類型轉換 // requiredType.isInstance(bean) 相似 bean instanceof requiredType // 爲true表示能夠直接返回,或強轉 //clazz.isAssignableFrom(obj.getClass()) == clazz.isInstance(obj) //僅當obj not null if (requiredType != null && !requiredType.isInstance(bean)) { try { T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType); if (convertedBean == null) { throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } return convertedBean; } catch (TypeMismatchException ex) { if (logger.isTraceEnabled()) { logger.trace("Failed to convert bean '" + name + "' to required type '" + ClassUtils.getQualifiedName(requiredType) + "'", ex); } throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } } return (T) bean; } 複製代碼

一圖勝千言(時序圖)

doGetBean時序圖
doGetBean時序圖

當看完主流程時,我有以下疑問:ide

  1. getObjectForBeanInstance的做用?
  2. markBeanAsCreated 和getMergedLocalBeanDefinition的做用
  3. bean的DependsOn是什麼?
  4. createBean的邏輯

getObjectForBeanInstance和createBean的邏輯下一篇再聊,預計20190921前上傳。 這裏先說下2,4。ui

補充說明:

markBeanAsCreated 和getMergedLocalBeanDefinition的做用

簡單說,就是清除原有的RootBeanDefinition,再經過當前的多個GenericBeanDefinition合併成新的RootBeanDefinition,供BeanFactory使用。this

什麼是BeanDefinition

  • BeanDefinition 是一個極簡接口,主要目的是容許BeanFactoryPostProcessor(例如:PropertyPlaceholderConfigurer)去反射和修改屬性值和其餘的bean元數據。
  • GenericBeanDefinition 用於一站式定義標準的bean。和其餘beanDefinition同樣,它容許指定一個類,屬性值,可選的構造器參數值,而且能夠經過配置parentName,來支持派生關係。
  • RootBeanDefinition 表明合併的beanDefinition,Spring的BeanFactory使用RootBeanDefinition建立指定的bean它可能繼承了多個原始的BeanDefinition,這些源BeanDefinition一般是GenericBeanDefinition,RootBeanDefinition本質上是運行時的「統一」bean定義視圖。

若是有興趣能夠搜索url

org.springframework.beans.factory.config.BeanDefinition org.springframework.beans.factory.support.GenericBeanDefinition org.springframework.beans.factory.support.RootBeanDefinitionspa

bean的DependOn是什麼?

我最開始覺得是bean須要依賴,處理Autowired註解,然而不是。 利用IDEA查找了調用BeanDefinition的setDependsOn的地方, .net

發現了AnnotationConfigUtils.processCommonDefinitionAnnotations方法中如以下代碼:

AnnotationAttributes dependsOn = attributesFor(metadata, DependsOn.class);
if (dependsOn != null) {
   abd.setDependsOn(dependsOn.getStringArray("value"));
}
複製代碼

也就是說doGetBean中的

String[] dependsOn = mbd.getDependsOn();
  if (dependsOn != null) {
       .....
       }
複製代碼

是對@DependOn註解的支持,我孤陋寡聞,工做中重來沒有使用過@DependOn註解。從網上查閱了資料。
當時我查下的資料的url:blog.csdn.net/qq_30257149…

@DependOn註解用來表示一個bean A的實例化依賴另外一個bean B的實例化, 可是A並不須要持有一個B的對象

囉嗦幾句

讀源碼不容易,最開始我老是揪住一個方法不放,個把小時下來,已經不知道本身讀到哪裏了,後來老是渾淪吞棗,一味求快,好像看了不少,實際上什麼也不知道。今年9月初,不知道是北京的天氣變涼快了,仍是什麼別的緣由,我本身也不清楚,居然能靜下心來,不求快,一層一層讀,一點一點翻譯,天天僅讀40分鐘,總算感受是明白了點,不由感慨以下:
只抓細節太糊塗
渾淪吞棗淨瞎看
靜心慢讀未必快
反正我是入門了

下一篇解決 createBean 和getObjectForBeanInstance

相關文章
相關標籤/搜索