Spring原始碼淺析之bean例項的建立過程(二)

狐言不胡言 發表於 2021-09-03
Spring

在上一篇內容中,介紹了doGetBean方法的原始碼內容,知道了bean在建立的過程中,有三個範圍,單例、多例、Scope,裡面都使用到了createBean。下面本篇文章的主要內容,就是圍繞createBean來進行展開。

createBean方法

/**
 * Create a bean instance for the given merged bean definition (and arguments).
 * The bean definition will already have been merged with the parent definition
 * in case of a child definition.
 * <p>All bean retrieval methods delegate to this method for actual bean creation.
 * @param beanName the name of the bean
 * @param mbd the merged bean definition for the bean
 * @param args explicit arguments to use for constructor or factory method invocation
 * @return a new instance of the bean
 * @throws BeanCreationException if the bean could not be created
 */
protected abstract Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
      throws BeanCreationException;

在AbstractBeanFactory類中,有createBean介面,具體的建立過程交給了子類進行實現:AbstractAutowireCapableBeanFactory

/**
 * Central method of this class: creates a bean instance,
 * populates the bean instance, applies post-processors, etc.
 * 建立bean例項、填充bean例項,以及進行一些後置處理
 * @see #doCreateBean
 */
@Override
protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
      throws BeanCreationException {

   if (logger.isTraceEnabled()) {
      logger.trace("Creating instance of bean '" + beanName + "'");
   }
   RootBeanDefinition mbdToUse = mbd;

   //將bean類名解析為class引用
   Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
   //如果resolvedClass不為空,且bean定義中沒有beanClass,且bean定義擁有beanClassName
   if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
      mbdToUse = new RootBeanDefinition(mbd);
      mbdToUse.setBeanClass(resolvedClass);
   }

   // Prepare method overrides.
   //準備方法覆蓋
   try {
      mbdToUse.prepareMethodOverrides();
   }
   catch (BeanDefinitionValidationException ex) {
      throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
            beanName, "Validation of method overrides failed", ex);
   }

   try {
      // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
      //給BeanPostProcessors一個建立代理物件的機會
      Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
      if (bean != null) {
         return bean;
      }
   }
   catch (Throwable ex) {
      throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
            "BeanPostProcessor before instantiation of bean failed", ex);
   }

   try {
      //建立原生的bean例項
      Object beanInstance = doCreateBean(beanName, mbdToUse, args);
      if (logger.isTraceEnabled()) {
         logger.trace("Finished creating instance of bean '" + beanName + "'");
      }
      return beanInstance;
   }
   catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {
      // A previously detected exception with proper bean creation context already,
      // or illegal singleton state to be communicated up to DefaultSingletonBeanRegistry.
      throw ex;
   }
   catch (Throwable ex) {
      throw new BeanCreationException(
            mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex);
   }
}

mbdToUse.prepareMethodOverrides()方法的作用,就是驗證當前方法是不是被過載了,如果這個方法只過載了一次,那麼就設定overloaded為false,來避免引數型別的檢查。因為如果這個方法被過載多次,那麼在例項化bean例項的時候,就會根據引數型別進行匹配,這一步消耗的時間比較多。

在Spring裡面,支援兩種方法的覆蓋:lookup-method和replace-method,下面簡單看下lookup-method的程式碼示例:

public class User {
   public void showUser() {
       System.out.println("使用者。。。。。。");
   }
}
public class Student extends User {

    @Override
    public void showUser() {
        System.out.println("學生。。。。。。");
    }
}
public abstract class DemoTest {

    public void showUser() {
        getBean().showUser();
    }

    public abstract User getBean();

    public abstract User getBean(String name);
}
<bean id="demoTest" class="edu.demo.spring.instantiate.DemoTest" >
   <lookup-method name="getBean" bean="user"></lookup-method>
</bean>
<bean id="student" class="edu.demo.spring.instantiate.Student" />
<bean id="user" class="edu.dongnao.courseware.spring.instantiate.User" />

resolveBeforeInstantiation方法就是通過呼叫InstantiationAwareBeanPostProcessor裡面的方法,在bean例項化的前後進行一些處理,這裡是一個擴充套件點,它會返回bean例項的代理物件,來干涉bean的例項化。

//包可見欄位,表示bean例項化前後的處理器已經啟動
@Nullable
volatile Boolean beforeInstantiationResolved;
@Nullable
protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
   Object bean = null;
   //如果bean例項化前後的處理器已經啟動,就執行下面的程式碼
   if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
      // Make sure bean class is actually resolved at this point.
      //在這裡要確保bean類已經被實際解析了
      //如果bean不是Spring容器自己定義的,並且持有InstantiationAwareBeanPostProcessors
      if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
         //確定給定bean定義的目標型別
         Class<?> targetType = determineTargetType(beanName, mbd);
         if (targetType != null) {
            //bean例項化前的處理
            bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
            if (bean != null) {
               //bean例項化後的處理
               bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
            }
         }
      }
      mbd.beforeInstantiationResolved = (bean != null);
   }
   return bean;
}

doCreateBean方法

/** Cache of unfinished FactoryBean instances: FactoryBean name to BeanWrapper. */
private final ConcurrentMap<String, BeanWrapper> factoryBeanInstanceCache = new ConcurrentHashMap<>();
protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
      throws BeanCreationException {

   //例項化bean定義
   BeanWrapper instanceWrapper = null;
   //如果bean是單例的,那就先從快取中獲取,然後再進行移除
   if (mbd.isSingleton()) {
      instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
   }
   //這一步就是建立bean例項的主要步驟,裡面使用了一些簡單的策略,來例項化bean
   //工廠方法、建構函式,簡單初始化
   if (instanceWrapper == null) {
      instanceWrapper = createBeanInstance(beanName, mbd, args);
   }
   //獲取包裝之後的例項物件
   Object bean = instanceWrapper.getWrappedInstance();
   //獲取包裝之後的例項物件的型別
   Class<?> beanType = instanceWrapper.getWrappedClass();
   //如果型別不是空bean,就進行賦值
   if (beanType != NullBean.class) {
      mbd.resolvedTargetType = beanType;
   }
}

在bean例項化的時候,把bean封裝成了BeanWrapper,BeanWrapper主要有下面的作用:

  • Bean的包裝
  • 屬性編輯器
  • 屬性編輯器登錄檔
  • 型別轉換器

createBeanInstance方法

/** Common lock for the four constructor fields below. */
final Object constructorArgumentLock = new Object();

/** Package-visible field for caching the resolved constructor or factory method. */
//包可見欄位,用來快取解析的建構函式和工廠方法
@Nullable
Executable resolvedConstructorOrFactoryMethod;

/** Package-visible field that marks the constructor arguments as resolved. */
//包可見欄位,用來標識建構函式的引數已經解析了
boolean constructorArgumentsResolved = false;
/**
 * 使用例項化策略,為指定的bean建立一個例項
 * 工廠方法、構造器的自動裝配、簡單例項化
 */
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
   // Make sure bean class is actually resolved at this point.
   //解析獲得bean對應的class
   Class<?> beanClass = resolveBeanClass(mbd, beanName);

   //如果beanClass不為空,並且beanClass類的修飾符不是public
   //而且不允許訪問非公共的建構函式和方法,那麼就丟擲異常
   if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed()) {
      throw new BeanCreationException(mbd.getResourceDescription(), beanName,
            "Bean class isn't public, and non-public access not allowed: " + beanClass.getName());
   }

   //如果存在Supplier例項化回撥介面,那麼就使用給定的回撥方法來建立一個例項物件
   Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
   if (instanceSupplier != null) {
      return obtainFromSupplier(instanceSupplier, beanName);
   }

   //如果存在工廠方法,即配置了'factory-method',那麼就呼叫該方法來建立一個例項物件
   if (mbd.getFactoryMethodName() != null) {
      return instantiateUsingFactoryMethod(beanName, mbd, args);
   }

   // Shortcut when re-creating the same bean...
   //在這裡,主要就是判斷bean定義的構造方法是不是已經解析了
   //因為找到匹配的構造方法是一個比較繁瑣的過程,所以這裡在找到後,會設定到bean定義中
   //避免重複的去尋找匹配的構造方法
   boolean resolved = false;
   boolean autowireNecessary = false;
   if (args == null) {
      //加鎖
      synchronized (mbd.constructorArgumentLock) {
         //構造方法已經解析出來了
         if (mbd.resolvedConstructorOrFactoryMethod != null) {
            resolved = true;
            //方法有引數的話,這裡進行設定一下
            autowireNecessary = mbd.constructorArgumentsResolved;
         }
      }
   }
   //如果構造方法或者工廠方法已經解析出來了
   if (resolved) {
      //如果有引數,就使用下面的方法進行例項化bean
      if (autowireNecessary) {
         return autowireConstructor(beanName, mbd, null, null);
      }
      else {
         //沒有引數就使用下面的方法進行例項化bean
         return instantiateBean(beanName, mbd);
      }
   }

   // Candidate constructors for autowiring?
   //如果上面都沒有例項化bean,則意味著構造方法或者工廠方法還沒有被解析
   
   //通過SmartInstantiationAwareBeanPostProcessor來獲取一些構造方法
   Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
   //如果構造方法不為空,是通過構造器注入的,構造方法持有構造引數,或者定義了一些引數
   if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||
         mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
      return autowireConstructor(beanName, mbd, ctors, args);
   }

   // Preferred constructors for default construction?
   //獲取優先的建構函式
   ctors = mbd.getPreferredConstructors();
   if (ctors != null) {
      return autowireConstructor(beanName, mbd, ctors, null);
   }

   // No special handling: simply use no-arg constructor.
   //如果上面都沒有例項化,那麼就使用預設的建構函式進行例項化,即無參的建構函式
   return instantiateBean(beanName, mbd);
}

總結一下步驟:

  1. 首先解析獲取到beanName對應的beanClass
  2. 然後進行了判斷,beanClass不是空的,修飾符不是public,且不允許訪問非公共的方法,就丟擲異常
  3. 如果存在Supplier例項化回撥介面,那麼就使用給定的回撥方法來建立一個例項物件,裡面呼叫了方法obtainFromSupplier
  4. 如果配置了factory-method,那麼就使用該工廠方法來例項化bean,呼叫了方法instantiateUsingFactoryMethod
  5. 接下來就是一波判斷,判斷該bean定義的構造方法是不是已經解析出來了,是不是有引數,引數是不是已經解析出來了
  6. 如果構造方法已經解析出來,且有引數的話,就呼叫autowireConstructor方法來例項化bean,如果沒有引數,就呼叫instantiateBean方法來例項化bean
  7. 如果上面都沒有例項化bean,就獲取bean定義的一些構造方法,如果獲取到的構造方法不是空的,並且是通過構造器注入的,且構造方法定義了一些引數,或者通過getBean外部傳進來了一些引數,就呼叫autowireConstructor方法來例項化bean
  8. 如果還是沒有例項化,就獲取優先的構造方法,如果獲取到了,就呼叫autowireConstructor方法來例項化bean
  9. 最後,上面都沒有例項化bean,就使用預設的構造方法,即無參建構函式來進行例項化bean,呼叫了instantiateBean方法

obtainFromSupplier方法

/**
 * The name of the currently created bean, for implicit dependency registration
 * on getBean etc invocations triggered from a user-specified Supplier callback.
 */
private final NamedThreadLocal<String> currentlyCreatedBean = new NamedThreadLocal<>("Currently created bean");
/**
 * 從給定的供應商來獲取一個bean例項
 * @param instanceSupplier the configured supplier
 * @param beanName the corresponding bean name
 * @return a BeanWrapper for the new instance
 * @since 5.0
 * @see #getObjectForBeanInstance
 */
protected BeanWrapper obtainFromSupplier(Supplier<?> instanceSupplier, String beanName) {
   Object instance;

   //獲取當前執行緒建立的bean的名稱
   String outerBean = this.currentlyCreatedBean.get();
   //設定當前執行緒建立的bean的名稱
   this.currentlyCreatedBean.set(beanName);
   try {
      //通過呼叫Supplier的get方法,返回一個bean例項
      instance = instanceSupplier.get();
   }
   finally {
      if (outerBean != null) {
         //設定當前執行緒建立的bean的名稱
         this.currentlyCreatedBean.set(outerBean);
      }
      else {
         //移除
         this.currentlyCreatedBean.remove();
      }
   }

   //如果instance為空,就建立NullBean 空物件
   if (instance == null) {
      instance = new NullBean();
   }
   //把例項化的bean封裝成BeanWrapper
   BeanWrapper bw = new BeanWrapperImpl(instance);
   //初始化BeanWrapper物件
   initBeanWrapper(bw);
   //最後返回出去
   return bw;
}

總結步驟:

  1. 呼叫Supplier的get方法返回一個bean例項物件
  2. 使用BeanWrapper對bean例項物件進行包裝
  3. 初始化BeanWrapper物件

Supplier介面也是用來建立物件的,這裡可以替代bean工廠,簡單的使用例子如下:

public class SupplierBean {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

        GenericBeanDefinition definition = new GenericBeanDefinition();
        definition.setBeanClass(People.class);
        definition.setInstanceSupplier(SupplierBean::getPeople);
        context.registerBeanDefinition("user2", definition);
        context.refresh();
    }


    private static People getPeople() {
        return new People("翠花");
    }

    static class People {

        private String name;

        public People(String name) {
            this.name = name;
        }


    }
}

instantiateUsingFactoryMethod方法

protected BeanWrapper instantiateUsingFactoryMethod(
      String beanName, RootBeanDefinition mbd, @Nullable Object[] explicitArgs) {

   return new ConstructorResolver(this).instantiateUsingFactoryMethod(beanName, mbd, explicitArgs);
}

建立ConstructorResolver物件,然後呼叫了instantiateUsingFactoryMethod方法:

public BeanWrapper instantiateUsingFactoryMethod(
      String beanName, RootBeanDefinition mbd, @Nullable Object[] explicitArgs) {
}

上面的方法比較長,下面按照順序分段進行。

//建立BeanWrapperImpl物件
BeanWrapperImpl bw = new BeanWrapperImpl();
//初始化BeanWrapperImpl,設定ConversionService型別轉換器
//並且註冊了自定義的屬性編輯器
this.beanFactory.initBeanWrapper(bw);

//工廠bean
Object factoryBean;
//工廠方法所在的類
Class<?> factoryClass;
//是不是靜態工廠
boolean isStatic;

//獲取工廠bean的名稱
String factoryBeanName = mbd.getFactoryBeanName();
//如果工廠bean的名稱不為空,即沒有配置factory-bean,
//意味著這是一個非靜態工廠
if (factoryBeanName != null) {
   //如果bean定義裡面獲取的bean匹配上正在建立的bean,則拋異常
   //工廠bean引用指向了相同的bean定義
   if (factoryBeanName.equals(beanName)) {
      throw new BeanDefinitionStoreException(mbd.getResourceDescription(), beanName,
            "factory-bean reference points back to the same bean definition");
   }
   //獲取工廠bean,即工廠方法所在類的bean,不然的話沒辦法呼叫工廠方法
   factoryBean = this.beanFactory.getBean(factoryBeanName);
   //如果bean定義是單例的,並且bean工廠中存在了這個bean,則拋異常,重複建立
   if (mbd.isSingleton() && this.beanFactory.containsSingleton(beanName)) {
      throw new ImplicitlyAppearedSingletonException();
   }
   //獲取工廠的類
   factoryClass = factoryBean.getClass();
   //標記為非靜態工廠
   isStatic = false;
}
else {
   // It's a static factory method on the bean class.
   //這是一個靜態工廠,如果找不到對應的beanClass,那麼就無法呼叫方法,丟擲異常
   if (!mbd.hasBeanClass()) {
      throw new BeanDefinitionStoreException(mbd.getResourceDescription(), beanName,
            "bean definition declares neither a bean class nor a factory-bean reference");
   }
   //靜態工廠不需要工廠bean,這裡設定為null
   factoryBean = null;
   //獲取到工廠類beanClass
   factoryClass = mbd.getBeanClass();
   //標記為靜態工廠
   isStatic = true;
}

上面一部分程式碼的作用,主要就是用來獲取工廠方法相關的資訊,繼續往下檢視:

//包可見欄位,標誌著建構函式已經被解析
boolean constructorArgumentsResolved = false;

//包可見欄位,快取中已經完全解析的引數欄位
@Nullable
Object[] resolvedConstructorArguments;

//包可見欄位,快取中準備解析的引數欄位
@Nullable
Object[] preparedConstructorArguments;
//工廠方法物件
Method factoryMethodToUse = null;
ArgumentsHolder argsHolderToUse = null;
//相關的引數
Object[] argsToUse = null;

//如果是通過getBean方法指定了引數,那麼就直接使用
if (explicitArgs != null) {
   argsToUse = explicitArgs;
}
//否則,就通過bean定義來獲取工廠方法和引數
else {
   Object[] argsToResolve = null;
   //加鎖
   synchronized (mbd.constructorArgumentLock) {
      factoryMethodToUse = (Method) mbd.resolvedConstructorOrFactoryMethod;
      //如果工廠方法已經被解析過了,並且引數也被解析了
      if (factoryMethodToUse != null && mbd.constructorArgumentsResolved) {
         // Found a cached factory method...
         //解析到的引數
         argsToUse = mbd.resolvedConstructorArguments;
         //如果為空,那就嘗試去獲取未被解析過的引數
         if (argsToUse == null) {
            argsToResolve = mbd.preparedConstructorArguments;
         }
      }
   }
   //如果獲取到了未被解析的引數,那就呼叫下面的方法進行解析
   if (argsToResolve != null) {
      //處理引數值,進行一些型別轉換,比如把配置的String型別轉為Int型別:A("1")轉為A(1)
      argsToUse = resolvePreparedArguments(beanName, mbd, bw, factoryMethodToUse, argsToResolve, true);
   }
}

上面的原始碼內容,是嘗試從快取中獲取工廠方法和引數,獲取不到就走下面的程式碼:

//如果上面沒有獲取到工廠方法和對應的引數,就走下面的程式碼
if (factoryMethodToUse == null || argsToUse == null) {
   // Need to determine the factory method...
   // Try all methods with this name to see if they match the given arguments.
   //獲取工廠方法所在類的例項class,因為它可能是cglib包裝過的類
   factoryClass = ClassUtils.getUserClass(factoryClass);

   List<Method> candidates = null;
   //
   if (mbd.isFactoryMethodUnique) {
      //獲取工廠方法
      if (factoryMethodToUse == null) {
         factoryMethodToUse = mbd.getResolvedFactoryMethod();
      }
      //獲取所有候選的工廠方法
      if (factoryMethodToUse != null) {
         candidates = Collections.singletonList(factoryMethodToUse);
      }
   }
   //如果候選的工廠方法為空
   if (candidates == null) {
      candidates = new ArrayList<>();
      //獲取工廠方法所在的類中所有的方法
      Method[] rawCandidates = getCandidateMethods(factoryClass, mbd);
      //遍歷進行過濾
      for (Method candidate : rawCandidates) {
         //是不是和上面的isStatic進行匹配
         //是不是和定義的工廠方法名稱一樣,是的話就加入到候選方法的集合
         if (Modifier.isStatic(candidate.getModifiers()) == isStatic && mbd.isFactoryMethod(candidate)) {
            candidates.add(candidate);
         }
      }
   }
   
   //如果只找到一個匹配的方法,並且getBean裡面傳進來的引數explicitArgs是空的,
   //並且bean定義裡面也沒有引數,就直接呼叫這個方法進行例項化,然後返回
  if (candidates.size() == 1 && explicitArgs == null && !mbd.hasConstructorArgumentValues()) {
   Method uniqueCandidate = candidates.get(0);
   if (uniqueCandidate.getParameterCount() == 0) {
      mbd.factoryMethodToIntrospect = uniqueCandidate;
      synchronized (mbd.constructorArgumentLock) {
         mbd.resolvedConstructorOrFactoryMethod = uniqueCandidate;
         mbd.constructorArgumentsResolved = true;
         mbd.resolvedConstructorArguments = EMPTY_ARGS;
      }
      bw.setBeanInstance(instantiate(beanName, mbd, factoryBean, uniqueCandidate, EMPTY_ARGS));
      return bw;
   }
}

通過上面的程式碼,找到了所有匹配的工廠方法,那麼到底哪個方法是真正匹配上的呢,繼續往下看:

//如果找到的工廠方法大於1,先進行排序
if (candidates.size() > 1) {  // explicitly skip immutable singletonList
  //public修飾的建構函式優先,然後根據引數數量降序
  //非public的建構函式根據引數數量降序
   candidates.sort(AutowireUtils.EXECUTABLE_COMPARATOR);
}

//用來存放解析後的方法的引數值
ConstructorArgumentValues resolvedValues = null;
//是否是構造器注入的
boolean autowiring = (mbd.getResolvedAutowireMode() == AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR);
int minTypeDiffWeight = Integer.MAX_VALUE;
//匹配方法的集合
Set<Method> ambiguousFactoryMethods = null;

//確定方法引數的入引數量,匹配的方法的引數要等於或者多餘它

//方法的引數數量的最小值
int minNrOfArgs;
//如果getBean裡面指定了引數,那直接使用它作為引數數量的最小值
if (explicitArgs != null) {
   minNrOfArgs = explicitArgs.length;
}
//否則,從bean定義中獲取引數的最小值
else {
   // We don't have arguments passed in programmatically, so we need to resolve the
   // arguments specified in the constructor arguments held in the bean definition.
   //如果bean定義中有引數值
   if (mbd.hasConstructorArgumentValues()) {
      //獲取到方法的引數
      ConstructorArgumentValues cargs = mbd.getConstructorArgumentValues();
      resolvedValues = new ConstructorArgumentValues();
      //解析定義的引數值,並返回引數數量
      minNrOfArgs = resolveConstructorArguments(beanName, mbd, bw, cargs, resolvedValues);
   }
   else {
     //無參,引數最小值為0
      minNrOfArgs = 0;
   }
}

//記錄UnsatisfiedDependencyException異常的集合
LinkedList<UnsatisfiedDependencyException> causes = null;

上面一段程式碼,首先對找到的工廠方法進行了排序,然後確定方法引數的入引數量,後面要找匹配的方法,就是根據引數數量及其型別進行匹配了。

//遍歷候選的方法
for (Method candidate : candidates) {
   //獲取到方法引數的數量
   int parameterCount = candidate.getParameterCount();

   //方法引數的數量必須要大於或者等於最小引數值
   if (parameterCount >= minNrOfArgs) {
      //儲存引數的物件
      ArgumentsHolder argsHolder;

      //獲取方法引數的型別
      Class<?>[] paramTypes = candidate.getParameterTypes();
      //如果通過getBean指定了引數,直接使用
      if (explicitArgs != null) {
         // Explicit arguments given -> arguments length must match exactly.
         //指定的引數,引數長度必須完全匹配
         if (paramTypes.length != explicitArgs.length) {
            continue;
         }
         //建立ArgumentsHolder物件
         argsHolder = new ArgumentsHolder(explicitArgs);
      }
      else {
         //否則使用下面的程式碼解析引數
         // Resolved constructor arguments: type conversion and/or autowiring necessary.
         try {
            String[] paramNames = null;
            //獲取到引數名稱探測器
            ParameterNameDiscoverer pnd = this.beanFactory.getParameterNameDiscoverer();
            if (pnd != null) {
               //獲取到方法的引數名稱
               paramNames = pnd.getParameterNames(candidate);
            }
            //在給定解析引數的情況下,建立一個ArgumentsHolder物件
            argsHolder = createArgumentArray(beanName, mbd, resolvedValues, bw,
                  paramTypes, paramNames, candidate, autowiring, candidates.size() == 1);
         }
         catch (UnsatisfiedDependencyException ex) {
            if (logger.isTraceEnabled()) {
               logger.trace("Ignoring factory method [" + candidate + "] of bean '" + beanName + "': " + ex);
            }
            // Swallow and try next overloaded factory method.
            if (causes == null) {
               causes = new LinkedList<>();
            }
            causes.add(ex);
            continue;
         }
      }

      //根據權重來獲取最匹配的方法
      //判斷是在寬鬆模式還是在嚴格模式下進行解析
      //寬鬆模式:使用具有“最接近的模式”來進行匹配
      //嚴格模式:解析建構函式時,必須所有的都要進行匹配,否則丟擲異常
      int typeDiffWeight = (mbd.isLenientConstructorResolution() ?
            argsHolder.getTypeDifferenceWeight(paramTypes) : argsHolder.getAssignabilityWeight(paramTypes));
      // Choose this factory method if it represents the closest match.
      //如果該工廠方法作為接近,那就使用該工廠方法
      if (typeDiffWeight < minTypeDiffWeight) {
         factoryMethodToUse = candidate;
         argsHolderToUse = argsHolder;
         argsToUse = argsHolder.arguments;
         minTypeDiffWeight = typeDiffWeight;
         ambiguousFactoryMethods = null;
      }
      // Find out about ambiguity: In case of the same type difference weight
      // for methods with the same number of parameters, collect such candidates
      // and eventually raise an ambiguity exception.
      // However, only perform that check in non-lenient constructor resolution mode,
      // and explicitly ignore overridden methods (with the same parameter signature).
      //如果具有相同引數數量的方法具有相同型別的差異權重,那麼就把它加入到ambiguousFactoryMethods中
      //但是,只能在非寬容的建構函式解析模式下執行該檢查
      //並顯式忽略被覆蓋的方法(具有相同的引數簽名)
      else if (factoryMethodToUse != null && typeDiffWeight == minTypeDiffWeight &&
            !mbd.isLenientConstructorResolution() &&
            paramTypes.length == factoryMethodToUse.getParameterCount() &&
            !Arrays.equals(paramTypes, factoryMethodToUse.getParameterTypes())) {
         if (ambiguousFactoryMethods == null) {
            ambiguousFactoryMethods = new LinkedHashSet<>();
            ambiguousFactoryMethods.add(factoryMethodToUse);
         }
         ambiguousFactoryMethods.add(candidate);
      }
   }
}

上面一段程式碼很長,首先遍歷了所有的候選方法,然後解析出方法的入參,最後再獲取最佳的匹配方法。

if (factoryMethodToUse == null || argsToUse == null) {
   if (causes != null) {
      UnsatisfiedDependencyException ex = causes.removeLast();
      for (Exception cause : causes) {
         this.beanFactory.onSuppressedException(cause);
      }
      throw ex;
   }
   //。。。。。。省略的程式碼
   
   //把解析出來的工廠方法和引數進行快取,防止下次使用時再次解析
   if (explicitArgs == null && argsHolderToUse != null) {
       mbd.factoryMethodToIntrospect = factoryMethodToUse;
       argsHolderToUse.storeCache(mbd, factoryMethodToUse);
    }
 }

最後執行下面的程式碼:

//呼叫工廠方法建立例項,並設定到bw中,然後返回
bw.setBeanInstance(instantiate(beanName, mbd, factoryBean, factoryMethodToUse, argsToUse));
return bw;

最後的呼叫在SimpleInstantiationStrategy類中的instantiate方法:

Method priorInvokedFactoryMethod = currentlyInvokedFactoryMethod.get();
try {
   currentlyInvokedFactoryMethod.set(factoryMethod);
   //呼叫工廠方法
   Object result = factoryMethod.invoke(factoryBean, args);
   if (result == null) {
      result = new NullBean();
   }
   return result;
}
finally {
   if (priorInvokedFactoryMethod != null) {
      currentlyInvokedFactoryMethod.set(priorInvokedFactoryMethod);
   }
   else {
      currentlyInvokedFactoryMethod.remove();
   }
}

可以看到,上面使用到了invoke來進行工廠方法的呼叫。上面整個流程特別長,下面來總結一下步驟。

總結步驟:

  1. 建立了BeanWrapperImpl物件,然後進行了初始化,設定ConversionService型別轉換器,並且註冊了自定義的屬性編輯器
  2. 然後根據factoryBeanName來判斷,這個工廠方法是不是靜態工廠
  3. 嘗試獲取工廠方法和對應的引數,這一步是從快取中獲取
  4. 上面一步獲取不到,就去找所有匹配的工廠方法,然後根據方法的引數數量進行匹配
  5. 最後使用反射呼叫工廠方法進行例項化bean

總而言之,就是要獲取到最匹配的工廠方法,然後獲取到相關的引數,最後呼叫該工廠方法進行例項化bean。

autowireConstructor方法

autowireConstructor方法本質上和instantiateUsingFactoryMethod方法類似,一個是找工廠方法,一個是找建構函式,程式碼裡面有很多相似的地方,接下來看一下程式碼:

protected BeanWrapper autowireConstructor(
      String beanName, RootBeanDefinition mbd, @Nullable Constructor<?>[] ctors, @Nullable Object[] explicitArgs) {

   return new ConstructorResolver(this).autowireConstructor(beanName, mbd, ctors, explicitArgs);
}

建立ConstructorResolver物件,然後呼叫了autowireConstructor方法:

public BeanWrapper autowireConstructor(String beanName, RootBeanDefinition mbd,
      @Nullable Constructor<?>[] chosenCtors, @Nullable Object[] explicitArgs) {}

這裡的程式碼也比較長,下面進行分段。

//建立BeanWrapperImpl物件
BeanWrapperImpl bw = new BeanWrapperImpl();
//初始化BeanWrapperImpl,設定ConversionService型別轉換器
//並且註冊了自定義的屬性編輯器
this.beanFactory.initBeanWrapper(bw);

//構造方法
Constructor<?> constructorToUse = null;
ArgumentsHolder argsHolderToUse = null;
//構造方法的引數
Object[] argsToUse = null;

//如果getBean中設定了引數,就直接使用
if (explicitArgs != null) {
   argsToUse = explicitArgs;
}
//否則,從bean定義中嘗試獲取已經解析的構造方法和引數
else {
   //這裡是為了防止再次進行解析,因為前面可能已經解析過了
   Object[] argsToResolve = null;
   //加鎖
   synchronized (mbd.constructorArgumentLock) {
      //獲取已經解析的構造方法
      constructorToUse = (Constructor<?>) mbd.resolvedConstructorOrFactoryMethod;
      //如果解析後的構造方法不為空,並且引數也被解析過了
      if (constructorToUse != null && mbd.constructorArgumentsResolved) {
         // Found a cached constructor...
         argsToUse = mbd.resolvedConstructorArguments;
         //如果獲取到的解析過的構造引數是空的,那麼就嘗試從bean定義中獲取未被解析的構造引數
         if (argsToUse == null) {
            argsToResolve = mbd.preparedConstructorArguments;
         }
      }
   }
   //如果獲取到了未被解析的構造引數,那就呼叫下面的方法進行解析
   if (argsToResolve != null) {
      argsToUse = resolvePreparedArguments(beanName, mbd, bw, constructorToUse, argsToResolve, true);
   }
}

上面的程式碼,首先是嘗試獲取構造方法和引數,如果獲取不到就走下面的程式碼:

//如果上一步沒有找到對應的構造方法和引數,就開始尋找匹配的構造方法
if (constructorToUse == null || argsToUse == null) {
   // Take specified constructors, if any.
   //獲取所有的構造方法,如果指定了構造方法的集合,就使用這個集合chosenCtors
   Constructor<?>[] candidates = chosenCtors;
   if (candidates == null) {
      //獲取到beanClass
      Class<?> beanClass = mbd.getBeanClass();
      try {
         candidates = (mbd.isNonPublicAccessAllowed() ?
               beanClass.getDeclaredConstructors() : beanClass.getConstructors());
      }
      catch (Throwable ex) {
         throw new BeanCreationException(mbd.getResourceDescription(), beanName,
               "Resolution of declared constructors on bean Class [" + beanClass.getName() +
               "] from ClassLoader [" + beanClass.getClassLoader() + "] failed", ex);
      }
   }

   //如果只找到了一個構造方法,並且getBean傳過來的引數是空的
   //並且bean定義也沒有引數,那麼就直接呼叫這個構造方法來返回一個bean例項
   if (candidates.length == 1 && explicitArgs == null && !mbd.hasConstructorArgumentValues()) {
      Constructor<?> uniqueCandidate = candidates[0];
      if (uniqueCandidate.getParameterCount() == 0) {
         synchronized (mbd.constructorArgumentLock) {
            mbd.resolvedConstructorOrFactoryMethod = uniqueCandidate;
            mbd.constructorArgumentsResolved = true;
            mbd.resolvedConstructorArguments = EMPTY_ARGS;
         }
         bw.setBeanInstance(instantiate(beanName, mbd, uniqueCandidate, EMPTY_ARGS));
         return bw;
      }
   }
}

上面一部分程式碼,是開始獲取建構函式和方法了。

// Need to resolve the constructor.
//是否是構造器注入
boolean autowiring = (chosenCtors != null ||
      mbd.getResolvedAutowireMode() == AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR);
//用來存放解析後的方法的引數值
ConstructorArgumentValues resolvedValues = null;

//方法引數值的最小數量
int minNrOfArgs;
//如果getBean傳來的引數不為空,就直接使用它的長度作為引數值的最小數量
if (explicitArgs != null) {
   minNrOfArgs = explicitArgs.length;
}
//否則,從bean定義中獲取
else {
   //獲取構造引數
   ConstructorArgumentValues cargs = mbd.getConstructorArgumentValues();
   resolvedValues = new ConstructorArgumentValues();
   //解析構造引數,並返回引數的數量
   minNrOfArgs = resolveConstructorArguments(beanName, mbd, bw, cargs, resolvedValues);
}
//進行排序,public優先,引數個數多的優先
AutowireUtils.sortConstructors(candidates);
int minTypeDiffWeight = Integer.MAX_VALUE;
Set<Constructor<?>> ambiguousConstructors = null;
LinkedList<UnsatisfiedDependencyException> causes = null;

上面的內容,是獲取構造方法的入引數量,下面會根據這個引數的數量來進行匹配:

//遍歷所有的建構函式
for (Constructor<?> candidate : candidates) {
   //獲取構造引數的數量
   int parameterCount = candidate.getParameterCount();

   //如果已經存在匹配的建構函式和引數,則跳出迴圈
   if (constructorToUse != null && argsToUse != null && argsToUse.length > parameterCount) {
      // Already found greedy constructor that can be satisfied ->
      // do not look any further, there are only less greedy constructors left.
      break;
   }
   //如果這個建構函式的引數小於最小引數值,則不符合
   if (parameterCount < minNrOfArgs) {
      continue;
   }

  //用來儲存引數的物件
   ArgumentsHolder argsHolder;
   Class<?>[] paramTypes = candidate.getParameterTypes();
   if (resolvedValues != null) {
      try {
         //獲取構造方法的引數名稱
         String[] paramNames = ConstructorPropertiesChecker.evaluate(candidate, parameterCount);
         if (paramNames == null) {
            //如果沒有獲取到,再使用ParameterNameDiscoverer來獲取
            ParameterNameDiscoverer pnd = this.beanFactory.getParameterNameDiscoverer();
            if (pnd != null) {
               paramNames = pnd.getParameterNames(candidate);
            }
         }
         //在給定解析引數的情況下,建立一個ArgumentsHolder物件
         argsHolder = createArgumentArray(beanName, mbd, resolvedValues, bw, paramTypes, paramNames,
               getUserDeclaredConstructor(candidate), autowiring, candidates.length == 1);
      }
      catch (UnsatisfiedDependencyException ex) {
         if (logger.isTraceEnabled()) {
            logger.trace("Ignoring constructor [" + candidate + "] of bean '" + beanName + "': " + ex);
         }
         // Swallow and try next constructor.
         if (causes == null) {
            causes = new LinkedList<>();
         }
         causes.add(ex);
         continue;
      }
   }
   else {
      // Explicit arguments given -> arguments length must match exactly.
      //給出的顯式引數->引數長度必須完全匹配
      if (parameterCount != explicitArgs.length) {
         continue;
      }
      //建立ArgumentsHolder物件
      argsHolder = new ArgumentsHolder物件(explicitArgs);
   }

   //根據權重來獲取最匹配的方法
  //判斷是在寬鬆模式還是在嚴格模式下進行解析
  //寬鬆模式:使用具有“最接近的模式”來進行匹配
  //嚴格模式:解析建構函式時,必須所有的都要進行匹配,否則丟擲異常
   int typeDiffWeight = (mbd.isLenientConstructorResolution() ?
         argsHolder.getTypeDifferenceWeight(paramTypes) : argsHolder.getAssignabilityWeight(paramTypes));
   // Choose this constructor if it represents the closest match.
   if (typeDiffWeight < minTypeDiffWeight) {
      constructorToUse = candidate;
      argsHolderToUse = argsHolder;
      argsToUse = argsHolder.arguments;
      minTypeDiffWeight = typeDiffWeight;
      ambiguousConstructors = null;
   }
   else if (constructorToUse != null && typeDiffWeight == minTypeDiffWeight) {
      if (ambiguousConstructors == null) {
         ambiguousConstructors = new LinkedHashSet<>();
         ambiguousConstructors.add(constructorToUse);
      }
      ambiguousConstructors.add(candidate);
   }
}

上面這麼長的程式碼,就是根據引數的數量和型別,來獲取最為匹配的構造方法

//把解析出來的構造方法和引數進行快取,防止下次使用時再次解析
if (explicitArgs == null && argsHolderToUse != null) {
   argsHolderToUse.storeCache(mbd, constructorToUse);
}
//呼叫構造方法建立例項,並設定到bw中,然後返回
Assert.state(argsToUse != null, "Unresolved constructor arguments");
bw.setBeanInstance(instantiate(beanName, mbd, constructorToUse, argsToUse));
return bw;

然後進入到InstantiationStrategy類中,檢視呼叫的介面:

//通過指定的建構函式例項化bean物件
Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner,
      Constructor<?> ctor, Object... args) throws BeansException;

看下具體的實現:

@Override
public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner,
      final Constructor<?> ctor, Object... args) {

   //bean是否存在方法重寫,如果不存在就使用newInstance例項化
   //否則使用cglib例項化
   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 {
      return instantiateWithMethodInjection(bd, beanName, owner, ctor, args);
   }
}

來看下BeanUtils.instantiateClass方法:

try {
   ReflectionUtils.makeAccessible(ctor);
   if (KotlinDetector.isKotlinReflectPresent() && KotlinDetector.isKotlinType(ctor.getDeclaringClass())) {
      return KotlinDelegate.instantiateClass(ctor, args);
   }
   else {
      Class<?>[] parameterTypes = ctor.getParameterTypes();
      Assert.isTrue(args.length <= parameterTypes.length, "Can't specify more arguments than constructor parameters");
      Object[] argsWithDefaultValues = new Object[args.length];
      for (int i = 0 ; i < args.length; i++) {
         if (args[i] == null) {
            Class<?> parameterType = parameterTypes[i];
            argsWithDefaultValues[i] = (parameterType.isPrimitive() ? DEFAULT_TYPE_VALUES.get(parameterType) : null);
         }
         else {
            argsWithDefaultValues[i] = args[i];
         }
      }
      return ctor.newInstance(argsWithDefaultValues);
   }
}

如果使用cglib會進入CglibSubclassingInstantiationStrategy類中:

Class<?> subclass = createEnhancedSubclass(this.beanDefinition);
Object instance;
if (ctor == null) {
   instance = BeanUtils.instantiateClass(subclass);
}
else {
   try {
      Constructor<?> enhancedSubclassConstructor = subclass.getConstructor(ctor.getParameterTypes());
      instance = enhancedSubclassConstructor.newInstance(args);
   }
   catch (Exception ex) {
      throw new BeanInstantiationException(this.beanDefinition.getBeanClass(),
            "Failed to invoke constructor for CGLIB enhanced subclass [" + subclass.getName() + "]", ex);
   }
}
// SPR-10785: set callbacks directly on the instance instead of in the
// enhanced class (via the Enhancer) in order to avoid memory leaks.
Factory factory = (Factory) instance;
factory.setCallbacks(new Callback[] {NoOp.INSTANCE,
      new LookupOverrideMethodInterceptor(this.beanDefinition, this.owner),
      new ReplaceOverrideMethodInterceptor(this.beanDefinition, this.owner)});
return instance;

總結步驟:

  1. 建立了BeanWrapperImpl物件,然後進行了初始化,設定ConversionService型別轉換器,並且註冊了自定義的屬性編輯器
  2. 嘗試獲取構造方法和引數,如果getBean指定了引數就直接使用,否則從bean定義中取獲取。從bean定義首先獲取已經解析的構造方法和引數,如果獲取到了尚未被解析的引數,那麼就進行解析
  3. 獲取所有匹配的構造方法,如果直接指定了構造方法的集合chosenCtors,就直接使用。如果當前獲取到的構造方法只有一個,並且getBean沒有指定引數,而且從bean定義中也獲取不到,那麼就呼叫這個構造方法進行例項的建立
  4. 上面一步如果沒有例項化,就開始確定引數的最小數量,要找的構造方法的引數的數量要大於等於它,然後使用權重找到最匹配的構造方法
  5. 最後使用newInstance或者cglib例項化出一個bean例項

總而言之,就是要找到匹配的構造方法,如果有引數,就要進行注入,然後呼叫這個建構函式來例項化一個bean

instantiateBean方法

使用預設的無參建構函式進行例項化,來看下程式碼:

/**
 * 使用預設的無參建構函式例項化bean.
 * @param beanName the name of the bean
 * @param mbd the bean definition for the bean
 * @return a BeanWrapper for the new instance
 */
protected BeanWrapper instantiateBean(String beanName, RootBeanDefinition mbd) {
   try {
      Object beanInstance;
      //許可權驗證
      if (System.getSecurityManager() != null) {
         //獲取InstantiationStrategy物件,呼叫instantiate方法來建立例項物件
         beanInstance = AccessController.doPrivileged(
               (PrivilegedAction<Object>) () -> getInstantiationStrategy().instantiate(mbd, beanName, this),
               getAccessControlContext());
      }
      else {
         //獲取InstantiationStrategy物件,呼叫instantiate方法來建立例項物件
         beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, this);
      }
      //儲存例項化的bean
      BeanWrapper bw = new BeanWrapperImpl(beanInstance);
      //初始化BeanWrapper
      initBeanWrapper(bw);
      return bw;
   }
   catch (Throwable ex) {
      throw new BeanCreationException(
            mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);
   }
}

看下SimpleInstantiationStrategy類中instantiate方法:

@Override
public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {
   // Don't override the class with CGLIB if no overrides.
   //如果沒有方法覆蓋,就使用反射來進行例項化
   if (!bd.hasMethodOverrides()) {
      Constructor<?> constructorToUse;
      //加鎖
      synchronized (bd.constructorArgumentLock) {
         //嘗試從bean定義中獲取已經解析的建構函式
         constructorToUse = (Constructor<?>) bd.resolvedConstructorOrFactoryMethod;
         //如果為空,就使用預設的建構函式
         if (constructorToUse == null) {
            //獲取class
            final Class<?> clazz = bd.getBeanClass();
            //如果這個類是介面,丟擲異常
            if (clazz.isInterface()) {
               throw new BeanInstantiationException(clazz, "Specified class is an interface");
            }
            try {
               if (System.getSecurityManager() != null) {
                 //從clazz中獲取構造方法
                  constructorToUse = AccessController.doPrivileged(
                        (PrivilegedExceptionAction<Constructor<?>>) clazz::getDeclaredConstructor);
               }
               else {
                  //獲取預設的構造方法
                  constructorToUse = clazz.getDeclaredConstructor();
               }
               //設定resolvedConstructorOrFactoryMethod,即這個構造方法已經被解析了
               bd.resolvedConstructorOrFactoryMethod = constructorToUse;
            }
            catch (Throwable ex) {
               throw new BeanInstantiationException(clazz, "No default constructor found", ex);
            }
         }
      }
      //通過反射例項化
      return BeanUtils.instantiateClass(constructorToUse);
   }
   else {
      // Must generate CGLIB subclass.
      //通過CGLIB生成一個子類物件
      return instantiateWithMethodInjection(bd, beanName, owner);
   }
}

總結步驟:

  1. 首先看是否有方法覆蓋,如果沒有就使用反射進行例項化
  2. 如果沒有找到已經解析出來的建構函式,就使用預設的建構函式
  3. 通過這個預設的建構函式例項化bean物件
  4. 如果存在方法覆蓋,就使用CGLIB生成一個子類物件

好了,到此整個bean的建立過程原始碼,就已經看的差不多了,如有錯誤請指正,多謝!

相關文章