Springboot內(nèi)嵌tomcat應(yīng)用原理深入分析
springboot版本:2.2.9.RELEASE。
默認(rèn)Servlet容器
springboot默認(rèn)支持tomcat、jetty、undertow作為底層容器,
一旦引入spring-boot-starter-web模塊,就默認(rèn)使用tomcat。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
切換Servlet容器
排除tomcat依賴;
引入其它的容器。
例如:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency>
內(nèi)嵌tomcat自動配置原理
tomcat自動配置類
打開spring-boot-2.2.9.RELEASE\spring-boot-project\spring-boot-autoconfigure\src\main\resources\META-INF\spring.factories,
這個ServletWebServerFactoryAutoConfiguration就是tomcat的自動配置類,
打開這個類,
EmbeddedTomcat
tomcat工廠類
TomcatServletWebServerFactory,
TomcatServletWebServerFactory有一個getWebServer方法,
@Override public WebServer getWebServer(ServletContextInitializer... initializers) { if (this.disableMBeanRegistry) { Registry.disableRegistry(); } // 實例化一個tomcat Tomcat tomcat = new Tomcat(); File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat"); // 設(shè)置tomcat的臨時工作目錄 tomcat.setBaseDir(baseDir.getAbsolutePath()); // 默認(rèn)使用Http11NioProtocol實例化connector Connector connector = new Connector(this.protocol); connector.setThrowOnFailure(true); // 給service添加connector tomcat.getService().addConnector(connector); customizeConnector(connector); tomcat.setConnector(connector); // 關(guān)閉熱部署 tomcat.getHost().setAutoDeploy(false); // 配置engine configureEngine(tomcat.getEngine()); for (Connector additionalConnector : this.additionalTomcatConnectors) { tomcat.getService().addConnector(additionalConnector); } prepareContext(tomcat.getHost(), initializers); // 實例化TomcatWebServer時會將DispatcherServlet以及一些Filter添加到tomcat return getTomcatWebServer(tomcat); }
實例化tomcat,
TomcatServletWebServerFactory#getTomcatWebServer
TomcatWebServer#TomcatWebServer(org.apache.catalina.startup.Tomcat, boolean)
TomcatWebServer#initialize
到this.tomcat.start();這一步,tomcat就啟動了。
所以一旦TomcatServletWebServerFactory#getWebServer被調(diào)用,內(nèi)嵌的tomcat就會創(chuàng)建并啟動。
何時被調(diào)用
在刷新應(yīng)用上下文的時候,
SpringBootMytestApplication#main
→
SpringApplication#run(java.lang.Class<?>, java.lang.String...) → SpringApplication#run(java.lang.Class<?>[], java.lang.String[])
→
SpringApplication#run(java.lang.String…)
→
SpringApplication#refreshContext
→
SpringApplication#refresh
→
AbstractApplicationContext#refresh
AbstractApplicationContext#refresh
onRefresh()
我們關(guān)注下onRefresh();這個方法,看ServletWebServerApplicationContext的實現(xiàn),
ServletWebServerApplicationContext#onRefresh
@Override protected void onRefresh() { super.onRefresh(); try { // 通過Servlet容器工廠TomcatServletWebServerFactory,獲取Servlet容器tomcat createWebServer(); } catch (Throwable ex) { throw new ApplicationContextException("Unable to start web server", ex); } }
ServletWebServerApplicationContext#createWebServer
可以看到這個類型是TomcatServletWebServerFactory,所以就是在這一步調(diào)用的。
finishRefresh()
AbstractApplicationContext#refresh
→
AbstractApplicationContext#finishRefresh
→
ServletWebServerApplicationContext#finishRefresh
@Override protected void finishRefresh() { super.finishRefresh(); // 實例化(在tomcat啟動時就要完成實例化的Servlet:loadStartUp > 0),打印啟動完成日志。 WebServer webServer = startWebServer(); if (webServer != null) { publishEvent(new ServletWebServerInitializedEvent(webServer, this)); } }
到此這篇關(guān)于Springboot內(nèi)嵌tomcat應(yīng)用原理深入分析的文章就介紹到這了,更多相關(guān)Springboot內(nèi)嵌tomcat內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis 映射文件中if標(biāo)簽判斷字符串相等的兩種方式
這篇文章主要介紹了mybatis 映射文件中if標(biāo)簽判斷字符串相等的方式,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06Java ArrayDeque實現(xiàn)Stack的功能
這篇文章主要介紹了Java ArrayDeque實現(xiàn)Stack功能的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-03-03mybatis-plus使用@EnumValue處理枚舉類型的示例代碼
這篇文章主要介紹了mybatis-plus使用@EnumValue處理枚舉類型的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09解決feignclient調(diào)用服務(wù),傳遞的中文數(shù)據(jù)成???問題
這篇文章主要介紹了解決feignclient調(diào)用服務(wù),傳遞的中文數(shù)據(jù)成???問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01