日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springboot-嵌入式Servlet容器(Tomcat)源码分析以及容器切换

發布時間:2025/3/19 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot-嵌入式Servlet容器(Tomcat)源码分析以及容器切换 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

一、springboot的嵌入式Servlet容器(tomcat)

1.原理

2.源碼

(1)ServletWebServerApplicationContext的createWebServer方法

(2)獲取tomcat的webServer

(3)tomcat的啟動方法

二、切換嵌入式Servlet容器

1.排除tomcat,添加undertow

2.啟動時就會使用undertow(還是建議使用tomcat)


一、springboot的嵌入式Servlet容器(tomcat)

1.原理

(1)SpringBoot應用啟動發現當前是Web應用。(web場景包starter會導入tomcat)

(2)web應用會創建一個web版的ioc容器 ServletWebServerApplicationContext。

(3)ServletWebServerApplicationContext??啟動的時候尋找 ServletWebServerFactory(Servlet 的web服務器工廠--->?生產Servlet 的web服務器)。

(4)SpringBoot底層默認有很多的WebServer工廠;TomcatServletWebServerFactory, JettyServletWebServerFactory, UndertowServletWebServerFactory。

(5)SpringBoot底層直接會有一個自動配置類。ServletWebServerFactoryAutoConfiguration

(6)ServletWebServerFactoryAutoConfiguration導入了ServletWebServerFactoryConfiguration(配置類)

(7)ServletWebServerFactoryConfiguration 配置類 根據動態判斷系統中到底導入了那個Web服務器的包。(默認是web-starter導入tomcat包),容器中就有TomcatServletWebServerFactory。

(8)TomcatServletWebServerFactory 創建出Tomcat服務器并啟動;TomcatWebServer 的構造器擁有初始化方法initialize---會調用this.tomcat.start();

(9)內嵌服務器,就是手動把啟動服務器的代碼調用(前提是tomcat核心jar包存在)

2.源碼

(1)ServletWebServerApplicationContext的createWebServer方法

// org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext#createWebServer private void createWebServer() {WebServer webServer = this.webServer; ServletContext servletContext = getServletContext();if (webServer == null && servletContext == null) {// 獲取ServletWebServerFactory,默認是tomcat的ServletWebServerFactory factory = getWebServerFactory();// 通過ServletWebServerFactory創建webServerthis.webServer = factory.getWebServer(getSelfInitializer());getBeanFactory().registerSingleton("webServerGracefulShutdown",new WebServerGracefulShutdownLifecycle(this.webServer));getBeanFactory().registerSingleton("webServerStartStop",new WebServerStartStopLifecycle(this, this.webServer));}else if (servletContext != null) {try {getSelfInitializer().onStartup(servletContext);}catch (ServletException ex) {throw new ApplicationContextException("Cannot initialize servlet context", ex);}}// 啟動tomcatinitPropertySources(); }

(2)獲取tomcat的webServer

// org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory#getWebServer @Override public WebServer getWebServer(ServletContextInitializer... initializers) {if (this.disableMBeanRegistry) {Registry.disableRegistry();}Tomcat tomcat = new Tomcat();File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat");tomcat.setBaseDir(baseDir.getAbsolutePath());Connector connector = new Connector(this.protocol);connector.setThrowOnFailure(true);tomcat.getService().addConnector(connector);customizeConnector(connector);tomcat.setConnector(connector);tomcat.getHost().setAutoDeploy(false);configureEngine(tomcat.getEngine());for (Connector additionalConnector : this.additionalTomcatConnectors) {tomcat.getService().addConnector(additionalConnector);}prepareContext(tomcat.getHost(), initializers);return getTomcatWebServer(tomcat); }

(3)tomcat的啟動方法

tomcat初始的時候就會調用start。

// org.springframework.boot.web.embedded.tomcat.TomcatWebServer#start @Override public void start() throws WebServerException {synchronized (this.monitor) {if (this.started) {return;}try {addPreviouslyRemovedConnectors();Connector connector = this.tomcat.getConnector();if (connector != null && this.autoStart) {performDeferredLoadOnStartup();}checkThatConnectorsHaveStarted();this.started = true;logger.info("Tomcat started on port(s): " + getPortsDescription(true) + " with context path '"+ getContextPath() + "'");}catch (ConnectorStartFailedException ex) {stopSilently();throw ex;}catch (Exception ex) {PortInUseException.throwIfPortBindingException(ex, () -> this.tomcat.getConnector().getPort());throw new WebServerException("Unable to start embedded Tomcat server", ex);}finally {Context context = findContext();ContextBindings.unbindClassLoader(context, context.getNamingToken(), getClass().getClassLoader());}} }

二、切換嵌入式Servlet容器

1.排除tomcat,添加undertow

<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>

2.啟動時就會使用undertow(還是建議使用tomcat)

?

總結

以上是生活随笔為你收集整理的springboot-嵌入式Servlet容器(Tomcat)源码分析以及容器切换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。