Spring源碼淺析之bean實例的創建過程(二)


在上一篇內容中,介紹了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的創建過程源碼,就已經看的差不多了,如有錯誤請指正,多謝!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM