溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SpringIOC refresh()初始化代碼實(shí)例

發(fā)布時(shí)間:2020-09-29 09:57:24 來源:腳本之家 閱讀:139 作者:FFStayF 欄目:編程語(yǔ)言

finishBeanFactoryInitialization(beanFactory):初始化不需要延遲實(shí)例化的單例bean

/* org/springframework/context/support/AbstractApplicationContext.java:870 */
  protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {//初始化容器的conversionService
    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));
    }
//如果沒有任何一個(gè)類似PropertyPlaceholderConfigurer bean處理器
    //默認(rèn)一個(gè)嵌入式值解析器,主要是為了解析屬性值例如數(shù)據(jù)庫(kù)連接中${password}等屬性解析
    if (!beanFactory.hasEmbeddedValueResolver()) {
      beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
    }
// 先實(shí)例化LoadTimeWeaverAware相關(guān)的beans,以便盡早轉(zhuǎn)換,AOP相關(guān)
    String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
    for (String weaverAwareName : weaverAwareNames) {
      getBean(weaverAwareName);
    }

//刪除臨時(shí)類加載器,停止臨時(shí)類加載器進(jìn)行類型匹配
    beanFactory.setTempClassLoader(null);

//拷貝beandefinitionNames到frozenBeanDefinitionNames
    beanFactory.freezeConfiguration();

    // 實(shí)例化所有不需要延遲初始化的單例bean
    beanFactory.preInstantiateSingletons();
  }

beanFactory.preInstantiateSingletons():實(shí)例化所有不需要延遲初始化的單例bean

/* org/springframework/beans/factory/support/DefaultListableBeanFactory.java:885 */
  public void preInstantiateSingletons() throws BeansException {
    if (logger.isTraceEnabled()) {
      logger.trace("Pre-instantiating singletons in " + this);
    }

    // Iterate over a copy to allow for init methods which in turn register new bean definitions.
    // While this may not be part of the regular factory bootstrap, it does otherwise work fine.
    //拷貝beanDefinitionNames副本,考慮到遍歷中其他線程可能修改beandefinitionNames
    // ArrayList類型遍歷時(shí)規(guī)避fast-fail,實(shí)現(xiàn)fast-safe
    List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);

    // Trigger initialization of all non-lazy singleton beans...
    for (String beanName : beanNames) {
      //當(dāng)bean存在parentBean時(shí)的繼承關(guān)系
      //① bean不存在parentBean時(shí),創(chuàng)建一個(gè)new RootBeanDefinition(beandefinition)返回。
      //② bean存在parentBean時(shí),支持繼承關(guān)系
      // 需要先創(chuàng)建parentBean的new RootBeanDefinition(parentBeanDefinition)
      // 然后用beandefinition覆蓋parentBean的RootBeanDefinition的相關(guān)屬性并返回。superBean.overrideFrom(subBean);
      RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
      //用mergedLocalBeanDefinition判斷是否需要初始化
      //篩選不是抽象類且不是延遲實(shí)例化的單例進(jìn)行初始化
      if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
        if (isFactoryBean(beanName)) {
          //工廠Bean beanName=&beanName
          Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
          if (bean instanceof FactoryBean) {
            final FactoryBean<?> factory = (FactoryBean<?>) bean;
            boolean isEagerInit;
            if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
              isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>)
                      ((SmartFactoryBean<?>) factory)::isEagerInit,
                  getAccessControlContext());
            }
            else {
              isEagerInit = (factory instanceof SmartFactoryBean &&
                  ((SmartFactoryBean<?>) factory).isEagerInit());
            }
            if (isEagerInit) {
              getBean(beanName);
            }
          }
        }
        else {
          //不是工廠bean 直接初始化
          getBean(beanName);
        }
      }
    }

    //SmartInitializingSingleton類型的單例初始化后的回調(diào)
    for (String beanName : beanNames) {
      Object singletonInstance = getSingleton(beanName);
      if (singletonInstance instanceof SmartInitializingSingleton) {
        final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
        if (System.getSecurityManager() != null) {
          AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
            smartSingleton.afterSingletonsInstantiated();
            return null;
          }, getAccessControlContext());
        }
        else {
          smartSingleton.afterSingletonsInstantiated();
        }
      }
    }
  }

getBean(beanName):Bean初始化

/* org/springframework/beans/factory/support/AbstractBeanFactory.java:414 */
  protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
      @Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
    //如果是aliaName,解析成beanName
    final String beanName = transformedBeanName(name);
    Object bean;

    // Eagerly check singleton cache for manually registered singletons.
    //檢查單例緩存中是否存在bean的實(shí)例化對(duì)象、初始化對(duì)象 (涉及循環(huán)依賴解決,需要先分清出Bean的兩個(gè)階段實(shí)例化、初始化)
    Object sharedInstance = getSingleton(beanName);
    if (sharedInstance != null && args == null) {
      if (logger.isTraceEnabled()) {
        if (isSingletonCurrentlyInCreation(beanName)) {
          logger.trace("Returning eagerly cached instance of singleton bean '" + beanName +
              "' that is not fully initialized yet - a consequence of a circular reference");
        }
        else {
          logger.trace("Returning cached instance of singleton bean '" + beanName + "'");
        }
      }
      bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
    }

    else {
      if (isPrototypeCurrentlyInCreation(beanName)) {
        //多例bean正在初始化時(shí)拋出異常
        //由于多例bean,每調(diào)用一次getBean就會(huì)創(chuàng)建一個(gè)實(shí)例,
        //所以通過ThreadLocal打標(biāo),來避免單個(gè)線程的重復(fù)創(chuàng)建
        throw new BeanCurrentlyInCreationException(beanName);
      }

      BeanFactory parentBeanFactory = getParentBeanFactory();
      //父容器不為空,用父容器初始化
      if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
        // Not found -> check parent.
        String nameToLookup = originalBeanName(name);
        if (parentBeanFactory instanceof AbstractBeanFactory) {
          return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
              nameToLookup, requiredType, args, typeCheckOnly);
        }
        else if (args != null) {
          // Delegation to parent with explicit args.
          return (T) parentBeanFactory.getBean(nameToLookup, args);
        }
        else if (requiredType != null) {
          // No args -> delegate to standard getBean method.
          return parentBeanFactory.getBean(nameToLookup, requiredType);
        }
        else {
          return (T) parentBeanFactory.getBean(nameToLookup);
        }
      }

      if (!typeCheckOnly) {
        //alreadyCreated容器記錄已經(jīng)被實(shí)例化的beanName,
        //這里將beanName添加到alreadyCreated
        markBeanAsCreated(beanName);
      }

      try {
        //重新獲取mergedLocalBeanDefinition(bean與parentBean合并)
        final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
        //檢查mbd不是抽象類
        checkMergedBeanDefinition(mbd, beanName, args);

        //創(chuàng)建bean單例之前,需要保證先bean所依賴的Bean單例已創(chuàng)建,
        //這里的依賴對(duì)應(yīng)@dependsOn聲明的依賴,這個(gè)注解不常用,這里也會(huì)出現(xiàn)一種循環(huán)依賴場(chǎng)景,與我們平常討論的引用循環(huán)依賴不同
        //@dependsOn(value = "b") class A;@dependsOn(value = "a") class B
        //如果出現(xiàn)這種循環(huán)依賴 會(huì)直接拋出異常 啟動(dòng)失敗
        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 + "'");
            }
            //保存依賴關(guān)系到dependentBeanMap容器中
            registerDependentBean(dep, beanName);
            try {
              //實(shí)例化依賴的bean 這里就是常見循環(huán)依賴地點(diǎn) A依賴B B依賴A
              getBean(dep);
            }
            catch (NoSuchBeanDefinitionException ex) {
              throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                  "'" + beanName + "' depends on missing bean '" + dep + "'", ex);
            }
          }
        }

        // Create bean instance.
        if (mbd.isSingleton()) {
          //單例Bean創(chuàng)建 先取緩存,沒有才createBean
          sharedInstance = getSingleton(beanName, () -> {
            try {
              //創(chuàng)建Bean實(shí)例
              return createBean(beanName, mbd, args);
            }
            catch (BeansException ex) {
              destroySingleton(beanName);
              throw ex;
            }
          });
          bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
        }

        else if (mbd.isPrototype()) {
          //多例Bean創(chuàng)建 直接createbean
          Object prototypeInstance = null;
          try {
            //正在創(chuàng)建bean打標(biāo)prototypesCurrentlyInCreation
            //與上面isPrototypeCurrentlyInCreation()聯(lián)合
            beforePrototypeCreation(beanName);
            prototypeInstance = createBean(beanName, mbd, args);
          }
          finally {
            //正在創(chuàng)建bean打標(biāo)prototypesCurrentlyInCreation刪除標(biāo)記
            afterPrototypeCreation(beanName);
          }
          bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
        }

        else {
            //自定義scope初始化、scope.get()
          String scopeName = mbd.getScope();
          final Scope scope = this.scopes.get(scopeName);
          if (scope == null) {
            throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
          }
          try {
            Object scopedInstance = scope.get(beanName, () -> {
              beforePrototypeCreation(beanName);
              try {
                return createBean(beanName, mbd, args);
              }
              finally {
                afterPrototypeCreation(beanName);
              }
            });
            bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
          }
          catch (IllegalStateException ex) {
            throw new BeanCreationException(beanName,
                "Scope '" + scopeName + "' is not active for the current thread; consider " +
                "defining a scoped proxy for this bean if you intend to refer to it from a singleton",
                ex);
          }
        }
      }
      catch (BeansException ex) {
        cleanupAfterBeanCreationFailure(beanName);
        throw ex;
      }
    }
//getBean(beanName,requiredType)時(shí)檢查創(chuàng)建的實(shí)例是否需要的類型,容器初始化時(shí)不會(huì)走進(jìn)里面
    if (requiredType != null && !requiredType.isInstance(bean)) {
      try {
        T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType);
        if (convertedBean == null) {
          throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
        }
        return convertedBean;
      }
      catch (TypeMismatchException ex) {
        if (logger.isTraceEnabled()) {
          logger.trace("Failed to convert bean '" + name + "' to required type '" +
              ClassUtils.getQualifiedName(requiredType) + "'", ex);
        }
        throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
      }
    }
    return (T) bean;
  }

sharedInstance = getSingleton(beanName, () -> {try {return createBean(beanName, mbd, args);}:單例初始化,這個(gè)方法比較簡(jiǎn)單,主要關(guān)注下和循壞依賴相關(guān)的邏輯

/* org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java:201 */
  public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
    Assert.notNull(beanName, "Bean name must not be null");
    synchronized (this.singletonObjects) {
      Object singletonObject = this.singletonObjects.get(beanName);
      if (singletonObject == null) {
        if (this.singletonsCurrentlyInDestruction) {
          throw new BeanCreationNotAllowedException(beanName,
              "Singleton bean creation not allowed while singletons of this factory are in destruction " +
              "(Do not request a bean from a BeanFactory in a destroy method implementation!)");
        }
        if (logger.isDebugEnabled()) {
          logger.debug("Creating shared instance of singleton bean '" + beanName + "'");
        }
        //循環(huán)依賴相關(guān):初始化前先singletonsCurrentlyInCreation.add(beanName)
        beforeSingletonCreation(beanName);
        boolean newSingleton = false;
        boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
        if (recordSuppressedExceptions) {
          this.suppressedExceptions = new LinkedHashSet<>();
        }
        try {
          //lamda表達(dá)式:其實(shí)是調(diào)用createBean(beanName, mbd, args):Bean初始化
          singletonObject = singletonFactory.getObject();
          newSingleton = true;
        }
        catch (IllegalStateException ex) {
          // Has the singleton object implicitly appeared in the meantime ->
          // if yes, proceed with it since the exception indicates that state.
          singletonObject = this.singletonObjects.get(beanName);
          if (singletonObject == null) {
            throw ex;
          }
        }
        catch (BeanCreationException ex) {
          if (recordSuppressedExceptions) {
            for (Exception suppressedException : this.suppressedExceptions) {
              ex.addRelatedCause(suppressedException);
            }
          }
          throw ex;
        }
        finally {
          if (recordSuppressedExceptions) {
            this.suppressedExceptions = null;
          }
          //循環(huán)依賴相關(guān):初始化后singletonsCurrentlyInCreation.remove(beanName)
          afterSingletonCreation(beanName);
        }
        if (newSingleton) {
          //初始化完后
          //this.singletonObjects.put(beanName, singletonObject);放入到單例容器中
          //this.singletonFactories.remove(beanName);清空循環(huán)依賴相關(guān)的兩個(gè)打標(biāo)
          //this.earlySingletonObjects.remove(beanName);
          //this.registeredSingletons.add(beanName);放入單例beanName容器中
          addSingleton(beanName, singletonObject);
        }
      }
      return singletonObject;
    }
  }

createBean(beanName, mbd, args):初始化實(shí)例,這個(gè)表方法注意一下,初始化實(shí)例前,留機(jī)會(huì)給BeanPostProcessor初始化代理類直接返回代理類初始化實(shí)例

/* org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java:467 */
  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;

    Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
    if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
      //確保beanclass確實(shí)被解析了,若合并出錯(cuò),重新定義bean定義
      mbdToUse = new RootBeanDefinition(mbd);
      mbdToUse.setBeanClass(resolvedClass);
    }

    // Prepare method overrides.
    try {
      //校驗(yàn)需要重寫方法是否全部重寫,并且會(huì)給重寫方法是否重載打標(biāo)
      mbdToUse.prepareMethodOverrides();
    }
    catch (BeanDefinitionValidationException ex) {
      throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
          beanName, "Validation of method overrides failed", ex);
    }

    try {
      //resolveBeforeInstantiation方法是調(diào)用BeanPostProcessor處理器
      //后續(xù)研究一下BeanPostProcessor
      //這里是給AOP相關(guān)BeanPostProcessor處理器生成代理類實(shí)例的機(jī)會(huì),直接生成初始化代理類實(shí)例返回
      //會(huì)面研究AOP時(shí)發(fā)現(xiàn),proxy object不是在這里初始化,這里只是提供一個(gè)機(jī)會(huì)
      //proxy object具體是在的下面doCreateBean時(shí)里先創(chuàng)建target object 
      //然后通過后置處理器的后置方法初始化的
      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 {
      //初始化實(shí)例
      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);
    }
  }

doCreateBean(beanName,mbdToUse,args):beanClass實(shí)例化(即earlysingleton,注意不是初始化)然后DI完成初始化

/* org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java:549 */
  protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
      throws BeanCreationException {

    // Instantiate the bean.
    BeanWrapper instanceWrapper = null;
    if (mbd.isSingleton()) {
      instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
    }
    if (instanceWrapper == null) {
      //反射調(diào)用beanClass的構(gòu)造方法創(chuàng)建實(shí)例并包裝成beanwrapper--
      instanceWrapper = createBeanInstance(beanName, mbd, args);
    }
    final Object bean = instanceWrapper.getWrappedInstance();
    Class<?> beanType = instanceWrapper.getWrappedClass();
    if (beanType != NullBean.class) {
      mbd.resolvedTargetType = beanType;
    }

    // Allow post-processors to modify the merged bean definition.
    //允許MergedBeanDefinitionPostProcessors后置管理器修改rootBeandefinition
    synchronized (mbd.postProcessingLock) {
      if (!mbd.postProcessed) {
        try {
          applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
        }
        catch (Throwable ex) {
          throw new BeanCreationException(mbd.getResourceDescription(), beanName,
              "Post-processing of merged bean definition failed", ex);
        }
        mbd.postProcessed = true;
      }
    }

    // Eagerly cache singletons to be able to resolve circular references
    // even when triggered by lifecycle interfaces like BeanFactoryAware.
    boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
        isSingletonCurrentlyInCreation(beanName));
    if (earlySingletonExposure) {
      if (logger.isTraceEnabled()) {
        logger.trace("Eagerly caching bean '" + beanName +
            "' to allow for resolving potential circular references");
      }
      //循環(huán)依賴相關(guān)邏輯:
      //this.singletonFactories.put(beanName, singletonFactory);
      //將實(shí)例化bean、beanName組裝成singletonFactory裝入singletonFactories容器
      //this.earlySingletonObjects.remove(beanName);
      //刪除earlySingletonObjects中beanName
      addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
    }

    // Initialize the bean instance.
    //創(chuàng)建實(shí)例
    Object exposedObject = bean;
    try {
      //實(shí)例初始化 就是在這里面實(shí)現(xiàn)依賴注入DI的:
      //具體是調(diào)用AutowiredAnnotationBeanPostProcessor.postProcessProperties
      populateBean(beanName, mbd, instanceWrapper);//調(diào)用Bean后置管理器前置方法BeanPostProcessor.postProcessBeforeInitialization
      //instanceof aware接口,setBeanName setBeanClassLoader setBeanFactory
      //調(diào)用Bean后置管理器后置方法BeanPostProcessor.postProcessAfterInitialization
      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);
      }
    }
    //...
    return exposedObject;
  }

populateBean(beanName, mbd, instanceWrapper):依賴注入DI執(zhí)行時(shí)機(jī)

/* org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java:1381 */
  protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {
    //...
    PropertyDescriptor[] filteredPds = null;
    if (hasInstAwareBpps) {
      if (pvs == null) {
        pvs = mbd.getPropertyValues();
      }
      for (BeanPostProcessor bp : getBeanPostProcessors()) {
        if (bp instanceof InstantiationAwareBeanPostProcessor) {
          InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
          //這里調(diào)用AutowiredAnnotationBeanPostProcessor.postProcessProperties()
          //完成注解自動(dòng)注入(DI)
          PropertyValues pvsToUse = ibp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName);
          if (pvsToUse == null) {
            if (filteredPds == null) {
              filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
            }
            pvsToUse = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
            if (pvsToUse == null) {
              return;
            }
          }
          pvs = pvsToUse;
        }
      }
    }
    if (needsDepCheck) {
      if (filteredPds == null) {
        filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
      }
      checkDependencies(beanName, mbd, filteredPds, pvs);
    }

    if (pvs != null) {
      applyPropertyValues(beanName, mbd, bw, pvs);
    }
  }

DI結(jié)束,bean初始化基本結(jié)束了。然后就是bean初始化的BeanPostProcessor執(zhí)行前置后置方法

BeanPostProcessor前置后置方法:一些很重要架構(gòu)都需要依靠實(shí)現(xiàn),例如AOP動(dòng)態(tài)代理產(chǎn)生代理對(duì)象

AnnotationAwareAspectJAutoProxyCreator.postProcessAfterInitialization

/* org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean(java.lang.String, java.lang.Object, org.springframework.beans.factory.support.RootBeanDefinition) */
  protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {
    if (System.getSecurityManager() != null) {
      AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
        invokeAwareMethods(beanName, bean);
        return null;
      }, getAccessControlContext());
    }
    else {
      invokeAwareMethods(beanName, bean);
    }

    Object wrappedBean = bean;
    if (mbd == null || !mbd.isSynthetic()) {
      //調(diào)用所有BeanPostProcessor前置方法BeanPostProcessor.postProcessBeforeInitialization
      wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
    }

    try {
      invokeInitMethods(beanName, wrappedBean, mbd);
    }
    catch (Throwable ex) {
      throw new BeanCreationException(
          (mbd != null ? mbd.getResourceDescription() : null),
          beanName, "Invocation of init method failed", ex);
    }
    if (mbd == null || !mbd.isSynthetic()) {
      //調(diào)用所有BeanPostProcessor前置方法BeanPostProcessor.postProcessAfterInitialization
      //例如AOP代理對(duì)象就是這里初始化的,AnnotationAwareAspectJAutoProxyCreator.postProcessAfterInitialization
      wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
    }

    return wrappedBean;
  }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI