<code>作者:木木匠
來源:https://my.oschina.net/luozhou/blog/3088908/<code>
前言
我們知道SpringBoot給我們帶來了一個全新的開發體驗,我們可以直接把web程序達成jar包,直接啟動,這就得益於SpringBoot內置了容器,可以直接啟動,本文將以Tomcat為例,來看看SpringBoot是如何啟動Tomcat的,同時也將展開學習下Tomcat的源碼,瞭解Tomcat的設計。
從 Main 方法說起
用過SpringBoot的人都知道,首先要寫一個main方法來啟動
<code>@SpringBootApplication
public class TomcatdebugApplication {
public static void main(String[] args) {
SpringApplication.run(TomcatdebugApplication.class, args);
}
}/<code>
我們直接點擊run方法的源碼,跟蹤下來,發下最終 的run方法是調用ConfigurableApplicationContext方法,源碼如下:
其實這個方法我們可以簡單的總結下步驟為 > 1. 配置屬性 > 2. 獲取監聽器,發佈應用開始啟動事件 > 3. 初始化輸入參數 > 4. 配置環境,輸出 banner > 5. 創建上下文 > 6. 預處理上下文 > 7. 刷新上下文 > 8. 再刷新上下文 > 9. 發佈應用已經啟動事件 > 10. 發佈應用啟動完成事件
其實上面這段代碼,如果只要分析 tomcat 內容的話,只需要關注兩個內容即可,上下文是如何創建的,上下文是如何刷新的,分別對應的方法就是 createApplicationContext() 和 refreshContext(context),接下來我們來看看這兩個方法做了什麼。
這裡就是根據我們的 webApplicationType 來判斷創建哪種類型的 Servlet,代碼中分別對應著 Web 類型(SERVLET),響應式 Web 類型(REACTIVE),非 Web 類型(default),我們建立的是 Web 類型,所以肯定實例化 DEFAULT_SERVLET_WEB_CONTEXT_CLASS 指定的類,也就是 AnnotationConfigServletWebServerApplicationContext 類
我們來用圖來說明下這個類的關係
通過這個類圖我們可以知道,這個類繼承的是 ServletWebServerApplicationContext,這就是我們真正的主角,而這個類最終是繼承了 AbstractApplicationContext,瞭解完創建上下文的情況後,我們再來看看刷新上下文,相關代碼如下:
<code>//類:SpringApplication.java
private void refreshContext(ConfigurableApplicationContext context) {
//直接調用刷新方法
refresh(context);
if (this.registerShutdownHook) {
try {
context.registerShutdownHook();
}
catch (AccessControlException ex) {
// Not allowed in some environments.
}
}
}
//類:SpringApplication.java
protected void refresh(ApplicationContext applicationContext) {
Assert.isInstanceOf(AbstractApplicationContext.class, applicationContext);
((AbstractApplicationContext) applicationContext).refresh();
}/<code>
這裡還是直接傳遞調用本類的 refresh(context)方法,最後是強轉成父類 AbstractApplicationContext 調用其 refresh()方法,該代碼如下:
這裡我們看到 onRefresh()方法是調用其子類的實現,根據我們上文的分析,我們這裡的子類是 ServletWebServerApplicationContext。
到這裡,其實廬山真面目已經出來了,createWebServer()就是啟動 web 服務,但是還沒有真正啟動 Tomcat,既然 webServer 是通過 ServletWebServerFactory 來獲取的,我們就來看看這個工廠的真面目。
走進Tomcat內部
根據上圖我們發現,工廠類是一個接口,各個具體服務的實現是由各個子類來實現的,所以我們就去看看TomcatServletWebServerFactory.getWebServer()的實現。
根據上面的代碼,我們發現其主要做了兩件事情,第一件事就是把 Connnctor(我們稱之為連接器)對象添加到 Tomcat 中,第二件事就是 configureEngine,這連接器我們勉強能理解(不理解後面會述說),那這個 Engine 是什麼呢?
我們查看 tomcat.getEngine()的源碼:
根據上面的源碼,我們發現,原來這個 Engine 是容器,我們繼續跟蹤源碼,找到 Container 接口
上圖中,我們看到了 4 個子接口,分別是 Engine,Host,Context,Wrapper。我們從繼承關係上可以知道他們都是容器
那麼他們到底有啥區別呢?我看看他們的註釋是怎麼說的。
<code> /**
If used, an Engine is always the top level Container in a Catalina
* hierarchy. Therefore, the implementation's <code>setParent()/<code> method
* should throw <code>IllegalArgumentException/<code>.
*
* @author Craig R. McClanahan
*/
public interface Engine extends Container {
//省略代碼
}
/**
*
* The parent Container attached to a Host is generally an Engine, but may
* be some other implementation, or may be omitted if it is not necessary.
*
* The child containers attached to a Host are generally implementations
* of Context (representing an individual servlet context).
*
* @author Craig R. McClanahan
*/
public interface Host extends Container {
//省略代碼
}
/***
* The parent Container attached to a Context is generally a Host, but may
* be some other implementation, or may be omitted if it is not necessary.
*
* The child containers attached to a Context are generally implementations
* of Wrapper (representing individual servlet definitions).
*
*
* @author Craig R. McClanahan
*/
public interface Context extends Container, ContextBind {
//省略代碼
}
/**
* The parent Container attached to a Wrapper will generally be an
* implementation of Context, representing the servlet context (and
* therefore the web application) within which this servlet executes.
*/<code>
* Child Containers are not allowed on Wrapper implementations, so the
* <code>addChild()/<code> method should throw an
* <code>IllegalArgumentException/<code>.
*
* @author Craig R. McClanahan
*/
public interface Wrapper extends Container {
//省略代碼
}
上面的註釋翻譯過來就是,Engine是最高級別的容器,其子容器是Host,Host的子容器是Context,Wrapper是Context的子容器,所以這4個容器的關係就是父子關係,也就是Engine>Host>Context>Wrapper。
我們再看看Tomcat類的源碼:
閱讀Tomcat的getServer()我們可以知道,Tomcat的最頂層是Server,Server就是Tomcat的實例,一個Tomcat一個Server;通過getEngine()我們可以瞭解到Server下面是Service,而且是多個,一個Service代表我們部署的一個應用,而且我們還可以知道,Engine容器,一個service只有一個;根據父子關係,我們看setHost()源碼可以知道,host容器有多個;同理,我們發現addContext()源碼下,Context也是多個;addServlet()表明Wrapper容器也是多個,而且這段代碼也暗示了,其實Wrapper和Servlet是一層意思。另外我們根據setConnector源碼可以知道,連接器(Connector)是設置在service下的,而且是可以設置多個連接器(Connector)。
根據上面分析,我們可以小結下:
Tomcat主要包含了2個核心組件,連接器(Connector)和容器(Container),用圖表示如下:
一個Tomcat是一個Server,一個Server下有多個service,也就是我們部署的多個應用,一個應用下有多個連接器(Connector)和一個容器(Container),容器下有多個子容器,關係用圖表示如下:
Engine下有多個Host子容器,Host下有多個Context子容器,Context下有多個Wrapper子容器。
總結
SpringBoot的啟動是通過new SpringApplication()實例來啟動的,啟動過程主要做如下幾件事情:
- 配置屬性
- 獲取監聽器,發佈應用開始啟動事件
- 初始化輸入參數
- 配置環境,輸出banner
- 創建上下文
- 預處理上下文
- 刷新上下文
- 再刷新上下文
- 發佈應用已經啟動事件
- 發佈應用啟動完成事件
而啟動Tomcat就是在第7步中“刷新上下文”;Tomcat的啟動主要是初始化2個核心組件,連接器(Connector)和容器(Container),一個Tomcat實例就是一個Server,一個Server包含多個Service,也就是多個應用程序,每個Service包含多個連接器(Connetor)和一個容器(Container),而容器下又有多個子容器,按照父子關係分別為:Engine,Host,Context,Wrapper,其中除了Engine外,其餘的容器都是可以有多個。
閱讀更多 程序員BUG 的文章