天天看點

springboot加載過程_SpringBoot怎麼完成的main啟動?

前言-作者寫文章的目的:

(1)讓不知道的人了解。

(2)讓知道的人可以熟悉一下。

(3)讓精通的人可以互相探讨。

(4)可以探讨,不要擡杠(沒有人能從擡杠中獲益)。

springboot加載過程_SpringBoot怎麼完成的main啟動?

springboot

有可能有些人用了很久的springboot,卻仍然不太了解,springboot如何實作的main啟動,如何實作的加載容器。此文章主要介紹springboot的啟動過程及容器的加載過程。

一、目錄

1、springboot的main啟動過程。

2、springboot加載容器的過程(tomcat/jetty)。

二、詳解

2.1、springboot的main啟動過程。

springboot加載過程_SpringBoot怎麼完成的main啟動?

springboot

說明:

(1)、啟動調用SpringApplication.run()。

(2)、聲明ConfigurableApplicationContext對象。

(3)、建立run監聽器SpringApplicationRunListeners。

(4)、建立ApplicationEvent事件。

(5)、擷取啟動參數及環境變量配配及Banner。

(6)、ConfigurableApplicationContext執行個體化createApplicationContext。

(7)、預準備ConfigurableApplicationContext。

(8)、重新整理加載到ContextrefreshContext。

(9)、釋出ApplicationEvent事件。

(10)、預啟動SpingApplication.callRunners。

(11)、啟動listeners.running(context);

代碼:

public ConfigurableApplicationContext run(String... args) {        StopWatch stopWatch = new StopWatch();        stopWatch.start();        ConfigurableApplicationContext context = null;        Collection exceptionReporters = new ArrayList();        this.configureHeadlessProperty();// 1、建立SpringRunListener        SpringApplicationRunListeners listeners = this.getRunListeners(args);        listeners.starting();        Collection exceptionReporters;        try {// 2、擷取啟動參數            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);// 3、預加載環境變量 注意這個步驟            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);            this.configureIgnoreBeanInfo(environment);// 4、加載自定義Banner            Banner printedBanner = this.printBanner(environment);// 5、建立ConfigurableApplicationContext對象            context = this.createApplicationContext();            exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);//6、預準備ConfigurableApplicationContext(加載啟動資訊到ConfigurableListableBeanFactory工廠)            this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);            this.refreshContext(context);            this.afterRefresh(context, applicationArguments);            stopWatch.stop();            if (this.logStartupInfo) {                (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);            }            listeners.started(context);            this.callRunners(context, applicationArguments);        } catch (Throwable var10) {            this.handleRunFailure(context, var10, exceptionReporters, listeners);            throw new IllegalStateException(var10);        }        try {            listeners.running(context);            return context;        } catch (Throwable var9) {            this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);            throw new IllegalStateException(var9);        }    }           

2.2、Springboot加載裝配Tomcat

2.2.1選擇容器對象

springboot加載過程_SpringBoot怎麼完成的main啟動?

選擇啟動容器

上圖實在SpringApplication.run()的示例化AppllicationContext對象時(第六步)建立容器。

2.2.2選擇容器方法

springboot加載過程_SpringBoot怎麼完成的main啟動?

選擇啟動容器的實作

選擇使用容器是SERVLET 還是REACTIVE。或者預設的AnnotationConfigServletWebServerApplicationContext

這個類內建自,ServletWebServerApplicationContext 最終為AbstractApplicationContext。

springboot加載過程_SpringBoot怎麼完成的main啟動?

繼承關系

2.2.3 AbstractApplicationContext.refresh()方法中的 onRefresh()才真正的建立了WebServer

springboot加載過程_SpringBoot怎麼完成的main啟動?

onRefresh-createWebServer

2.2.4建立WebServer

springboot加載過程_SpringBoot怎麼完成的main啟動?

createServer

2.2.5webServer實作

springboot加載過程_SpringBoot怎麼完成的main啟動?

webServerImpl

到此完成springboot對tomcat的內建。

而tomcat的參數配置會在加載配置檔案和環境變量時裝載到webServer中。主要方法:

springboot加載過程_SpringBoot怎麼完成的main啟動?

tomcat詳解

繼續閱讀