1. 程式人生 > 其它 >SpringBoot內建tomcat啟動過程及原理

SpringBoot內建tomcat啟動過程及原理

作者:李巖科

1 背景

SpringBoot 是一個框架,一種全新的程式設計規範,他的產生簡化了框架的使用,同時也提供了很多便捷的功能,比如內建 tomcat 就是其中一項,他讓我們省去了搭建 tomcat 容器,生成 war,部署,啟動 tomcat。因為內建了啟動容器,應用程式可以直接通過 Maven 命令將專案編譯成可執行的 jar 包,通過 java -jar 命令直接啟動,不需要再像以前一樣,打包成 War 包,然後部署在 Tomcat 中。那麼內建 tomcat 是如何實現的呢

2 tomcat 啟動過程及原理

2.1 下載一個 springboot 專案

在這裡下載一個專案 https://start.spring.io/

 也可以在 idea 新建 SpringBoot-Web 工程.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

點選 pom.xml 會有 tomcat 依賴

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>

2.2 從啟動入口開始一步步探索

點選進入 run 方法

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

//繼續點選進入run方法
public static ConfigurableApplicationContext run(Class<?>[] primarySources,
String[] args) {
return new SpringApplication(primarySources).run(args);
}

進入到這個 run 方法之後就可以看到,我們認識的一些初始化事件。主要的過程也是在這裡完成的。

2.3 原始碼程式碼流程大致是這樣

/**
* Run the Spring application, creating and refreshing a new
* {@link ApplicationContext}.
* @param args the application arguments (usually passed from a Java main method)
* @return a running {@link ApplicationContext}
*/
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
/**1、配置系統屬性*/
configureHeadlessProperty();
/**2.獲取監聽器*/
SpringApplicationRunListeners listeners = getRunListeners(args);
/**釋出應用開始啟動事件 */
listeners.starting();
try {
/** 3.初始化引數 */
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
/** 4.配置環境*/
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);

configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
/**5.建立應用上下文*/
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(
SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
/**6.預處理上下文*/
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);

/**6.重新整理上下文*/
refreshContext(context);
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
/** 8.釋出應用已經啟動事件 */
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}

try {
/** 9.釋出應用已經啟動完成的監聽事件 */
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}

程式碼中主要就是通過 switch 語句,根據 webApplicationType 的型別來建立不同的 ApplicationContext:

  • DEFAULT_SERVLET_WEB_CONTEXT_CLASS:Web 型別,例項化 AnnotationConfigServletWebServerApplicationContext
  • DEFAULT_REACTIVE_WEB_CONTEXT_CLASS:響應式 Web 型別,例項化 AnnotationConfigReactiveWebServerApplicationContext
  • DEFAULT_CONTEXT_CLASS:非 Web 型別,例項化 AnnotationConfigApplicationContext

2.4 建立完應用上下文之後,我們在看重新整理上下文方法

一步步通過斷點點選方法進去檢視,我們看到很熟悉程式碼 spring 的相關程式碼。

@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
//初始化前的準備工作,主要是一些系統屬性、環境變數的校驗,比如Spring啟動需要某些環境變數,可以在這個地方進行設定和校驗
prepareRefresh();

// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

// Prepare the bean factory for use in this context.
//準備bean工廠 註冊了部分類
prepareBeanFactory(beanFactory);

try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);

// Invoke factory processors registered as beans in the context.
//註冊bean工廠後置處理器,並解析java程式碼配置bean定義
invokeBeanFactoryPostProcessors(beanFactory);

// Register bean processors that intercept bean creation.
//註冊bean後置處理器,並不會執行後置處理器,在後面例項化的時候執行
registerBeanPostProcessors(beanFactory);

// Initialize message source for this context.
initMessageSource();

// Initialize event multicaster for this context.
//初始化事件監聽多路廣播器
initApplicationEventMulticaster();

// Initialize other special beans in specific context subclasses.
//待子類實現,springBoot在這裡實現建立內建的tomact容器
onRefresh();

// Check for listener beans and register them.
registerListeners();

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

// Last step: publish corresponding event.
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.5 onRefresh () 方法是呼叫其子類實現的

也就是 ServletWebServerApplicationContext

/** 得到Servlet工廠 **/
this.webServer = factory.getWebServer(getSelfInitializer());

其中 createWebServer () 方法是用來啟動 web 服務的,但是還沒有真正啟動 Tomcat,只是通過 ServletWebServerFactory 建立了一個 WebServer,繼續來看這個 ServletWebServerFactory:

this.webServer = factory.getWebServer (getSelfInitializer ()); 這個方法可以看出 TomcatServletWebServerFactory 的實現。相關 Tomcat 的實現。

2.6 TomcatServletWebServerFactory 的 getWebServer () 方法

清晰的看到 new 出來了一個 Tomcat.

2.7 Tomcat 建立之後,繼續分析 Tomcat 的相關設定和引數

@Override
public WebServer getWebServer(ServletContextInitializer... initializers) {

/** 1、建立Tomcat例項 **/
Tomcat tomcat = new Tomcat();
//建立Tomcat工作目錄
File baseDir = (this.baseDirectory != null) ? this.baseDirectory
: createTempDir("tomcat");
tomcat.setBaseDir(baseDir.getAbsolutePath());
Connector connector = new Connector(this.protocol);
tomcat.getService().addConnector(connector);
customizeConnector(connector);
/** 2、給建立好的tomcat設定聯結器connector **/
tomcat.setConnector(connector);
/** 3.設定不自動部署 **/
tomcat.getHost().setAutoDeploy(false);
/** 4.配置Tomcat容器引擎 **/
configureEngine(tomcat.getEngine());
for (Connector additionalConnector : this.additionalTomcatConnectors) {
tomcat.getService().addConnector(additionalConnector);
}
/**準備Tomcat的StandardContext,並新增到Tomcat中*/
prepareContext(tomcat.getHost(), initializers);
/** 將建立好的Tomcat包裝成WebServer返回**/
return getTomcatWebServer(tomcat);
}

2.8 繼續點選 getTomcatWebServer 方法,找到 initialize () 方法,可以看到 tomcat.start (); 啟動 tomcat 服務方法。

// Start the server to trigger initialization listeners
//啟動tomcat服務
this.tomcat.start();
//開啟阻塞非守護程序
startDaemonAwaitThread();

//Tomcat.java

2.9 TomcatWebServer.java 控制檯會列印這句話

Tomcat started on port(s): 8080 (http) with context path ‘’

3 總結

SpringBoot 的啟動主要是通過例項化 SpringApplication 來啟動的,啟動過程主要做了如下幾件事情:

配置系統屬性、獲取監聽器,釋出應用開始啟動事件、初始化引數、配置環境、建立應用上下文、預處理上下文、重新整理上下文、再次重新整理上下文、釋出應用已經啟動事件、釋出應用啟動完成事件。而啟動 Tomcat 是重新整理上下文 這一步。

Spring Boot 建立 Tomcat 時,會先建立一個上下文,將 WebApplicationContext 傳給 Tomcat;

啟動 Web 容器,需要呼叫 getWebserver (),因為預設的 Web 環境就是 TomcatServletWebServerFactory,所以會建立 Tomcat 的 Webserver,這裡會把根上下文作為引數給 TomcatServletWebServerFactory 的 getWebServer ();啟動 Tomcat,呼叫 Tomcat 中 Host、Engine 的啟動方法。

3.1 Tomcat 相關名稱介紹