SpringIOC源碼解析(下)

注意,看完這篇文章須要很長很長很長時間。。。

本篇文章是SpringIOC源碼解析(上)的續集,上一篇文章介紹了使用XML的方式啓動Spring,而後追蹤了BeanFactory容器的建立、配置文件的解析、Bean的註冊等。前端

12. finishBeanFactoryInitialization()

前方超長篇幅預警。。。java

剛纔咱們提到了bean尚未初始化。這個方法就是負責初始化全部的沒有設置懶加載的singleton bean面試

開始擼了redis

protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {

        if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
                beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
            beanFactory.setConversionService(
                    beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
        }

        if (!beanFactory.hasEmbeddedValueResolver()) {
            beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
        }
        //先初始化 LoadTimeWeaverAware 類型的 Bean
        String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
        for (String weaverAwareName : weaverAwareNames) {
            getBean(weaverAwareName);
        }
       //中止使用用於類型匹配的臨時類加載器
        beanFactory.setTempClassLoader(null);
      //凍結全部的bean定義,即已註冊的bean定義將不會被修改或後處理
        beanFactory.freezeConfiguration();
      //初始化
        beanFactory.preInstantiateSingletons();
    }

上方沒有解釋的代碼意義往下看吧spring

conversionService

這種類型的bean最實用的場景就是用來將前端傳過來的參數和後端的controller方法上的參數格式轉換的時候使用後端

例如:前端要傳一個String,後端使用Date接受的時候就能夠這樣操做緩存

public class StringToDateConverter implements Converter<String, Date> {

    @Override
    public Date convert(String date) {
        try {
            return dateFormat.parse(date);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("日期轉換失敗!");
            return null;
        }
        
    }
}

再搞個beansession

<bean id="conversionService"
  class="org.springframework.context.support.ConversionServiceFactoryBean">
  <property name="converters">
    <list>
      <bean class="cn.shiyujun.utils.StringToDateConverter"/>
    </list>
  </property></bean>
EmbeddedValueResolver

利用EmbeddedValueResolver能夠很方便的實現讀取配置文件的屬性多線程

@Component
public class PropertiesUtil implements EmbeddedValueResolverAware {

    private StringValueResolver resolver;

    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        this.resolver = resolver;
    }


    /**
     * 獲取屬性時直接傳入屬性名稱便可
     */
    public String getPropertiesValue(String key) {
        StringBuilder name = new StringBuilder("${").append(key).append("}");
        return resolver.resolveStringValue(name.toString());
    }

}
初始化

敲黑板了,重點來了。。。app

這裏分析beanFactory.preInstantiateSingletons()方法

public void preInstantiateSingletons() throws BeansException {
   if (this.logger.isDebugEnabled()) {
      this.logger.debug("Pre-instantiating singletons in " + this);
   }
   // this.beanDefinitionNames 保存了全部的 beanNames
   List<String> beanNames = new ArrayList<String>(this.beanDefinitionNames);

   for (String beanName : beanNames) {

      // 合併父 Bean 中的配置,主意<bean id="" class="" parent="" /> 中的 parent屬性
      RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);

      // 不是抽象類、是單例的且不是懶加載的
      if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
         // 處理 FactoryBean
         if (isFactoryBean(beanName)) {
            //在 beanName 前面加上「&」 符號
            final FactoryBean<?> factory = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName);
            // 判斷當前 FactoryBean 是不是 SmartFactoryBean 的實現
            boolean isEagerInit;
            if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
               isEagerInit = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
                  @Override
                  public Boolean run() {
                     return ((SmartFactoryBean<?>) factory).isEagerInit();
                  }
               }, getAccessControlContext());
            }
            else {
               isEagerInit = (factory instanceof SmartFactoryBean &&
                     ((SmartFactoryBean<?>) factory).isEagerInit());
            }
            if (isEagerInit) {

               getBean(beanName);
            }
         }
         else {
            // 不是FactoryBean的直接使用此方法進行初始化
            getBean(beanName);
         }
      }
   }


  
   // 若是bean實現了 SmartInitializingSingleton 接口的,那麼在這裏獲得回調
   for (String beanName : beanNames) {
      Object singletonInstance = getSingleton(beanName);
      if (singletonInstance instanceof SmartInitializingSingleton) {
         final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
         if (System.getSecurityManager() != null) {
            AccessController.doPrivileged(new PrivilegedAction<Object>() {
               @Override
               public Object run() {
                  smartSingleton.afterSingletonsInstantiated();
                  return null;
               }
            }, getAccessControlContext());
         }
         else {
            smartSingleton.afterSingletonsInstantiated();
         }
      }
   }
}

能夠看到,不論是不是FactoryBean,最後都調用了getBean(beanName),繼續看這個方法吧

@Override
public Object getBean(String name) throws BeansException {
   return doGetBean(name, null, null, false);
}

protected <T> T doGetBean(
      final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly)
      throws BeansException {
   // 獲取beanName,處理兩種狀況,一個是前面說的 FactoryBean(前面帶 ‘&’),再一個這個方法是能夠根據別名來獲取Bean的,因此在這裏是要轉換成最正統的BeanName
  //主要邏輯就是若是是FactoryBean就把&去掉若是是別名就把根據別名獲取真實名稱後面就不貼代碼了
   final String beanName = transformedBeanName(name);

   //最後的返回值
   Object bean; 

   // 檢查是否已初始化
   Object sharedInstance = getSingleton(beanName);
  //若是已經初始化過了,且沒有傳args參數就表明是get,直接取出返回
   if (sharedInstance != null && args == null) {
      if (logger.isDebugEnabled()) {
         if (isSingletonCurrentlyInCreation(beanName)) {
            logger.debug("...");
         }
         else {
            logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
         }
      }
      // 這裏若是是普通Bean 的話,直接返回,若是是 FactoryBean 的話,返回它建立的那個實例對象
      bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
   }

   else {
       // 若是存在prototype類型的這個bean
      if (isPrototypeCurrentlyInCreation(beanName)) {
                throw new BeanCurrentlyInCreationException(beanName);
      }

      // 若是當前BeanDefinition不存在這個bean且具備父BeanFactory
      BeanFactory parentBeanFactory = getParentBeanFactory();
      if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
         String nameToLookup = originalBeanName(name);
        // 返回父容器的查詢結果
         if (args != null) {
            return (T) parentBeanFactory.getBean(nameToLookup, args);
         }
         else {
            return parentBeanFactory.getBean(nameToLookup, requiredType);
         }
      }

      if (!typeCheckOnly) {
         // typeCheckOnly 爲 false,將當前 beanName 放入一個 alreadyCreated 的 Set 集合中。
         markBeanAsCreated(beanName);
      }

      /*
       * 到這就要建立bean了
       */
      try {
         final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
         checkMergedBeanDefinition(mbd, beanName, args);

         // 先初始化依賴的全部 Bean, depends-on 中定義的依賴
         String[] dependsOn = mbd.getDependsOn();
         if (dependsOn != null) {
            for (String dep : dependsOn) {
               // 檢查是否是有循環依賴
               if (isDependent(beanName, dep)) {
                  throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                        "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
               }
               // 註冊一下依賴關係
               registerDependentBean(dep, beanName);
               // 先初始化被依賴項
               getBean(dep);
            }
         }

         // 若是是單例的
         if (mbd.isSingleton()) {
            sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() {
               @Override
               public Object getObject() throws BeansException {
                  try {
                     // 執行建立 Bean,下面說
                     return createBean(beanName, mbd, args);
                  }
                  catch (BeansException ex) {
                     destroySingleton(beanName);
                     throw ex;
                  }
               }
            });
            bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
         }

         // 若是是prototype
         else if (mbd.isPrototype()) {
            Object prototypeInstance = null;
            try {
               beforePrototypeCreation(beanName);
               // 執行建立 Bean
               prototypeInstance = createBean(beanName, mbd, args);
            }
            finally {
               afterPrototypeCreation(beanName);
            }
            bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
         }

         // 若是不是 singleton 和 prototype 那麼就是自定義的scope、例如Web項目中的session等類型,這裏就交給自定義scope的應用方去實現
         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, new ObjectFactory<Object>() {
                  @Override
                  public Object getObject() throws BeansException {
                     beforePrototypeCreation(beanName);
                     try {
                        // 執行建立 Bean
                        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;
      }
   }

   //檢查bean的類型
   if (requiredType != null && bean != null && !requiredType.isInstance(bean)) {
      try {
         return getTypeConverter().convertIfNecessary(bean, requiredType);
      }
      catch (TypeMismatchException ex) {
         if (logger.isDebugEnabled()) {
            logger.debug("Failed to convert bean '" + name + "' to required type '" +
                  ClassUtils.getQualifiedName(requiredType) + "'", ex);
         }
         throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
      }
   }
   return (T) bean;
}

看了上方方法咱們知道了原來Spring自己只定義了兩種Scope,也知道了SpringMVC的幾種Scope是如何實現的了。
而後發現一開始會先判斷bean存不存在,若是存在就直接返回了。若是不存在那就要接着往下看createBean方法了

protected Object createBean(String beanName, RootBeanDefinition mbd, Object[] args) throws BeanCreationException {
   if (logger.isDebugEnabled()) {
      logger.debug("Creating instance of bean '" + beanName + "'");
   }
   RootBeanDefinition mbdToUse = mbd;

   // 確保 BeanDefinition 中的 Class 被加載
   Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
   if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
      mbdToUse = new RootBeanDefinition(mbd);
      mbdToUse.setBeanClass(resolvedClass);
   }

   // 準備方法覆寫,若是bean中定義了 <lookup-method /> 和 <replaced-method />
   try {
      mbdToUse.prepareMethodOverrides();
   }
   catch (BeanDefinitionValidationException ex) {
      throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
            beanName, "Validation of method overrides failed", ex);
   }

   try {
      // 若是有代理的話直接返回
      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);
   }
   // 建立 bean
   Object beanInstance = doCreateBean(beanName, mbdToUse, args);
   if (logger.isDebugEnabled()) {
      logger.debug("Finished creating instance of bean '" + beanName + "'");
   }
   return beanInstance;
}

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

   BeanWrapper instanceWrapper = null;
   if (mbd.isSingleton()) {
     //若是是.factoryBean則從緩存刪除
      instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
   }
   if (instanceWrapper == null) {
      // 實例化 Bean,這個方法裏面纔是終點,下面說
      instanceWrapper = createBeanInstance(beanName, mbd, args);
   }
    //bean實例
   final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null);
    //bean類型
   Class<?> beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null);
   mbd.resolvedTargetType = beanType;

   synchronized (mbd.postProcessingLock) {
      if (!mbd.postProcessed) {
         try {
            // 循環調用實現了MergedBeanDefinitionPostProcessor接口的postProcessMergedBeanDefinition方法
           // Spring對這個接口有幾個默認的實現,其中你們最熟悉的一個是操做@Autowired註解的
            applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
         }
         catch (Throwable ex) {
            throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                  "Post-processing of merged bean definition failed", ex);
         }
         mbd.postProcessed = true;
      }
   }

 
   // 解決循環依賴問題
   boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
         isSingletonCurrentlyInCreation(beanName));
   if (earlySingletonExposure) {
      if (logger.isDebugEnabled()) {
         logger.debug("Eagerly caching bean '" + beanName +
               "' to allow for resolving potential circular references");
      }
     //當正在建立A時,A依賴B,此時經過(8將A做爲ObjectFactory放入單例工廠中進行early expose,此處B須要引用A,但A正在建立,從單例工廠拿到ObjectFactory,從而容許循環依賴
      addSingletonFactory(beanName, new ObjectFactory<Object>() {
         @Override
         public Object getObject() throws BeansException {
            return getEarlyBeanReference(beanName, mbd, bean);
         }
      });
   }

   Object exposedObject = bean;
   try {
      // 負責屬性裝配,很重要,下面說
      populateBean(beanName, mbd, instanceWrapper);
      if (exposedObject != null) {
         // 這裏是處理bean初始化完成後的各類回調,例如init-method、InitializingBean 接口、BeanPostProcessor 接口
         exposedObject = initializeBean(beanName, exposedObject, mbd);
      }
   }
   catch (Throwable ex) {
      if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {
         throw (BeanCreationException) ex;
      }
      else {
         throw new BeanCreationException(
               mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
      }
   }
    //一樣的,若是存在循環依賴
   if (earlySingletonExposure) {
      Object earlySingletonReference = getSingleton(beanName, false);
      if (earlySingletonReference != null) {
         if (exposedObject == bean) {
            exposedObject = earlySingletonReference;
         }
         else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
            String[] dependentBeans = getDependentBeans(beanName);
            Set<String> actualDependentBeans = new LinkedHashSet<String>(dependentBeans.length);
            for (String dependentBean : dependentBeans) {
               if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {
                  actualDependentBeans.add(dependentBean);
               }
            }
            if (!actualDependentBeans.isEmpty()) {
               throw new BeanCurrentlyInCreationException(beanName,
                     "Bean with name '" + beanName + "' has been injected into other beans [" +
                     StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +
                     "] in its raw version as part of a circular reference, but has eventually been " +
                     "wrapped. This means that said other beans do not use the final version of the " +
                     "bean. This is often the result of over-eager type matching - consider using " +
                     "'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.");
            }
         }
      }
   }

   // 把bean註冊到相應的Scope中
   try {
      registerDisposableBeanIfNecessary(beanName, bean, mbd);
   }
   catch (BeanDefinitionValidationException ex) {
      throw new BeanCreationException(
            mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
   }

   return exposedObject;
}

到這裏第一次初始化的bean也返回了,你覺得就這樣結束了麼。不,還有幾個很重要的點

建立bean實例createBeanInstance ()
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, Object[] args) {
   // 確保已經加載了此 class
   Class<?> beanClass = resolveBeanClass(mbd, beanName);

   // 校驗類的訪問權限
   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());
   }
   
   if (mbd.getFactoryMethodName() != null)  {
      // 採用工廠方法實例化
      return instantiateUsingFactoryMethod(beanName, mbd, args);
   }

  //是否第一次
   boolean resolved = false;
  //是否採用構造函數注入
   boolean autowireNecessary = false;
   if (args == null) {
      synchronized (mbd.constructorArgumentLock) {
         if (mbd.resolvedConstructorOrFactoryMethod != null) {
            resolved = true;
            autowireNecessary = mbd.constructorArgumentsResolved;
         }
      }
   }
   if (resolved) {
      if (autowireNecessary) {
         return autowireConstructor(beanName, mbd, null, null);
      }
      else {
         // 無參構造函數
         return instantiateBean(beanName, mbd);
      }
   }

   // 判斷是否採用有參構造函數
   Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
   if (ctors != null ||
         mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR ||
         mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args))  {
      // 構造函數依賴注入
      return autowireConstructor(beanName, mbd, ctors, args);
   }

   // 調用無參構造函數
   return instantiateBean(beanName, mbd);
}

選一個無參的構造看一下吧

protected BeanWrapper instantiateBean(final String beanName, final RootBeanDefinition mbd) {
   try {
      Object beanInstance;
      final BeanFactory parent = this;
      if (System.getSecurityManager() != null) {
         beanInstance = AccessController.doPrivileged(new PrivilegedAction<Object>() {
            @Override
            public Object run() {

               return getInstantiationStrategy().instantiate(mbd, beanName, parent);
            }
         }, getAccessControlContext());
      }
      else {
         // 具體實例化的實現,往下看
         beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, parent);
      }
      BeanWrapper bw = new BeanWrapperImpl(beanInstance);
      initBeanWrapper(bw);
      return bw;
   }
   catch (Throwable ex) {
      throw new BeanCreationException(
            mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);
   }
}


public Object instantiate(RootBeanDefinition bd, String beanName, BeanFactory owner) {

   // 若是不存在方法覆寫,那就使用 java 反射進行實例化,不然使用 CGLIB,
   if (bd.getMethodOverrides().isEmpty()) {
      Constructor<?> constructorToUse;
      synchronized (bd.constructorArgumentLock) {
         constructorToUse = (Constructor<?>) bd.resolvedConstructorOrFactoryMethod;
         if (constructorToUse == null) {
            final Class<?> clazz = bd.getBeanClass();
            if (clazz.isInterface()) {
               throw new BeanInstantiationException(clazz, "Specified class is an interface");
            }
            try {
               if (System.getSecurityManager() != null) {
                  constructorToUse = AccessController.doPrivileged(new PrivilegedExceptionAction<Constructor<?>>() {
                     @Override
                     public Constructor<?> run() throws Exception {
                        return clazz.getDeclaredConstructor((Class[]) null);
                     }
                  });
               }
               else {
                  constructorToUse = clazz.getDeclaredConstructor((Class[]) null);
               }
               bd.resolvedConstructorOrFactoryMethod = constructorToUse;
            }
            catch (Throwable ex) {
               throw new BeanInstantiationException(clazz, "No default constructor found", ex);
            }
         }
      }
      // 利用構造方法進行實例化
      return BeanUtils.instantiateClass(constructorToUse);
   }
   else {
      // 存在方法覆寫,利用 CGLIB 來完成實例化,須要依賴於 CGLIB 生成子類,這裏就不展開了
      return instantiateWithMethodInjection(bd, beanName, owner);
   }
}
bean屬性注入populateBean ()
protected void populateBean(String beanName, RootBeanDefinition mbd, BeanWrapper bw) {
   // bean的全部屬性
   PropertyValues pvs = mbd.getPropertyValues();

   if (bw == null) {
      if (!pvs.isEmpty()) {
         throw new BeanCreationException(
               mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance");
      }
      else {
         return;
      }
   }


   boolean continueWithPropertyPopulation = true;
   if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
      for (BeanPostProcessor bp : getBeanPostProcessors()) {
         if (bp instanceof InstantiationAwareBeanPostProcessor) {
            InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
            // 若是返回 false,表明不須要進行後續的屬性設值,也不須要再通過其餘的 BeanPostProcessor 的處理
            if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
               continueWithPropertyPopulation = false;
               break;
            }
         }
      }
   }

   if (!continueWithPropertyPopulation) {
      return;
   }

   if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME ||
         mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) {
      MutablePropertyValues newPvs = new MutablePropertyValues(pvs);

      // 經過名字找到全部屬性值,若是是 bean 依賴,先初始化依賴的 bean。記錄依賴關係
      if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) {
         autowireByName(beanName, mbd, bw, newPvs);
      }

      // 經過類型裝配。複雜一些
      if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) {
         autowireByType(beanName, mbd, bw, newPvs);
      }

      pvs = newPvs;
   }

   boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
   boolean needsDepCheck = (mbd.getDependencyCheck() != RootBeanDefinition.DEPENDENCY_CHECK_NONE);

   if (hasInstAwareBpps || needsDepCheck) {
      PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
      if (hasInstAwareBpps) {
         for (BeanPostProcessor bp : getBeanPostProcessors()) {
            if (bp instanceof InstantiationAwareBeanPostProcessor) {
               InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
               // 這裏就是上方曾經提到過得對@Autowired處理的一個BeanPostProcessor了
               // 它會對全部標記@Autowired、@Value 註解的屬性進行設值
               pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
               if (pvs == null) {
                  return;
               }
            }
         }
      }
      if (needsDepCheck) {
         checkDependencies(beanName, mbd, filteredPds, pvs);
      }
   }
   // 設置 bean 實例的屬性值
   applyPropertyValues(beanName, mbd, bw, pvs);
}

getBean這一塊就搞完了

13. finishRefresh()
protected void finishRefresh() {
        //看名字就知道了,清理剛纔一系列操做使用到的資源緩存
        clearResourceCaches();

        // 初始化LifecycleProcessor
        initLifecycleProcessor();

        // 這個方法的內部實現是啓動全部實現了Lifecycle接口的bean
        getLifecycleProcessor().onRefresh();

        //發佈ContextRefreshedEvent事件
        publishEvent(new ContextRefreshedEvent(this));

        // 檢查spring.liveBeansView.mbeanDomain是否存在,有就會建立一個MBeanServer
        LiveBeansView.registerApplicationContext(this);
    }
14. resetCommonCaches()

最後仍是一步仍是清除緩存

15. refresh()總結

上方用了這麼長的篇幅把整個refresh()方法的細節給梳理清楚,這裏再把剛開始看的懵懵的refresh() 方法貼一下

public void refresh() throws BeansException, IllegalStateException {
 
   synchronized (this.startupShutdownMonitor) {

      // 記錄容器的啓動時間、標記「已啓動」狀態、檢查環境變量
      prepareRefresh();

      // 初始化BeanFactory容器、註冊BeanDefinition
      ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

      // 設置 BeanFactory 的類加載器,添加幾個 BeanPostProcessor,手動註冊幾個特殊的 bean
      prepareBeanFactory(beanFactory);

      try {
         // 擴展點
         postProcessBeanFactory(beanFactory);
         // 調用 BeanFactoryPostProcessor 各個實現類的 postProcessBeanFactory(factory) 方法
         invokeBeanFactoryPostProcessors(beanFactory);

         // 註冊 BeanPostProcessor 的實現類
         registerBeanPostProcessors(beanFactory);

         // 初始化MessageSource
         initMessageSource();

         // 初始化事件廣播器
         initApplicationEventMulticaster();

         // 擴展點
         onRefresh();

         // 註冊事件監聽器
         registerListeners();


         // 初始化全部的 singleton beans
         finishBeanFactoryInitialization(beanFactory);

         // 廣播事件
         finishRefresh();
      }

      catch (BeansException ex) {
         if (logger.isWarnEnabled()) {
            logger.warn("Exception encountered during context initialization - " +
                  "cancelling refresh attempt: " + ex);
         }

         // 銷燬已經初始化的的Bean
         destroyBeans();

         // 設置 'active' 狀態
         cancelRefresh(ex);

         throw ex;
      }

      finally {
         // 清除緩存
         resetCommonCaches();
      }
   }
}

總結

至此,本文到此結束。鑑於XML方式比較簡單、通俗易懂,因此本文基於XML的方式大體介紹了SpringIOC的啓動流程、稍微深刻的講解了Bean容器的建立以及Bean的初始化過程。這也是做者第一次閱讀開源框架的源碼,如文章有錯誤之處還請您費心指出。

鑑於如今比較流行SpringBoot和SpringCloud,下篇文章將會從基於註解的方向分析SpringIOC

<h4 style="color:red">推薦閱讀</h4>

  1. SpringCloud學習系列彙總
  2. 爲何一線大廠面試必問redis,有啥好問的?
  3. 多線程面試必備基礎知識彙總
  4. Java集合源碼分析彙總-JDK1.8
  5. Linux經常使用命令速查-彙總篇
  6. JVM系列文章彙總
  7. MySQL系列文章彙總
  8. RabbitMQ系列文章彙總

<h4 style="color:red">博客全部文章首發於公衆號《Java學習錄》轉載請保留
掃碼關注公衆號便可領取2000GJava學習資源 </h4>

1

相關文章
相關標籤/搜索