Spring IOC容器核心流程原始碼分析

狐言不胡言發表於2021-08-16

簡單介紹

Spring IOC的核心方法就在於refresh方法,這個方法裡面完成了Spring的初始化、準備bean、例項化bean和擴充套件功能的實現。

  • 這個方法的作用是什麼?
  • 它是如何完成這些功能的?
  • 為什麼要這樣去實現?
  • 有哪些值得借鑑的地方?

refresh方法

在ConfigurableApplicationContext裡面定義了這個方法:

/**
 * Load or refresh the persistent representation of the configuration, which
 * might be from Java-based configuration, an XML file, a properties file, a
 * relational database schema, or some other format.
 * <p>As this is a startup method, it should destroy already created singletons
 * if it fails, to avoid dangling resources. In other words, after invocation
 * of this method, either all or no singletons at all should be instantiated.
 * @throws BeansException if the bean factory could not be initialized
 * @throws IllegalStateException if already initialized and multiple refresh
 * attempts are not supported
 */
void refresh() throws BeansException, IllegalStateException;

根據註釋可以知道,這個方法是用來載入重新整理配置,這些配置可能來自java配置、xml檔案、properties檔案、關係型資料庫或者其他格式。

作為一個啟動方法,它應當在初始化失敗後銷燬已經建立的單例,防止佔著資源而不使用。也就是說呼叫這個方法的話,要麼所有的單例已經被例項化,要麼所有的單例都不存在。

BeansException:bean工廠不能被初始化,丟擲BeansException

IllegalStateException:bean工廠已經被初始化了,但是不支援多次重新整理,丟擲IllegalStateException

具體實現

在AbstractApplicationContext中進行了實現:

@Override
public void refresh() throws BeansException, IllegalStateException {
   synchronized (this.startupShutdownMonitor) {
      //為重新整理做準備,初始化各種狀態,啟動時間、關閉啟用狀態、配置檔案資訊等
      prepareRefresh();

      //通知子類重新整理內部BeanFactory,並返回重新整理後的BeanFactory
      //關注方法中的refreshBeanFactory方法,兩個子類對它重寫的差異
      //AbstractRefreshableApplicationContext#refreshBeanFactory中重新構建,完成BeanDefinition的解析構建
      //GenericApplicationContext#refreshBeanFactory則是設定ID,並且只能重新整理一次
      ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

      //給容器準備BeanFactory,配置相關的屬性
      prepareBeanFactory(beanFactory);

      try {
         //允許子類處理後置beanFactory
         postProcessBeanFactory(beanFactory);

         //呼叫註冊了beanFactory後置處理器介面例項bean對應的方法
         invokeBeanFactoryPostProcessors(beanFactory);

         //註冊bean後置處理器介面的例項bean
         registerBeanPostProcessors(beanFactory);

         //初始化國際化資源
         initMessageSource();

         //初始化事件傳播者
         initApplicationEventMulticaster();

         //refresh事件,初始化一些指定的bean
         onRefresh();

         //註冊監聽器bean
         registerListeners();

         //完成bean工廠的初始化,初始化所有非懶載入的單例bean
         finishBeanFactoryInitialization(beanFactory);

         //最後,釋出finishRefresh事件
         finishRefresh();
      }

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

         //銷燬了所有已經建立了的單例bean
         destroyBeans();

         //重置active標記
         cancelRefresh(ex);

         // Propagate exception to caller.
         throw ex;
      }

      finally {
         // Reset common introspection caches in Spring's core, since we
         // might not ever need metadata for singleton beans anymore...
         resetCommonCaches();
      }
   }
}

使用示例:

GenericApplicationContext context3 = new GenericApplicationContext();
new XmlBeanDefinitionReader(context3).loadBeanDefinitions("classpath:application.xml");
new ClassPathBeanDefinitionScanner(context3).scan("edu.demo");
// 一定要重新整理
context3.refresh();

IOC容器重新整理整體流程

prepareRefresh方法

//關閉和重新整理的狀態都是AtomicBoolean型別的,原子性
/** Flag that indicates whether this context is currently active. */
private final AtomicBoolean active = new AtomicBoolean();

/** Flag that indicates whether this context has been closed already. */
private final AtomicBoolean closed = new AtomicBoolean();

/**
 * 為容器重新整理做準備,設定啟動日期和活動標識
 * 並執行一些資源的初始化
 */
protected void prepareRefresh() {
   //容器切換到啟動狀態
   //設定啟動容器的時間
   this.startupDate = System.currentTimeMillis();
   //設定關閉的狀態
   this.closed.set(false);
   //設定啟動的狀態
   this.active.set(true);

   if (logger.isDebugEnabled()) {
      if (logger.isTraceEnabled()) {
         logger.trace("Refreshing " + this);
      }
      else {
         logger.debug("Refreshing " + getDisplayName());
      }
   }

   //初始化容器的佔位符資源
   initPropertySources();

   // 驗證所有標記為required的屬性都是可解析的:
   // see ConfigurablePropertyResolver#setRequiredProperties
   getEnvironment().validateRequiredProperties();

    //一些監聽器的初始化
   // Store pre-refresh ApplicationListeners...
   if (this.earlyApplicationListeners == null) {
      this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
   }
   else {
      // Reset local application listeners to pre-refresh state.
      this.applicationListeners.clear();
      this.applicationListeners.addAll(this.earlyApplicationListeners);
   }

   // Allow for the collection of early ApplicationEvents,
   // to be published once the multicaster is available...
   this.earlyApplicationEvents = new LinkedHashSet<>();
}

obtainFreshBeanFactory方法

/**
 * 告訴子類重新整理內部的bean factory.
 * @return the fresh BeanFactory instance
 * @see #refreshBeanFactory()
 * @see #getBeanFactory()
 */
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
   refreshBeanFactory();
   return getBeanFactory();
}

其實就是配置使用者的屬性、載入bean定義,並且返回重新整理後的bean工廠。prepareBeanFacotry方法內部又有兩個抽象方法構成,交給子類去實現。也就是說子類必定含有一個BeanFactory例項,並且還需要提供重新整理方法,返回BeanFactory例項的方法。

具體實現類:

  1. AbstractRefreshableApplicationContext

AbstractRefreshableApplicationContext中實現的重新整理方法,可以被多次的呼叫執行,也就是可以多次重新整理。

/**
 * 實際的重新整理方法。關閉以前的bean工廠,初始化一個新的bean工廠
 */
@Override
protected final void refreshBeanFactory() throws BeansException {
   //如果存在bean工廠,則銷燬單例bean,關閉bean工廠
   if (hasBeanFactory()) {
      destroyBeans();
      closeBeanFactory();
   }
   try {
      //建立DefaultListableBeanFactory例項
      DefaultListableBeanFactory beanFactory = createBeanFactory();
      //設定ID
      beanFactory.setSerializationId(getId());
      //自定義bean工廠屬性,bean定義重新、迴圈引用等配置
      customizeBeanFactory(beanFactory);
      //等待子類實現的,向bean工廠註冊bean定義
      loadBeanDefinitions(beanFactory);
      //持有bean工廠的例項,以便在getBeanFactory方法中返回出去
      this.beanFactory = beanFactory;
   }
   catch (IOException ex) {
      throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
   }
}

//自定義bean工廠屬性
protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
   if (this.allowBeanDefinitionOverriding != null) {
      beanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
   }
   if (this.allowCircularReferences != null) {
      beanFactory.setAllowCircularReferences(this.allowCircularReferences);
   }
}

//關閉bean工廠的方法
@Override
protected final void closeBeanFactory() {
   DefaultListableBeanFactory beanFactory = this.beanFactory;
   //如果bean工廠存在
   if (beanFactory != null) {
      //設定ID為空
      beanFactory.setSerializationId(null);
      //設定bean工廠的例項為空
      this.beanFactory = null;
   }
}

AbstractApplicationContext類中的銷燬方法:

//銷燬方法
protected void destroyBeans() {
   getBeanFactory().destroySingletons();
}
  1. GenericApplicationContext

GenericApplicationContext中的重新整理方法,只能被呼叫一次,被AtomicBoolean型別的refreshed控制著

private final AtomicBoolean refreshed = new AtomicBoolean();

/**
 * 持有一個內部的bean工廠,通過公共的方法註冊bean
 * @see #registerBeanDefinition
 */
@Override
protected final void refreshBeanFactory() throws IllegalStateException {
   //CAS的方式進行修改
   //多次重新整理就丟擲異常
   if (!this.refreshed.compareAndSet(false, true)) {
      throw new IllegalStateException(
            "GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once");
   }
   //設定ID
   this.beanFactory.setSerializationId(getId());
}

beanFactory在建構函式中被建立的,程式碼如下:

/**
 * Create a new GenericApplicationContext.
 * @see #registerBeanDefinition
 * @see #refresh
 */
public GenericApplicationContext() {
   this.beanFactory = new DefaultListableBeanFactory();
}

/**
 * Create a new GenericApplicationContext with the given DefaultListableBeanFactory.
 * @param beanFactory the DefaultListableBeanFactory instance to use for this context
 * @see #registerBeanDefinition
 * @see #refresh
 */
public GenericApplicationContext(DefaultListableBeanFactory beanFactory) {
   Assert.notNull(beanFactory, "BeanFactory must not be null");
   this.beanFactory = beanFactory;
}

這個時候的beanFactory裡面都有些什麼呢?根據之前的註解的載入過程。可以知道,應該有BeanFactoryPostProcessor、BeanPostProcessor、ApplicationListener資訊。

prepareBeanFactory方法

/**
 * 配置工廠的標準容器特徵
 * 例如容器的ClassLoader和post-processor
 * @param beanFactory the BeanFactory to configure
 */
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
   // Tell the internal bean factory to use the context's class loader etc.
   //給bean工作準備ClassLoader、SPL表示式解析器、屬性編輯註冊器等
   beanFactory.setBeanClassLoader(getClassLoader());
   beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
   beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

   // Configure the bean factory with context callbacks.
   //向beanFactory註冊ApplicationContextAwareProcessor
   //後面的六種Aware忽略他們的自動依賴注入
   beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
   beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
   beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
   beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
   beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
   beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
   beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);

   // BeanFactory interface not registered as resolvable type in a plain factory.
   // MessageSource registered (and found for autowiring) as a bean.
   //依賴需要使用的bean,IOC容器自己的多角色身份,@Autowried方式獲得下列型別bean
   beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
   beanFactory.registerResolvableDependency(ResourceLoader.class, this);
   beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
   beanFactory.registerResolvableDependency(ApplicationContext.class, this);

   // Register early post-processor for detecting inner beans as ApplicationListeners.
   //注入ApplicationListeners介面實現的發現處理器,同時帶有銷燬、bean定義混合的實現
   beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));

   // Detect a LoadTimeWeaver and prepare for weaving, if found.
   if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
      beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
      // Set a temporary ClassLoader for type matching.
      beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
   }

   // Register default environment beans.
   if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
      beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
   }
   if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
      // properties配置檔案
      beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
   }
   if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
      // 作業系統環境變數:JAVA_HOME等
      beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
   }
}

ApplicationContextAwareProcessor方法中的,注入Processor

@Override
@Nullable
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
//只針對EnvironmentAware、EmbeddedValueResolverAware、ResourceLoaderAware、ApplicationEventPublisherAware、MessageSourceAware、ApplicationContextAware 6種實現進行處理。
   if (!(bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||
         bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||
         bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)){
      return bean;
   }

   AccessControlContext acc = null;

   if (System.getSecurityManager() != null) {
      acc = this.applicationContext.getBeanFactory().getAccessControlContext();
   }

   if (acc != null) {
      AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
         invokeAwareInterfaces(bean);
         return null;
      }, acc);
   }
   else {
      invokeAwareInterfaces(bean);
   }

   return bean;
}

//將beanFactory注入給上述的6種Aware
private void invokeAwareInterfaces(Object bean) {
   if (bean instanceof EnvironmentAware) {
      ((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());
   }
   if (bean instanceof EmbeddedValueResolverAware) {
      ((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver);
   }
   if (bean instanceof ResourceLoaderAware) {
      ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
   }
   if (bean instanceof ApplicationEventPublisherAware) {
      ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);
   }
   if (bean instanceof MessageSourceAware) {
      ((MessageSourceAware) bean).setMessageSource(this.applicationContext);
   }
   if (bean instanceof ApplicationContextAware) {
      ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
   }
}

postProcessBeanFactory方法

postProcessBeanFactory方法並沒有去實現它。

/**
 * Modify the application context's internal bean factory after its standard
 * initialization. All bean definitions will have been loaded, but no beans
 * will have been instantiated yet. This allows for registering special
 * BeanPostProcessors etc in certain ApplicationContext implementations.
 * @param beanFactory the bean factory used by the application context
 */
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
}

invokeBeanFactoryPostProcessors方法

這個時候,bean工廠基本上已經準備完成,但是bean例項還沒有進行初始化,接下來呼叫BeanFactoryPostProcessor對Bean工廠中的Bean定義進行調整

/**
 * 執行BeanFactoryPostProcessor
 * <p>必須在單例bean例項化之前呼叫
 */
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
   //最重要的處理在委託裡面實現
   //getBeanFactoryPostProcessors()獲取的是容器裡面的列表屬性,
   //這個屬性沒有機會給外部去注入BeanFactoryPostProcessor物件。
   //因為ApplicationContext子類在建構函式中呼叫了refresh方法。所以這裡是個空值
   PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());

   // Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
   // (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
   if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
      beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
      beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
   }
}

invokeBeanFactoryPostProcessors方法:

public static void invokeBeanFactoryPostProcessors(
      ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

   // Invoke BeanDefinitionRegistryPostProcessors first, if any.
   Set<String> processedBeans = new HashSet<>();

   if (beanFactory instanceof BeanDefinitionRegistry) {
      BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
      //BeanFactoryPostProcessor和BeanDefinitionRegistryPostProcessor
      List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
      List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();

      for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
         if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
            //獲取BeanDefinitionRegistryPostProcessor
            BeanDefinitionRegistryPostProcessor registryProcessor =
                  (BeanDefinitionRegistryPostProcessor) postProcessor;
            //這裡既然做了處理,為何又加入到了registryProcessors集合中?   
            registryProcessor.postProcessBeanDefinitionRegistry(registry);
            registryProcessors.add(registryProcessor);
         }
         else {
            regularPostProcessors.add(postProcessor);
         }
      }

      // Do not initialize FactoryBeans here: We need to leave all regular beans
      // uninitialized to let the bean factory post-processors apply to them!
      // Separate between BeanDefinitionRegistryPostProcessors that implement
      // PriorityOrdered, Ordered, and the rest.
      List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();

      // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
      //第一步,處理繼承PriorityOrdered的BeanDefinitionRegistryPostProcessors
      String[] postProcessorNames =
            beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
      for (String ppName : postProcessorNames) {
         if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
            currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
            processedBeans.add(ppName);
         }
      }
      sortPostProcessors(currentRegistryProcessors, beanFactory);
      registryProcessors.addAll(currentRegistryProcessors);
      invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
      currentRegistryProcessors.clear();

      // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
      //第二步,處理繼承自Ordered的BeanDefinitionRegistryPostProcessors
      postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
      for (String ppName : postProcessorNames) {
         if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
            currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
            processedBeans.add(ppName);
         }
      }
      sortPostProcessors(currentRegistryProcessors, beanFactory);
      registryProcessors.addAll(currentRegistryProcessors);
      invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
      currentRegistryProcessors.clear();

      // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
      //最後一步,處理沒有優先順序的BeanDefinitionRegistryPostProcessors
      boolean reiterate = true;
      while (reiterate) {
         reiterate = false;
         postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
         for (String ppName : postProcessorNames) {
            if (!processedBeans.contains(ppName)) {
               currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
               processedBeans.add(ppName);
               reiterate = true;
            }
         }
         sortPostProcessors(currentRegistryProcessors, beanFactory);
         registryProcessors.addAll(currentRegistryProcessors);
         invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
         currentRegistryProcessors.clear();
      }

      // Now, invoke the postProcessBeanFactory callback of all processors handled so far.
      //回撥
      invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
      invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
   }

   else {
      // Invoke factory processors registered with the context instance.
      invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
   }

   // Do not initialize FactoryBeans here: We need to leave all regular beans
   // uninitialized to let the bean factory post-processors apply to them!
   String[] postProcessorNames =
         beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);

   // Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
   // Ordered, and the rest.
   //處理BeanFactoryPostProcessor,按照PriorityOrdered、Ordered和其他
   List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
   List<String> orderedPostProcessorNames = new ArrayList<>();
   List<String> nonOrderedPostProcessorNames = new ArrayList<>();
   for (String ppName : postProcessorNames) {
      if (processedBeans.contains(ppName)) {
         // skip - already processed in first phase above
      }
      else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
         priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
      }
      else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
         orderedPostProcessorNames.add(ppName);
      }
      else {
         nonOrderedPostProcessorNames.add(ppName);
      }
   }

   // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
   //第一步,處理繼承自PriorityOrdered的BeanFactoryPostProcessors
   sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
   invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);

   // Next, invoke the BeanFactoryPostProcessors that implement Ordered.
   //第二步,處理繼承自Ordered的BeanFactoryPostProcessors
   List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
   for (String postProcessorName : orderedPostProcessorNames) {
      orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
   }
   sortPostProcessors(orderedPostProcessors, beanFactory);
   invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);

   // Finally, invoke all other BeanFactoryPostProcessors.
   //最後一步,處理其他的BeanFactoryPostProcessors
   List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
   for (String postProcessorName : nonOrderedPostProcessorNames) {
      nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
   }
   invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);

   // Clear cached merged bean definitions since the post-processors might have
   // modified the original metadata, e.g. replacing placeholders in values...
   //清除快取,因為後面的處理器可能會修改後設資料
   beanFactory.clearMetadataCache();
}

從上面的原始碼部分可以看到:

  • 拆成了兩部分:registryProcessors、regularPostProcessors,分別處理BeanDefinitionRegistryPostProcessor和BeanFactoryPostProcessor
  • 排序處理:PriorityOrdered、Ordered和為排序的,按照這樣的優先順序進行處理

多次呼叫了invokeBeanFactoryPostProcessors,因為在呼叫BeanFactoryPostProcessors的過程中,還會繼續產生BeanFactoryPostProcessors,這裡多次呼叫,就是為了處理掉所有的BeanFactoryPostProcessors。

registerBeanPostProcessors方法

到了這一步,bean定義已經載入完成,bean工廠完成了重新整理和例項化,而且通過BeanFactoryProcessor修整了bean定義資訊,接下來註冊BeanPostProcessors

/**
 * Instantiate and register all BeanPostProcessor beans,
 * respecting explicit order if given.
 * <p>Must be called before any instantiation of application beans.
 */
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
   PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
}

PostProcessorRegistrationDelegate的registerBeanPostProcessors方法:

public static void registerBeanPostProcessors(
      ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {

   String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);

   // Register BeanPostProcessorChecker that logs an info message when
   // a bean is created during BeanPostProcessor instantiation, i.e. when
   // a bean is not eligible for getting processed by all BeanPostProcessors.
   int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
   beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));

   // Separate between BeanPostProcessors that implement PriorityOrdered,
   // Ordered, and the rest.
   List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
   List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
   List<String> orderedPostProcessorNames = new ArrayList<>();
   List<String> nonOrderedPostProcessorNames = new ArrayList<>();
   for (String ppName : postProcessorNames) {
      if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
         BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
         priorityOrderedPostProcessors.add(pp);
         if (pp instanceof MergedBeanDefinitionPostProcessor) {
            internalPostProcessors.add(pp);
         }
      }
      else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
         orderedPostProcessorNames.add(ppName);
      }
      else {
         nonOrderedPostProcessorNames.add(ppName);
      }
   }

   // First, register the BeanPostProcessors that implement PriorityOrdered.
   sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
   registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);

   // Next, register the BeanPostProcessors that implement Ordered.
   List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
   for (String ppName : orderedPostProcessorNames) {
      BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
      orderedPostProcessors.add(pp);
      if (pp instanceof MergedBeanDefinitionPostProcessor) {
         internalPostProcessors.add(pp);
      }
   }
   sortPostProcessors(orderedPostProcessors, beanFactory);
   registerBeanPostProcessors(beanFactory, orderedPostProcessors);

   // Now, register all regular BeanPostProcessors.
   List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
   for (String ppName : nonOrderedPostProcessorNames) {
      BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
      nonOrderedPostProcessors.add(pp);
      if (pp instanceof MergedBeanDefinitionPostProcessor) {
         internalPostProcessors.add(pp);
      }
   }
   registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);

   // Finally, re-register all internal BeanPostProcessors.
   sortPostProcessors(internalPostProcessors, beanFactory);
   registerBeanPostProcessors(beanFactory, internalPostProcessors);

   // Re-register post-processor for detecting inner beans as ApplicationListeners,
   // moving it to the end of the processor chain (for picking up proxies etc).
   beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}

可以看到上面的原始碼部分,和與BeanFactroyPostProcessor的處理簡直一樣,排序然後呼叫處理邏輯。

  1. priorityOrderedPostProcessors
  2. orderedPostProcessors
  3. nonOrderedPostProcessors
  4. internalPostProcessors
  5. ApplicationListenerDetector

下面是排序的例項:

  • 沒有繼承任何排序的介面:
@Component
public class MyBeanDefinitonRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {

   @Override
   public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
      System.out.println("--- MyBeanDefinitonRegistryPostProcessor.postProcessBeanFactory 被執行了。");

   }

   @Override
   public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
      System.out.println("--- MyBeanDefinitonRegistryPostProcessor.postProcessBeanDefinitionRegistry 被執行了。");
   }

}
  • 繼承Ordered介面:
@Component
public class MyBeanDefinitonRegistryPostProcessor2 implements BeanDefinitionRegistryPostProcessor, Ordered {

   @Override
   public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
      System.out.println("--- MyBeanDefinitonRegistryPostProcessor2.postProcessBeanFactory 被執行了。");

   }

   @Override
   public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
      System.out.println("--- MyBeanDefinitonRegistryPostProcessor2.postProcessBeanDefinitionRegistry 被執行了。");
   }

   @Override
   public int getOrder() {
      return 0;
   }
}
  • 繼承PriorityOrdered介面:
@Component
public class MyBeanDefinitonRegistryPostProcessor3 implements BeanDefinitionRegistryPostProcessor, PriorityOrdered {

   @Override
   public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
      System.out.println("--- MyBeanDefinitonRegistryPostProcessor3.postProcessBeanFactory 被執行了。");

   }

   @Override
   public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
      System.out.println("--- MyBeanDefinitonRegistryPostProcessor3.postProcessBeanDefinitionRegistry 被執行了。");
   }

   @Override
   public int getOrder() {
      return 1;
   }
}

執行結果如下:

image.png

從上圖可以清楚的看到,PriorityOrdered > Ordered > 未排序

initMessageSource方法

此時BenPostProcessor都已經註冊完成,下一步就是準備國際化資源:

/**
 * 初始化國際化資源,如果沒有,就使用父類的
 */
protected void initMessageSource() {
   //獲取bean工廠
   ConfigurableListableBeanFactory beanFactory = getBeanFactory();
   //如果Bean工廠中存在名為messageSource的bean,則使用它
   if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
      this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
      // Make MessageSource aware of parent MessageSource.
      //獲取父類的MessageSource
      if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {
         HierarchicalMessageSource hms = (HierarchicalMessageSource) this.messageSource;
         if (hms.getParentMessageSource() == null) {
            // Only set parent context as parent MessageSource if no parent MessageSource
            // registered already.
            hms.setParentMessageSource(getInternalParentMessageSource());
         }
      }
      if (logger.isTraceEnabled()) {
         logger.trace("Using MessageSource [" + this.messageSource + "]");
      }
   }
   else {
      // Use empty MessageSource to be able to accept getMessage calls.
      //使用DelegatingMessageSource作為國際化資源處理bean
      DelegatingMessageSource dms = new DelegatingMessageSource();
      dms.setParentMessageSource(getInternalParentMessageSource());
      this.messageSource = dms;
      beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);
      if (logger.isTraceEnabled()) {
         logger.trace("No '" + MESSAGE_SOURCE_BEAN_NAME + "' bean, using [" + this.messageSource + "]");
      }
   }
}

簡單的例子:
在application.properties配置如下資訊:

logging.config=classpath:logback.xml
my.love=java
@Configuration
@PropertySource("classpath:/application.properties")
public class MessageConfiguration {

   @Bean("messageSource")
   public ReloadableResourceBundleMessageSource getReloadableResourceBundleMessageSource() {
      ReloadableResourceBundleMessageSource rms = new ReloadableResourceBundleMessageSource();
      rms.setBasename("message");
      return rms;
   }
}
@MyComponetAnno
public class MessageBean {

   @Autowired
   private ApplicationContext applicationContext;

   public MessageBean() {
      System.out.println("-----------------Abean 被例項化了。。。。。。。。。");
   }

   public void doSomething() {
      System.out.println(this + " do something .....my.love="
            + this.applicationContext.getEnvironment().getProperty("my.love"));
      System.out
            .println("-----------project.name=" + 
                  this.applicationContext.getMessage("project.name", null, Locale.CHINA));
   }
}
public class MessageStarter {

   public static void main(String[] args) {
      // 註解方式,指定掃描的包
      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
            "edu.demo.spring.message");
      MessageBean bean = context.getBean(MessageBean.class);
      bean.doSomething();
      context.close();

   }
}

輸出如下:

image.png

initApplicationEventMulticaster方法

下面就是初始化事件多播器:

/**
 * 初始化ApplicationEventMulticaster.
 * Uses SimpleApplicationEventMulticaster if none defined in the context.
 * @see org.springframework.context.event.SimpleApplicationEventMulticaster
 */
protected void initApplicationEventMulticaster() {
   //獲取bean工廠
   ConfigurableListableBeanFactory beanFactory = getBeanFactory();
   //如果bean工廠中包含applicationEventMulticaster,就使用它
   if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
      this.applicationEventMulticaster =
            beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
      if (logger.isTraceEnabled()) {
         logger.trace("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
      }
   }
   else {
      //使用SimpleApplicationEventMulticaster
      this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
      beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
      if (logger.isTraceEnabled()) {
         logger.trace("No '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "' bean, using " +
               "[" + this.applicationEventMulticaster.getClass().getSimpleName() + "]");
      }
   }
}

onRefresh方法

沒有子類進行實現

/**
 * Template method which can be overridden to add context-specific refresh work.
 * Called on initialization of special beans, before instantiation of singletons.
 * <p>This implementation is empty.
 * @throws BeansException in case of errors
 * @see #refresh()
 */
protected void onRefresh() throws BeansException {
   // For subclasses: do nothing by default.
}

registerListeners方法

接下來,檢查事件監聽器,並註冊到事件傳播器上

/**
 *  在不生成Bean物件的情況下新增ApplicationListener bean作為監聽器
 * Add beans that implement ApplicationListener as listeners.
 * Doesn't affect other listeners, which can be added without being beans.
 */
protected void registerListeners() {
   // Register statically specified listeners first.
   for (ApplicationListener<?> listener : getApplicationListeners()) {
      getApplicationEventMulticaster().addApplicationListener(listener);
   }

   // Do not initialize FactoryBeans here: We need to leave all regular beans
   // uninitialized to let post-processors apply to them!
   String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
   for (String listenerBeanName : listenerBeanNames) {
      getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
   }

   // Publish early application events now that we finally have a multicaster...
   Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
   this.earlyApplicationEvents = null;
   if (!CollectionUtils.isEmpty(earlyEventsToProcess)) {
      for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
         getApplicationEventMulticaster().multicastEvent(earlyEvent);
      }
   }
}

事件廣播器執行的方法:
SimpleApplicationEventMulticaster的multicastEvent(ApplicationEvent, ResolvableType)方法:

@Override
public void multicastEvent(ApplicationEvent event) {
   multicastEvent(event, resolveDefaultEventType(event));
}

@Override
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
   ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
   Executor executor = getTaskExecutor();
   for (ApplicationListener<?> listener : getApplicationListeners(event, type)) {
      if (executor != null) {
         executor.execute(() -> invokeListener(listener, event));
      }
      else {
         invokeListener(listener, event);
      }
   }
}

finishBeanFactoryInitialization方法

完成bean工廠的初始化,將bean工廠中,非懶載入的單例bean進行例項化

這一部分將在後面的文章中進行分析

/**
 * Finish the initialization of this context's bean factory,
 * initializing all remaining singleton beans.
 */
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
   // Initialize conversion service for this context.
   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));
   }

   // Register a default embedded value resolver if no bean post-processor
   // (such as a PropertyPlaceholderConfigurer bean) registered any before:
   // at this point, primarily for resolution in annotation attribute values.
   if (!beanFactory.hasEmbeddedValueResolver()) {
      beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
   }

   // Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
   String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
   for (String weaverAwareName : weaverAwareNames) {
      getBean(weaverAwareName);
   }

   // Stop using the temporary ClassLoader for type matching.
   beanFactory.setTempClassLoader(null);

   // Allow for caching all bean definition metadata, not expecting further changes.
   beanFactory.freezeConfiguration();

   // Instantiate all remaining (non-lazy-init) singletons.
   beanFactory.preInstantiateSingletons();
}

finishRefresh方法

/**
 * Finish the refresh of this context, invoking the LifecycleProcessor's
 * onRefresh() method and publishing the
 * {@link org.springframework.context.event.ContextRefreshedEvent}.
 */
protected void finishRefresh() {
   // Clear context-level resource caches (such as ASM metadata from scanning).
   //清除快取
   clearResourceCaches();

   // Initialize lifecycle processor for this context.
   //初始化容器的生命週期處理器
   initLifecycleProcessor();

   // Propagate refresh to lifecycle processor first.
   //將重新整理傳播到生命週期處理器
   getLifecycleProcessor().onRefresh();

   // Publish the final event.
   //釋出最終事件
   publishEvent(new ContextRefreshedEvent(this));

   // Participate in LiveBeansView MBean, if active.
   LiveBeansView.registerApplicationContext(this);
}

容器的關閉:

AbstractApplicationContext#close

AbstractApplicationContext#registerShutdownHook

AbstractApplicationContext#doClose

整體流程圖如下:
image.png

相關文章