天天看點

【Spring源碼解析】 IOC 初始化流程分析1 .AbstractApplicationContext prepareBeanFactory2 核心方法解析2.1 obtainFreshBeanFactory() 擷取BeanFactory3 單例Bean注冊的位置

1 .AbstractApplicationContext prepareBeanFactory

@Override
    public void refresh() throws BeansException, IllegalStateException {
        synchronized (this.startupShutdownMonitor) {
            //重新整理前預處理
            prepareRefresh();

            //擷取beanFactory執行個體。(DefaultListableBeanFactory)
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

            //beanFactory屬性指派
            prepareBeanFactory(beanFactory);

            try {
                // 子類通過重寫這個方法在BeanFactory建立并預準備完成以後做進一步設定
                postProcessBeanFactory(beanFactory);

                // 調用實作BeanDefinitionRegistryPostProcessor,BeanFactoryPostProcessor 接口的類相關方法
                invokeBeanFactoryPostProcessors(beanFactory);
               
                // 注冊實作了BeanPostProcessor接口的Bean
                registerBeanPostProcessors(beanFactory);

                // 标簽國際化資源,初始化MessageSource元件
                initMessageSource();
  
                // 建立事件廣播器
                initApplicationEventMulticaster();
  
                // 空方法,留給子容器實作,在容器重新整理的時候可以子定義邏輯    
                onRefresh();
  
               // 注冊監聽器事件
                registerListeners();
 
               // 單例模式bean的注冊
                finishBeanFactoryInitialization(beanFactory);

                // 重新整理完成容器
                finishRefresh();
            }
            catch (BeansException ex) {
                if (logger.isWarnEnabled()) {
                    logger.warn("Exception encountered during context initialization - " +
                            "cancelling refresh attempt: " + ex);
                }
                // Destroy already created singletons to avoid dangling resources.
                destroyBeans();
                // Reset 'active' flag.
                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();
            }
        }
    }     
           

2 核心方法解析

2.1 obtainFreshBeanFactory() 擷取BeanFactory

(1) AbstractApplicationContext obtainFreshBeanFactory

protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
   //重新整理容器
   refreshBeanFactory();
   //擷取GenericApplicationContext建立的容器
   ConfigurableListableBeanFactory beanFactory = getBeanFactory();
   if (logger.isDebugEnabled()) {
      logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
   }
   return beanFactory;
}
           

GenericApplicationContext refreshBeanFactory()

【Spring源碼解析】 IOC 初始化流程分析1 .AbstractApplicationContext prepareBeanFactory2 核心方法解析2.1 obtainFreshBeanFactory() 擷取BeanFactory3 單例Bean注冊的位置
【Spring源碼解析】 IOC 初始化流程分析1 .AbstractApplicationContext prepareBeanFactory2 核心方法解析2.1 obtainFreshBeanFactory() 擷取BeanFactory3 單例Bean注冊的位置

2.2 invokeBeanFactoryPostProcessors(beanFactory)

實際調用PostProcessorRegistrationDelegate invokeBeanFactoryPostProcessors
(1) 調用實作BeanDefinitionRegistryPostProcessor接口的postProcessBeanDefinitionRegistry,postProcessBeanFactory方法
(2) 調用實作類BeanFactoryPostProcessor接口的postProcessBeanFactory方法
 PS:BeanFactoryPostProcessor源碼機票  -------> https://blog.csdn.net/qq_34125999/article/details/104645988
           

2.3 registerBeanPostProcessors(beanFactory);

實際調用PostProcessorRegistrationDelegate  registerBeanPostProcessors,其作用就是給工廠中添加實作了BeanPostProcessor接口的bean
PS:源碼機票  ------->  https://blog.csdn.net/qq_34125999/article/details/104572875
           

2.4 finishBeanFactoryInitialization(beanFactory);

完成單執行個體Bean的注冊。
PS:源碼機票  ------->  https://blog.csdn.net/qq_34125999/article/details/104547157
           

3 單例Bean注冊的位置

【Spring源碼解析】 IOC 初始化流程分析1 .AbstractApplicationContext prepareBeanFactory2 核心方法解析2.1 obtainFreshBeanFactory() 擷取BeanFactory3 單例Bean注冊的位置
【Spring源碼解析】 IOC 初始化流程分析1 .AbstractApplicationContext prepareBeanFactory2 核心方法解析2.1 obtainFreshBeanFactory() 擷取BeanFactory3 單例Bean注冊的位置

繼續閱讀