【源碼分析】Springboot啟動流程源碼分析-創(chuàng)新互聯(lián)

1.簡介

springboot版本:2.7.2

城關(guān)網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),城關(guān)網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為城關(guān)1000多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請找那個售后服務(wù)好的城關(guān)做網(wǎng)站的公司定做!

SpringApplication類是用來執(zhí)行Spring框架啟動的引導(dǎo)類。
有兩種方式可以進(jìn)行啟動引導(dǎo):

  • 通過靜態(tài)方法SpringApplication.run啟動。
  • 先創(chuàng)建SpringApplication實例,在調(diào)用的實例方法run進(jìn)行啟動。
    無論是以上哪種方式,最終都是通過創(chuàng)建SpringApplication實例,在調(diào)用run()啟動。
public static void main(String[] args) {SpringApplication.run(StateMachineApplication.class, args);
    }
2.源碼總結(jié)分析

類:org.springframework.boot.SpringApplication

run(String… args) 方法分析

主要方法:下面

public ConfigurableApplicationContext run(String... args) {long startTime = System.nanoTime();
        //初始化啟動上下文
        DefaultBootstrapContext bootstrapContext = this.createBootstrapContext();
        ConfigurableApplicationContext context = null;
        //配置Headless模式:Headless模式是在環(huán)境缺少顯示器等設(shè)備情況下的一種配置,和啟動流程并無太多關(guān)系
        this.configureHeadlessProperty();
        //listeners 監(jiān)聽器可以用來監(jiān)聽SpringApplication啟動過程中的各個階段。
        //默認(rèn)的監(jiān)聽器是EventPublishRunListener,用戶也可以通過實現(xiàn)SpringApplicationRunListener接口,實現(xiàn)應(yīng)用程序?qū)pringApplication啟動過程的監(jiān)聽。
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
        listeners.starting(bootstrapContext, this.mainApplicationClass);
        try {//準(zhǔn)備環(huán)境,創(chuàng)建ConfigurableEnvironment對象
        //SpringApplication會創(chuàng)建Spring啟動所需的環(huán)境,這個環(huán)境主要由ConfigurableEnviroment對象表示。首先,該對象確認(rèn)了程序是否需要設(shè)置Web環(huán)境,其次,該對象還確定了程序所需要的參數(shù)和讀取的配置文件等信息。此步驟會回調(diào)SpringApplicationRunListener的environmentPrepared()方法,通知監(jiān)聽器環(huán)境已經(jīng)準(zhǔn)備好。
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, bootstrapContext, applicationArguments);
            //決定是否跳過 BeanInfo 類的掃描
            this.configureIgnoreBeanInfo(environment);
            //只是會向控制臺或是日志中輸出Spring的Logo和版本信息
            Banner printedBanner = this.printBanner(environment);
            //創(chuàng)建應(yīng)用程序上下文并加載Bean
            //擴(kuò)展:在準(zhǔn)備好環(huán)境之后,接下來要做的就是創(chuàng)建應(yīng)用程序上下文ApplicationContext對象。ApplicationContext是Spring IoC的核心組件,它不僅是程序所需Bean的容器,還提供了國際化,事件發(fā)布等功能。
            //在創(chuàng)建應(yīng)用程序上下文的時候,首先會根據(jù)之前配置決定上下文的具體類型(AnnotationConfigApplicationContext或是AnnotationConfigServletWebServerApplicationContext)。再通過反射實例化到對象。
            context = this.createApplicationContext();
            //為此應(yīng)用程序上下文設(shè)置ApplicationStartup。
            context.setApplicationStartup(this.applicationStartup);
            //準(zhǔn)備ApplicationContext
            //擴(kuò)展:
            //雖然已經(jīng)得到了ApplicationContext對象,但此時的對象還只是一個空白對象,需要準(zhǔn)備和處理后,ApplicationContext才能被使用。
            //在準(zhǔn)備過程中主要做了做了幾件事:為ApplicationContext設(shè)置之前準(zhǔn)備好的Environment對象。
			//通過對ApplicationContext后置處理或是BeanDefinitionLoader等方式往容器中添加一些初始的Bean。
			//應(yīng)用默認(rèn)的初始化器初始化應(yīng)用程序上下文(責(zé)任鏈模式的應(yīng)用,多個初始化器形成一個List,應(yīng)用程序需要被每個初始化器應(yīng)用一次,每個初始化器有自己的職責(zé))。
			//準(zhǔn)備過程中ApplicationRunListener發(fā)出兩個消息,分別是contextPrepared和contextLoaded。
            this.prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
            //刷新上下文
            //擴(kuò)展:(下面在去解析這個方法,比較復(fù)雜)
            //通過刷新應(yīng)用程序上下文發(fā)現(xiàn)Bean并加載到容器中。refreshContext()會調(diào)用ApplicationContext.refresh()方法。
			//AbstractApplicationContext中定義了refresh()方法的基本框架(模板模式的應(yīng)用)。
            this.refreshContext(context);
            //在Application完成刷新后,SpringApplication給子類留了afterRefresh()的方法作為回調(diào)。
            this.afterRefresh(context, applicationArguments);
            //啟動完成后,stopWatch會記錄下本次啟動消費的時間。
            Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);
            if (this.logStartupInfo) {(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), timeTakenToStartup);
            }
            //然后向ApplicationRunListener發(fā)布started事件,說明已經(jīng)啟動就緒
            listeners.started(context, timeTakenToStartup);
            //啟動完成后,正式運行前,SpringApplication還會執(zhí)行用戶定義的ApplicationRunner和CommandLineRunner兩個接口中定義的run()方法。
            this.callRunners(context, applicationArguments);
        } catch (Throwable var12) {this.handleRunFailure(context, var12, listeners);
            throw new IllegalStateException(var12);
        }

        try {//在執(zhí)行完成后,向ApplicationRunListener發(fā)布runing的消息。至此,啟動流程結(jié)束。
            Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);
            listeners.ready(context, timeTakenToReady);
            return context;
        } catch (Throwable var11) {this.handleRunFailure(context, var11, (SpringApplicationRunListeners)null);
            throw new IllegalStateException(var11);
        }
    }
this.refreshContext(context);分析

類:org\springframework\context\support\AbstractApplicationContext.java

@Override
	public void refresh() throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {	StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");

			//準(zhǔn)備刷新
			//準(zhǔn)備刷新的階段做了初始化和校驗的工作。
			prepareRefresh();

			// 告訴子類刷新內(nèi)部bean工廠
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.(準(zhǔn)備用于此上下文的bean工廠。)
			//準(zhǔn)備BeanFactory  
			//擴(kuò)展:
			//對BeanFactory的準(zhǔn)備主要是:添加一些必要組件,比如類加載器,表達(dá)式解析器,屬性編輯器注冊表等。
			//以及一些后置處理器,比如ApplicationContextAwareProcessor(xxxAware的接口就是通過后置處理器在Bean創(chuàng)建的時候,通過后置處理器設(shè)置的)。此外還有一些特殊的Bean,environment,systemProperties和systemEnvirnoment。
			prepareBeanFactory(beanFactory);

			try {		// (后置處理BeanFactory)Allows post-processing of the bean factory in context subclasses.
				//擴(kuò)展:
				//對于非WebServlet環(huán)境的ApplicationContext而言這個方法是個空方法,但是Web環(huán)境下的ApplicationContext會通過這個方法定制一些后處理動作,比如添加WebApplicationContextServletAwareProcessor后置處理器,添加在web環(huán)境中可能使用的Scope(session和request)。
				postProcessBeanFactory(beanFactory);

				StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
				//(在上下文中調(diào)用注冊為bean的工廠處理器。) Invoke factory processors registered as beans in the context.
				//其實就是實例化并調(diào)用BeanFactoryPostProcessor
				//擴(kuò)展:(看標(biāo)題有詳細(xì)解釋)
				//BeanFactoryPostProcessor是一種特殊的后置處理器,其操作的對象是針對BeanFactory。
				invokeBeanFactoryPostProcessors(beanFactory);

				// (注冊Bean后置處理器)Register bean processors that intercept bean creation.\
				//擴(kuò)展:
				//上一步是針對BeanFactory和BeanDefinitionRegistry的后置處理器,這一步從BeanFactory中獲取針對普通Bean的后置處理器BeanFactoryPostProcessor放到專門的容器beanPostProcessors中。
				registerBeanPostProcessors(beanFactory);
				beanPostProcess.end();

				//(初始化MessageSource處理國際化相關(guān)內(nèi)容) Initialize message source for this context.
				initMessageSource();

				//(初始化ApplicationEventMulticaster) Initialize event multicaster for this context.
				//擴(kuò)展:
				//ApplicationEventMulticaster是ApplicationEvent廣播器,可以通過這個對象向容器中添加移除Listener,也可以通過這個對象發(fā)布事件(觀察者模式的應(yīng)用)。
				initApplicationEventMulticaster();

				// (刷新應(yīng)用程序)Initialize other special beans in specific context subclasses.
				// 擴(kuò)展:
				//發(fā)現(xiàn)了所有的Bean,并且需要實例化的Bean也都被創(chuàng)建好了之后,Spring接下去要做的是創(chuàng)建ThemeSource(和主題相關(guān)的組件),以及創(chuàng)建Webserver如果存在。
				//此方法可以重寫以添加特定于上下文的刷新工作的模板方法。在實例化單體之前,在初始化特殊bean時調(diào)用。
				onRefresh();

				//注冊監(jiān)聽器 Check for listener beans and register them.
				//擴(kuò)展:
				//這一步會將初始化得到的ApplicationListener方法和容器中獲得ApplicationListener一起注冊到ApplicationEventMulticaster中,并且如果存在需要早起發(fā)布的事件,則發(fā)布事件。
				registerListeners();

				// (初始化容器中的Bean)Instantiate all remaining (non-lazy-init) singletons.
				//擴(kuò)展:
				//經(jīng)過之前的步驟,現(xiàn)在容器中必要的組件都已經(jīng)準(zhǔn)備好了,并且所有需要容器管理的Bean也都已經(jīng)被發(fā)現(xiàn)注冊成BeanDefinition注冊表中。
				//對于Scope是Singleton的Bean而言,此時已經(jīng)具備了實例化Bean的條件,因此在這一步中,Spring會對所有Singleton且非lazy-init的Bean進(jìn)行實例化。
				//主要做法就是獲取容器中所有為singletion且非lazyInit的BeanDefinition,然后通過getBean創(chuàng)建出Bean的實例,保存在容器內(nèi)部。
				finishBeanFactoryInitialization(beanFactory);

				//完成刷新 Last step: publish corresponding event.
				//主要是一些資源清理以及注冊LifeCycleProcessor。LifeCycleProcessor可以用來在 Spring 生命周期的refresh和close時觸發(fā)回調(diào)。并且發(fā)布Refresh的消息。ContextRefreshedEvent
				finishRefresh();
			}

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

				//銷毀已創(chuàng)建的單實例以避免懸空資源。Destroy already created singletons to avoid dangling resources.
				destroyBeans();

				// 重置“激活”標(biāo)志。 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();
				contextRefresh.end();
			}
		}
	}
BeanFactory 詳解

BeanFactory是 Spring 框架中容器的底層實現(xiàn),所有的 Bean 都存放在BeanFactory中,雖然ApplicationContext也實現(xiàn)了BeanFactory接口,但是在其內(nèi)部還是將獲取 Bean 的相關(guān)操作委托給內(nèi)部的DefaultListableBeanFactory變量,只是ApplicationContext幫用戶屏蔽了底層的操作,同時提供出一些更符合外部用戶使用的接口。

BeanFactoryPostProcessor 詳解

BeanFactoryPostProcessor 主要有三個后置處理器,分是:

1、SharedMetadataReaderFactoryContextInitializer$CachingMetadataReaderFactoryPostProcessor

2、ConfigurationWarningsApplicationContextInitializer$ConfigurationWarningsPostProcessor

3、ConfigFileApplicationListener$PropertySourceOrderingPostProcessor。

一般主要是這倆:
在這里插入圖片描述
我們看名字也能發(fā)現(xiàn)類是怎么來的(外部類是xxxInitializer的就說明是初始化器設(shè)置的)。

其中第一個類和啟動流程有關(guān),因為它會向容器注冊ConfigurationClassPostProcessor。

如果BeanFactoryPostProcessor同時又是BeanDefinitionRegistryPostProcessor,則先進(jìn)行針對BeanDefinition注冊表的后置處理,目的是為了發(fā)現(xiàn)Bean。

在最初的三個BeanFactoryProcessor后置處理完成后,會從容器中獲取BeanDefinitionRegistryPostProcessor類型的后置處理器(這里主要會得到剛才加載的ConfigurationClassPostProcessor實例)。再調(diào)用這些BeanDefinitionRegistry的后置處理器,繼續(xù)向發(fā)現(xiàn)并向容器中注冊新的Bean。

這里主要是通過@Configuration注解作為入口發(fā)現(xiàn)Bean,如果發(fā)現(xiàn)的Bean中又存在新的@ConfigurationBean,則以此Bean為入口再進(jìn)行發(fā)現(xiàn),直到所有的Bean都被發(fā)現(xiàn)。

注意 Bean的發(fā)現(xiàn)過程只是向BeanDefinition注冊表注冊BeanDefinition的過程,并沒有針對發(fā)現(xiàn)的Bean進(jìn)行實例化(少部分需要用到的Bean會進(jìn)行實例化,比如這部分會對BeanDefinitionRegistryPostProcessor類型的Bean實例化)。

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

本文標(biāo)題:【源碼分析】Springboot啟動流程源碼分析-創(chuàng)新互聯(lián)
文章鏈接:http://bm7419.com/article32/dscisc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、品牌網(wǎng)站建設(shè)定制開發(fā)、外貿(mào)建站建站公司、Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司