springboot啟動流程

pure-java發表於2021-02-25

[toc]

前言

​ 為了記錄 spring 的學習日誌,以筆記的形式將學習過程記錄下來,有問題或有遺漏請指出,謝謝!

現將spring boot啟動流程進行梳理記錄。

1. SpringApplication 準備

@SpringBootApplication
public class DomeApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(DomeApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(this.getClass());
    }
}

使用 @SpringBootApplication 註解,標識啟動類,它是一個複合註解,標識使用自動裝配、透過掃描註解注入bean。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
        @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication{}

在啟動類中,執行main方法,將呼叫SpringApplication.run

    public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
        return run(new Class<?>[] { primarySource }, args);
    }
    
    public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
        return new SpringApplication(primarySources).run(args);
    }
    
    public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
        return new SpringApplication(primarySources).run(args);
    }

透過原始碼看到,在SpringApplication.run中,使用primarySources構造 SpringApplication物件,然後呼叫run方法。準備工作在構造器中完成。

    public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
        // 用來獲取 Resource 和 classLoader 以及載入資源。
        this.resourceLoader = resourceLoader;
        Assert.notNull(primarySources, "PrimarySources must not be null");
        // 存放主載入類。
        this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
        // 推斷 web 型別:servlet 或 reactive。
        this.webApplicationType = WebApplicationType.deduceFromClasspath();
        // 獲取 ApplicationContextInitializer 的實現類,進行上下文初始化。
        setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
        // 獲取應用事件監聽器。
        setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
        // 推匯入口類。
        this.mainApplicationClass = deduceMainApplicationClass();
    }

2. SpringApplication 執行

run方法執行後,spring 開始正式執行。

public ConfigurableApplicationContext run(String... args) {
    // 計時器啟動
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    ConfigurableApplicationContext context = null;
    Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
    // 配置伺服器環境有無外設:如顯示器、滑鼠,如果需要這些資料需要cpu模擬。
    configureHeadlessProperty();
    // 獲取 spring 執行監聽器,從spring.factories檔案中載入
    // 如: EventPublishingRunListener(SpringApplication application, String[] args)
    SpringApplicationRunListeners listeners = getRunListeners(args);
    // 1. 監聽器釋出應用準備啟動 ApplicationStartingEvent 事件
    listeners.starting();
    try {
        // 將傳入的引數進行封裝。
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
        //2. 準備應用程式環境,並觸發事件
        ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
        // 設定Introspector.getBeaninfo引數是否忽略與引數class關聯的所有beanInfo
        configureIgnoreBeanInfo(environment);
        // 獲取橫幅物件
        Banner printedBanner = printBanner(environment);
        //3. 根據webApplicationType建立對應的spring context,並註冊註釋配置處理器
        context = createApplicationContext();
        // spring 啟動錯誤回撥
        exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
                                                         new Class[] { ConfigurableApplicationContext.class }, context);
        //4. 準備 applicationContext
        prepareContext(context, environment, listeners, applicationArguments, printedBanner);
               
        //5. 重新整理context
        refreshContext(context);
        
        //11. 上下文重新整理後觸發
        afterRefresh(context, applicationArguments);
        
        stopWatch.stop();
        if (this.logStartupInfo) {
            new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
        }
        // 釋出started事件
        listeners.started(context);
        callRunners(context, applicationArguments);
    }
    catch (Throwable ex) {
        // 異常處理
        handleRunFailure(context, ex, exceptionReporters, listeners);
        throw new IllegalStateException(ex);
    }

    try {
        // 觸發running事件
        listeners.running(context);
    }
    catch (Throwable ex) {
        handleRunFailure(context, ex, exceptionReporters, null);
        throw new IllegalStateException(ex);
    }
    return context;
}
2.1 觸發SpringApplication開始啟動事件

獲取spring.factories中 key為SpringApplicationRunListener的物件例項。

    // SpringApplication類中
    private SpringApplicationRunListeners getRunListeners(String[] args) {
        Class<?>[] types = new Class<?>[] { SpringApplication.class, String[].class };
        return new SpringApplicationRunListeners(logger,
                getSpringFactoriesInstances(SpringApplicationRunListener.class, types, this, args));
    }
# Run Listeners
org.springframework.boot.SpringApplicationRunListener=\
org.springframework.boot.context.event.EventPublishingRunListener

建立EventPublishingRunListener物件時將SpringApplication中的監聽器存入廣播器中,進行事件廣播

public EventPublishingRunListener(SpringApplication application, String[] args) {
   this.application = application;
   this.args = args;
   this.initialMulticaster = new SimpleApplicationEventMulticaster();
   for (ApplicationListener<?> listener : application.getListeners()) {
      this.initialMulticaster.addApplicationListener(listener);
   }
}
2.2 準備環境 prepareEnvironment

將系統配置和系統環境載入進 ConfigurableEnvironment 中,如果有spring cloud 專案 bootstrap上下文,則會優先載入spring cloud 配置和遠端配置。

  
   /**
    * SpringApplication類中
    * 準備應用程式環境以及配置。
    * 將系統的相關屬性和環境變數配置載入進應用配置物件中,並把main方法輸入引數 args 解析後也加入到應用配置中。
    */
   private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
                                                      ApplicationArguments applicationArguments) {
       //1. 根據容器型別建立環境配置
       // 並將系統的相關屬性(systemProperties)和環境變數配置(systemEnvironment)載入進應用配置的物件中
       ConfigurableEnvironment environment = getOrCreateEnvironment();
       
       //2. 設定配置屬性轉換器、新增應用預設配置、設定 profile
       configureEnvironment(environment, applicationArguments.getSourceArgs());
       
       //3. 將MutablePropertySources轉換為ConfigurationPropertySourcesPropertySource,主要充當介面卡
       ConfigurationPropertySources.attach(environment);
       
       //4. 釋出 ApplicationEnvironmentPreparedEvent 事件
       // 其中 BootstrapApplicationListener 將引導spring cloud 上下文,載入 bootstrap 配置檔案
       // ConfigFileApplicationListener 載入配置檔案(application.yml)
       listeners.environmentPrepared(environment);
       
       //5. 將environment 中 spring.main 開頭的配置繫結到 application 例項上
       bindToSpringApplication(environment);
       
       if (!this.isCustomEnvironment) {
           //6. 將 environment 轉化為對應容器型別的 Environment 例項
           // StandardServletEnvironment、StandardReactiveWebEnvironment、StandardEnvironment
           environment = new EnvironmentConverter(getClassLoader())
               .convertEnvironmentIfNecessary(environment, deduceEnvironmentClass());
       }
       
       //7. 將MutablePropertySources 轉換為 ConfigurationPropertySourcesPropertySource
       ConfigurationPropertySources.attach(environment);
       return environment;
   }
2.3 建立 createApplicationContext

建立時會根據應用型別 webApplicationType 建立對應的 ApplicationContext,並註冊預設的 BeanDefinitionRegistryPostProcessor

   /**
    * SpringApplication類中
    * 建立對應的 ApplicationContext
    */    
    protected ConfigurableApplicationContext createApplicationContext() {
        Class<?> contextClass = this.applicationContextClass;
        if (contextClass == null) {
            try {
                switch (this.webApplicationType) {
                case SERVLET:
                        // AnnotationConfigServletWebServerApplicationContext
                    contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS);
                    break;
                case REACTIVE:
                        // AnnotationConfigReactiveWebServerApplicationContext
                    contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
                    break;
                default:
                        // AnnotationConfigApplicationContext
                    contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
                }
            }
            catch (ClassNotFoundException ex) {
                throw new IllegalStateException(
                        "Unable create a default ApplicationContext, please specify an ApplicationContextClass", ex);
            }
        }
        return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
    }

   // -------------------------------------------------------
   /**
    * 這三個ApplicationContext的構造器中,都會建立 AnnotatedBeanDefinitionReader 和 ClassPathBeanDefinitionScanner
    */    
    public AnnotationConfigServletWebServerApplicationContext() {
        // 根據註解註冊bean
        this.reader = new AnnotatedBeanDefinitionReader(this);
        // 根據class path 掃描bean
        this.scanner = new ClassPathBeanDefinitionScanner(this);
    }
    public AnnotationConfigReactiveWebServerApplicationContext() {
        this.reader = new AnnotatedBeanDefinitionReader(this);
        this.scanner = new ClassPathBeanDefinitionScanner(this);
    }
    public AnnotationConfigApplicationContext() {
        this.reader = new AnnotatedBeanDefinitionReader(this);
        this.scanner = new ClassPathBeanDefinitionScanner(this);
    }

   /**
    * AnnotatedBeanDefinitionReader 中會註冊幾個 BeanDefinitionRegistryPostProcessor
    */    
    public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry, Environment environment) {
        Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
        Assert.notNull(environment, "Environment must not be null");
        this.registry = registry;
        this.conditionEvaluator = new ConditionEvaluator(registry, environment, null);
        AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
    }

   /**
    * AnnotationConfigUtils.registerAnnotationConfigProcessors 
    * 註冊註解後置處理器,將在 refresh 方法中被呼叫到。
    */    
    public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
            BeanDefinitionRegistry registry, @Nullable Object source) {
        
        // 獲取 beanFactory
        DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);
        if (beanFactory != null) {
            // 設定預設排序器
            if (!(beanFactory.getDependencyComparator() instanceof AnnotationAwareOrderComparator)) {
                beanFactory.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
            }
            // 設定自動注入處理器
            if (!(beanFactory.getAutowireCandidateResolver() instanceof ContextAnnotationAutowireCandidateResolver)) {
                beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
            }
        }

        Set<BeanDefinitionHolder> beanDefs = new LinkedHashSet<>(8);

        // 註冊 ConfigurationClassPostProcessor,他支援 @Configuration、@ComponentScans、@ComponentScan、@ImportResource、@Import 等註解進行註冊bean。
        if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {
            RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
            def.setSource(source);
            beanDefs.add(registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME));
        }
        
        // 註冊 AutowiredAnnotationBeanPostProcessor,支援使用 @Autowired 註解、Setter、@Value 自動注入、xml配置。
        if (!registry.containsBeanDefinition(AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
            RootBeanDefinition def = new RootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class);
            def.setSource(source);
            beanDefs.add(registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
        }
        // 支援 jsr250Present 時註冊 CommonAnnotationBeanPostProcessor
        // Check for JSR-250 support, and if present add the CommonAnnotationBeanPostProcessor.
        if (jsr250Present && !registry.containsBeanDefinition(COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)) {
            RootBeanDefinition def = new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class);
            def.setSource(source);
            beanDefs.add(registerPostProcessor(registry, def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
        }
        // 支援jpa,註冊 PersistenceAnnotationBeanPostProcessor
        // Check for JPA support, and if present add the PersistenceAnnotationBeanPostProcessor.
        if (jpaPresent && !registry.containsBeanDefinition(PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME)) {
            RootBeanDefinition def = new RootBeanDefinition();
            try {
                def.setBeanClass(ClassUtils.forName(PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME,
                        AnnotationConfigUtils.class.getClassLoader()));
            }
            catch (ClassNotFoundException ex) {
                throw new IllegalStateException(
                        "Cannot load optional framework class: " + PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME, ex);
            }
            def.setSource(source);
            beanDefs.add(registerPostProcessor(registry, def, PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME));
        }
        
        // 註冊 EventListenerMethodProcessor ,為事件監聽器提供支援
        if (!registry.containsBeanDefinition(EVENT_LISTENER_PROCESSOR_BEAN_NAME)) {
            RootBeanDefinition def = new RootBeanDefinition(EventListenerMethodProcessor.class);
            def.setSource(source);
            beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_PROCESSOR_BEAN_NAME));
        }
        
        // 註冊 DefaultEventListenerFactory,設定預設的監聽器工廠
        if (!registry.containsBeanDefinition(EVENT_LISTENER_FACTORY_BEAN_NAME)) {
            RootBeanDefinition def = new RootBeanDefinition(DefaultEventListenerFactory.class);
            def.setSource(source);
            beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_FACTORY_BEAN_NAME));
        }

        return beanDefs;
    }
2.4 準備 prepareContext

配置 applicationContext,呼叫 ApplicationContextInitializer 實現類

   /**
    * SpringApplication 中
    * 對 applicationContext 進行初始化設定。
    */
   private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment,
                               SpringApplicationRunListeners listeners, 
                               ApplicationArguments applicationArguments, Banner printedBanner) {
       //1. 設定環境
       context.setEnvironment(environment);
       //2. 設定 beanNameGenerator和bean 屬性資料型別轉換服務
       postProcessApplicationContext(context);
       //3. 呼叫 ApplicationContextInitializer
       applyInitializers(context);
       //4. 釋出 contextPrepared 事件
       listeners.contextPrepared(context);
       if (this.logStartupInfo) {
           logStartupInfo(context.getParent() == null);
           logStartupProfileInfo(context);
       }
       //5. 獲取 beanFactory
       ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
       //6. 註冊args引數為單例bean
       beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
       if (printedBanner != null) {
           // 7. 註冊banner為單例
           beanFactory.registerSingleton("springBootBanner", printedBanner);
       }
       //8. 設定 beanFactory 中可以覆蓋定義 bean
       if (beanFactory instanceof DefaultListableBeanFactory) {
           ((DefaultListableBeanFactory) beanFactory)
                   .setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
       }
       //9. 啟用懶載入所有bean:在spring啟動時不會載入bean,會在使用時進行載入
       if (this.lazyInitialization) {
           context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor());
       }
       //10 載入 main 方法所在的主類
       Set<Object> sources = getAllSources();
       Assert.notEmpty(sources, "Sources must not be empty");
       //11 將主類載入進beanFactory中
       load(context, sources.toArray(new Object[0]));
       //12 觸發contextLoade事件
       listeners.contextLoaded(context);
   }

   /**
    * SpringApplication 中
    * 2. 設定 applicationContext
    */    
    protected void postProcessApplicationContext(ConfigurableApplicationContext context) {
        //2.1 註冊 bean 名稱生成器
        if (this.beanNameGenerator != null) {
            context.getBeanFactory().registerSingleton(AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR,
                    this.beanNameGenerator);
        }
        //2.2 為上下文設定 資源載入器resourceLoader 和 classLoader
        if (this.resourceLoader != null) {
            if (context instanceof GenericApplicationContext) {
                ((GenericApplicationContext) context).setResourceLoader(this.resourceLoader);
            }
            if (context instanceof DefaultResourceLoader) {
                ((DefaultResourceLoader) context).setClassLoader(this.resourceLoader.getClassLoader());
            }
        }
        //2.3 註冊型別轉換服務:ConversionService
        if (this.addConversionService) {
            context.getBeanFactory().setConversionService(ApplicationConversionService.getSharedInstance());
        }
    }

   /**
    * SpringApplication 中
    * 3. 呼叫 ApplicationContextInitializer 初始化介面
    */    
    protected void applyInitializers(ConfigurableApplicationContext context) {
        for (ApplicationContextInitializer initializer : getInitializers()) {
            // 檢查泛型的型別與context一致
            Class<?> requiredType = GenericTypeResolver.resolveTypeArgument(initializer.getClass(),
                    ApplicationContextInitializer.class);
            Assert.isInstanceOf(requiredType, context, "Unable to call initializer.");
            initializer.initialize(context);
        }
    }
2.5 載入或重新整理 refreshContext

該方法主要是將應用程式中的bean註冊到spring application context 中,也就是 BeanFactory 中,進行統一管理;並且在註冊前後對bean 進行增強處理,如BeanDefinitionRegistryPostProcessor \`BeanFactoryPostProcessor` ,增強 bean 的註冊。

 
   /**
    * SpringApplication 中
    */    
    private void refreshContext(ConfigurableApplicationContext context) {
        if (this.registerShutdownHook) {
            try {
                // 註冊容器關閉鉤子
                context.registerShutdownHook();
            }
            catch (AccessControlException ex) {
                // Not allowed in some environments.
            }
        }
        refresh((ApplicationContext) context);
    }

    /**
     * SpringApplication 中
     * 載入或重新整理springContext 
     */
    @Override
    public void refresh() throws BeansException, IllegalStateException {
        // 同步
        synchronized (this.startupShutdownMonitor) {
            //1 準備上下文重新整理:將 applicationContext標記為 active狀態
            prepareRefresh();

            //2 重新整理 beanFactory
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

            //3 準備beanFactory,並手動註冊一些bean、新增 BeanPostProcessor
            prepareBeanFactory(beanFactory);

            try {
                //4 對 beanFactory 進行後置處理,交由子類實現
                postProcessBeanFactory(beanFactory);

                //5 呼叫已經註冊的BeanFactoryPostProcessors:如從xml中以及用scan掃描java config、Component、configuration載入java bean Definition到beanFactory中
                invokeBeanFactoryPostProcessors(beanFactory);

                //6 註冊 BeanPostProcessor,僅僅是註冊,呼叫在getBean的時候
                registerBeanPostProcessors(beanFactory);

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

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

                //9 留給子類實現的重新整理方法,在子類中,會建立對應的web容器:tomcat、jetty
                onRefresh();

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

                //11 初始化非懶載入的bean
                finishBeanFactoryInitialization(beanFactory);

                //12 最後一步,完成重新整理過程,釋出應用事件
                finishRefresh();
            }
            catch (BeansException ex) {
                if (logger.isWarnEnabled()) {
                    logger.warn("Exception encountered during context initialization - " +
                                "cancelling refresh attempt: " + ex);
                }
                //發生異常,則將已經初始化的bean登出掉
                destroyBeans();

                // Reset 'active' flag.
                // 取消重新整理,將active 置為false
                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.5.1 beanFactory 重新整理準備

將spring context 標記為啟用狀態(active),並且初始化和驗證配置檔案,保證配置能被正確解析和指定的配置屬性必須存在。

    /**
     * AbstractApplicationContext 中
     * 1. 準備重新整理 
     */    
    protected void prepareRefresh() {
        // Switch to active.
        this.startupDate = System.currentTimeMillis();
        //1.1 啟用
        this.closed.set(false);
        this.active.set(true);

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

        // Initialize any placeholder property sources in the context environment.
        //1.2 在上下文環境中初始化配置的佔位符,啟動時為空實現
        initPropertySources();

        // Validate that all properties marked as required are resolvable:
        // see ConfigurablePropertyResolver#setRequiredProperties
        //1.3 對所有的必要屬性進行驗證,如果有必要的屬性沒有配置,則會丟擲異常
        getEnvironment().validateRequiredProperties();

        //1.4 儲存重新整理之前的監聽器
        // 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);
        }

        //1.5 收集事件,一旦廣播器準備好就可以釋出事件
        // Allow for the collection of early ApplicationEvents,
        // to be published once the multicaster is available...
        this.earlyApplicationEvents = new LinkedHashSet<>();
    }


2.5.2 beanFactory 獲取
    /**
     * AbstractApplicationContext 中
     */        
    protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
        //2.1 可以在該方法中重新整理 beanFactory,如替換新的 beanFactory、重新配置 beanFactory等。
        refreshBeanFactory();
        //2.2 獲取更新後的beanFactory
        return getBeanFactory();
    }
2.5.3 beanFactory 配置
    /**
     * AbstractApplicationContext 中
     * 3. 設定 beanFactory 
     */    
    protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
        // Tell the internal bean factory to use the context's class loader etc.
        //3.1 設定類載入器
        beanFactory.setBeanClassLoader(getClassLoader());
        //3.2 設定el表示式解析器
        beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
        // 
        beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

        // Configure the bean factory with context callbacks.
        //3.3 新增 BeanPostProcessor ,完成與bean相關的XXXAware介面的注入工作
        beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
        //3.4 忽略這些介面的實現類中的依賴自動注入(setter注入),介面的依賴由 ApplicationContextAwareProcessor 統一處理注入的介面。
        beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
        beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
        beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
        beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
        beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
        beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);

        //3.5 註冊自動裝配物件,這樣BeanFactory/ApplicationContext雖然沒有以bean的方式被定義在工廠中,也支援自動注入。
        // BeanFactory interface not registered as resolvable type in a plain factory.
        // MessageSource registered (and found for autowiring) as a bean.
        beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
        beanFactory.registerResolvableDependency(ResourceLoader.class, this);
        beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
        beanFactory.registerResolvableDependency(ApplicationContext.class, this);

        //3.6 在 bean 初始化後,檢查該bean是否為 ApplicationListener,如是則將其加入到SpringContext中
        // Register early post-processor for detecting inner beans as ApplicationListeners.
        beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));

        // Detect a LoadTimeWeaver and prepare for weaving, if found.
        //3.7 https://www.cnblogs.com/wade-luffy/p/6078446.html
        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()));
        }

        //3.8 註冊預設的環境單例物件
        // Register default environment beans.
        if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
            beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
        }
        if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
            beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
        }
        if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
            beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
        }
    }
2.5.4 beanFactory 後置處理
    
    /**
     * AbstractApplicationContext 中
     * 對 beanFactory 進行後置處理,交由子類實現
     */    
    protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    }
2.5.5 beanFactory 後置處理器呼叫

​ 首先根據順序策略呼叫 BeanDefinitionRegistryPostProcessor 實現類。

​ 之後根據順序策略呼叫 BeanFactoryPostProcessor 實現類, 同時實現了BeanDefinitionRegistryPostProcessorBeanFactoryPostProcessor介面的實現類比只實現BeanFactoryPostProcessor介面的實現類先呼叫。

順序策略:優先呼叫實現了 PriorityOrdered 介面的實現類、在呼叫實現了 Ordered 介面的實現類,之後呼叫未實現前面兩個介面的實現類。

BeanDefinitionRegistryPostProcessor介面提供了可以註冊、刪除BeanDefinition 的支援, BeanFactoryPostProcessor介面提供了新增或修改BeanDefinition 的屬性支援。這兩個介面執行時,操作的是 BeanDefinition,這時 bean 還未被例項化的

createApplicationContext 方法中會載入 ConfigurationClassPostProcessorAutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessor,這幾個 beanFactory 後置處理器將會在這裡執行,進行掃描需要由spring管理的 BeanDefinition

  
   /**
    * AbstractApplicationContext 中
    * 5. 呼叫beanFactory後置處理器
    */    
    protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
        //5.1 實際呼叫
        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()));
        }
    }

    /**
     * PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors 
     * 5.1 呼叫beanFactory後置處理器
     * 後置處理器的執行流程
     */    
    public static void invokeBeanFactoryPostProcessors(
            ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

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

        // beanFactory 是BeanDefinitionRegistry型別
        if (beanFactory instanceof BeanDefinitionRegistry) {
            BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
            // 常規的BeanFactoryPostProcessor
            List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
            // BeanDefinitionRegistryPostProcessor型別的物件
            List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();

            for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
                // 為BeanDefinitionRegistryPostProcessor實現類設定BeanDefinitionRegistry
                if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
                    BeanDefinitionRegistryPostProcessor registryProcessor =
                            (BeanDefinitionRegistryPostProcessor) postProcessor;
                    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<>();

            // 獲取 BeanDefinitionRegistryPostProcessor 型別的bean名稱集合,
            // 首先執行實現了 PriorityOrdered 介面的 BeanDefinitionRegistryPostProcessor 實現類。
            // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
            String[] postProcessorNames =
                    beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
            for (String ppName : postProcessorNames) {
                // 將實現了PriorityOrdered的 BeanDefinitionRegistryPostProcessor 物件加入集合
                if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
                    currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                    processedBeans.add(ppName);
                }
            }
            // 對實現了PriorityOrdered的 BeanDefinitionRegistryPostProcessor 進行排序
            sortPostProcessors(currentRegistryProcessors, beanFactory);
            // 加入到 registryProcessors 集合中,對 註冊處理器進行彙總
            registryProcessors.addAll(currentRegistryProcessors);
           
            // 呼叫鉤子:BeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistry
            // bean定義已經被載入,但是沒有例項化的,允許新增自定義的bean定義
            invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
            currentRegistryProcessors.clear();

            // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
            postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
            // 執行實現了 Ordered 介面的 BeanDefinitionRegistryPostProcessor 。
            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 集合中,對 註冊處理器進行彙總
            registryProcessors.addAll(currentRegistryProcessors);
            
            // 呼叫鉤子:BeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistry
            // 實現 PriorityOrdered 介面比實現 Ordered 介面的實現類先執行
            invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
            // 清空當前集合
            currentRegistryProcessors.clear();

            // 執行其他的 BeanDefinitionRegistryPostProcessors 。
            // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
            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();
            }
            // 執行 BeanFactoryPostProcessor 和 BeanDefinitionRegistryPostProcessor 的共同實現類的 postProcessBeanFactory()。
            // 所有的bean 定義已經被載入,可以新增或修改其中的屬性值,在 BeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistry 後面執行。
            
            // 保證 spring 提供的 BeanFactoryPostProcessor 實現類先被執行
            // 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);
        }

        // 執行其他未執行 postProcessBeanFactory() 方法的 BeanFactoryPostProcessor實現類。
        // 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.
        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.
        sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
        invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);

        // Next, invoke the BeanFactoryPostProcessors that implement Ordered.
        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.
        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();
    }
2.5.6 beanFactory 註冊BeanPostProcessors

註冊BeanPostProcessor 實現,BeanPostProcessor支援對bean進行增強,在bean建立前後進行特殊操作。

/**
 * postProcessBeforeInitialization:在建立好bean之後執行,支援在bean建立後進行增強,比如修改bean的屬性、使用代理進行代理。
 *
 * postProcessAfterInitialization:在執行了bean的 init 方法後執行(實現了`InitializingBean#afterPropertiesSet()`介面或xml配置中指定了init-method 屬性的bean)。
 * postProcessAfterInitialization 方法與 postProcessBeforeInitialization 方法一樣,只是執行時機不一樣,一個是在執行init方法之前,一個是在init方法之後。
 */    
public interface BeanPostProcessor {

    /**
     * InitializingBean#afterPropertiesSet() 之前執行
     */
    @Nullable
    default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    /**
     * InitializingBean#afterPropertiesSet() 之後執行
     */
    @Nullable
    default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}

註冊 BeanPostProcessors。

    /**
     * 
     * AbstractApplicationContext 中
     */    
    protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
        PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
    }

    /**
     * PostProcessorRegistrationDelegate.registerBeanPostProcessors 
     * 5.1 向BeanFactory 註冊 BeanPostProcessors 
     */    
    public static void registerBeanPostProcessors(
            ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
        
        // 獲取BeanPostProcessor 實現類的bean名稱
        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.
        // 記錄bean處理器的總數,用於記錄執行過的處理器與記錄的處理器個數是否一致
        int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
        beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));

        // Separate between BeanPostProcessors that implement PriorityOrdered,
        // Ordered, and the rest.
        // 將處理器按照實現的介面進行拆分。
        // 實現了PriorityOrdered介面的BeanPostProcessor
        List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
        // 內部BeanPostProcessor,也就是實現了MergedBeanDefinitionPostProcessor介面的類
        List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
        // 實現類Order介面的類
        List<String> orderedPostProcessorNames = new ArrayList<>();
        // 未實現Order介面的類
        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);
                // 記錄內部BeanPostProcessor
                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.
        // 排序並註冊實現了PriorityOrdered介面的BeanPostProcessors,包括了內部BeanPostProcessors
        sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
        registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);

        // Next, register the BeanPostProcessors that implement Ordered.
        // 排序並註冊實現了Ordered介面的BeanPostProcessors
        List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
        for (String ppName : orderedPostProcessorNames) {
            BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
            orderedPostProcessors.add(pp);
            // 記錄內部BeanPostProcessor
            if (pp instanceof MergedBeanDefinitionPostProcessor) {
                internalPostProcessors.add(pp);
            }
        }
        sortPostProcessors(orderedPostProcessors, beanFactory);
        registerBeanPostProcessors(beanFactory, orderedPostProcessors);

        // 註冊未實現Order介面的BeanPostProcessors
        // 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);
            // 記錄內部BeanPostProcessor
            if (pp instanceof MergedBeanDefinitionPostProcessor) {
                internalPostProcessors.add(pp);
            }
        }
        registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);

        // 排序並重新註冊內部BeanPostProcessors
        // 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));
    }
2.5.7 初始化MessageSource

初始化 MessageSource,將 MessageSource委託給 DelegatingMessageSource,並註冊進 beanFactory 中

    
    /**
     * AbstractApplicationContext 中
     */    
    protected void initMessageSource() {
        ConfigurableListableBeanFactory beanFactory = getBeanFactory();
        if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
            this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
            // Make MessageSource aware of parent 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 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 + "]");
            }
        }
    }
2.5.8 初始化事件廣播器
    /**
     * 註冊事件廣播器,預設使用 SimpleApplicationEventMulticaster
     */
    protected void initApplicationEventMulticaster() {
        ConfigurableListableBeanFactory beanFactory = getBeanFactory();
        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 {
            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() + "]");
            }
        }
    }
2.5.9 初始化其他特殊bean
    // AbstractApplicationContext
    protected void onRefresh() throws BeansException {
        // For subclasses: do nothing by default.
    }
    
    // ServletWebServerApplicationContext
    @Override
    protected void onRefresh() {
        super.onRefresh();
        try {
            createWebServer();
        }
        catch (Throwable ex) {
            throw new ApplicationContextException("Unable to start reactive web server", ex);
        }
    }

    // ServletWebServerApplicationContext,建立web服務
    private void createWebServer() {
        WebServerManager serverManager = this.serverManager;
        if (serverManager == null) {
            String webServerFactoryBeanName = getWebServerFactoryBeanName();
            ReactiveWebServerFactory webServerFactory = getWebServerFactory(webServerFactoryBeanName);
            boolean lazyInit = getBeanFactory().getBeanDefinition(webServerFactoryBeanName).isLazyInit();
            this.serverManager = new WebServerManager(this, webServerFactory, this::getHttpHandler, lazyInit);
            getBeanFactory().registerSingleton("webServerGracefulShutdown",
                    new WebServerGracefulShutdownLifecycle(this.serverManager));
            getBeanFactory().registerSingleton("webServerStartStop",
                    new WebServerStartStopLifecycle(this.serverManager));
        }
        initPropertySources();
    }
2.5.10 註冊事件到事件廣播器中

註冊事件監聽器,併發布早期事件

    /**
     * AbstractApplicationContext
     */
    protected void registerListeners() {
        // 註冊在spring boot啟動時建立的監聽器例項到事件廣播器中: 
        // SpringApplication#prepareContext() 和 SpringApplication#applyInitializers() 中執行
        // ApplicationContextInitializer#initialize 時,新增的監聽器
        for (ApplicationListener<?> listener : getApplicationListeners()) {
            getApplicationEventMulticaster().addApplicationListener(listener);
        }

        // 查詢應用中ApplicationListener實現類,獲取beanName,註冊到廣播器中
        String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
        for (String listenerBeanName : listenerBeanNames) {
            getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
        }

        // 廣播在之前捕獲到的事件(在容器初始化完bean前釋出的事件將會暫存在earlyApplicationEvents中,進行延遲釋出)
        // Publish early application events now that we finally have a multicaster...
        Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
        // 這之後的事件都直接釋出,不在暫存到earlyApplicationEvents中進行延遲釋出
        this.earlyApplicationEvents = null;
        if (!CollectionUtils.isEmpty(earlyEventsToProcess)) {
            for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
                getApplicationEventMulticaster().multicastEvent(earlyEvent);
            }
        }
    }
2.5.11 例項化剩餘非lazy load 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.
        // 註冊型別轉換服務:ConversionService,在 SpringApplication#postProcessApplicationContext 中預設已經註冊
        // 這裡進行再次檢查
        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.
        // 再次檢查是否有字串轉換器:StringValueResolver,沒有則將解析委託給 PropertySourcesPropertyResolver
        if (!beanFactory.hasEmbeddedValueResolver()) {
            beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
        }

        // 先建立LoadTimeWeaverAware 型別的例項 bean
        // 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);
        
        // 允許快取 bean definition 
        // Allow for caching all bean definition metadata, not expecting further changes.
        beanFactory.freezeConfiguration();

        // 例項化不是懶載入的bean
        // Instantiate all remaining (non-lazy-init) singletons.
        beanFactory.preInstantiateSingletons();
    }

    /**
     * 例項化單例物件
     */ 
    @Override
    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.
        List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
        
        // 建立非懶載入的例項物件
        // Trigger initialization of all non-lazy singleton beans...
        for (String beanName : beanNames) {
            // 獲取頂層bean定義
            RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
            // 不是抽象、單例、非懶載入
            if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
                // 判斷是否是工程bean
                if (isFactoryBean(beanName)) {
                    // 如果是工程bean,則新增字首進行查詢
                    Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
                    if (bean instanceof FactoryBean) {
                        FactoryBean<?> factory = (FactoryBean<?>) bean;
                        // 是否立即建立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 {
                    // 非 beanFactory 的,則馬上建立
                    getBean(beanName);
                }
            }
        }
        
        // Trigger post-initialization callback for all applicable beans...
        for (String beanName : beanNames) {
            // 獲取單例物件,如果這個物件是 SmartInitializingSingleton 介面的實現類,
            // 則呼叫 afterSingletonsInstantiated() 方法
            Object singletonInstance = getSingleton(beanName);
            if (singletonInstance instanceof SmartInitializingSingleton) {
                SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
                if (System.getSecurityManager() != null) {
                    AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
                        smartSingleton.afterSingletonsInstantiated();
                        return null;
                    }, getAccessControlContext());
                }
                else {
                    smartSingleton.afterSingletonsInstantiated();
                }
            }
        }
    }
2.5.12 最後一步重新整理完成
    /**
     * AbstractApplicationContext
     */
    protected void finishRefresh() {
        // 清除上下文中的資源快取
        // Clear context-level resource caches (such as ASM metadata from scanning).
        clearResourceCaches();

        // 註冊 LifecycleProcessor 介面實現類,預設使用 DefaultLifecycleProcessor
        // Initialize lifecycle processor for this context.
        initLifecycleProcessor();

        // 觸發剛剛註冊的 LifecycleProcessor 介面實現類的 onRefresh 事件
        // Propagate refresh to lifecycle processor first.
        getLifecycleProcessor().onRefresh();

        // 釋出上下文重新整理完成事件
        // Publish the final event.
        publishEvent(new ContextRefreshedEvent(this));

        // 想bean檢視註冊上下文物件。
        // Participate in LiveBeansView MBean, if active.
        LiveBeansView.registerApplicationContext(this);
    }

總結

spring boot 啟動流程主要經過初始化和載入監聽器、準備應用程式環境(配置資訊)、建立容器上下文、重新整理容器上下文,其中最關鍵的是重新整理上下文 refresh。在重新整理上下文中,載入 BeanFactoryPostProcessor,使用 BeanFactoryPostProcessor載入程式中需要注入的BeanDefinition,準備事件廣播器,最後建立單例並且不是懶載入的bean。

spring提供的後置處理器執行順序:

  1. BeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistry(BeanDefinitionRegistry)

    動態註冊、修改與刪除 BeanDefinition

  2. BeanFactoryPostProcessor#postProcessBeanFactory(ConfigurableListableBeanFactory)

    新增或修改 BeanDefinition 的屬性

  3. doCreateBean

    建立bean

  4. 對bean注入依賴
  5. BeanPostProcessor#postProcessBeforeInitialization()

    建立 bean 之後,在對bean 進行注入依賴物件後,呼叫這個 bean 後置處理器。

  6. InitializingBean#afterPropertiesSet

    spring提供的 bean 進行初始化介面

  7. BeanPostProcessor#postProcessAfterInitialization()

    bean初始化完成後執行。

    如果是 InitializingBean的實現類,則在afterPropertiesSet 方法執行後執行,否則在BeanPostProcessor#postProcessBeforeInitialization()之後執行。

相關文章