Spring Boot 支持以下內(nèi)嵌的 Web 容器:
這些內(nèi)嵌的 Web 容器都可以在 Spring Boot 應(yīng)用中直接使用,無需額外的安裝和配置。Spring Boot 會自動根據(jù)項(xiàng)目的依賴和配置來選擇合適的 Web 容器,并進(jìn)行相應(yīng)的配置和啟動。
你可以根據(jù)項(xiàng)目的需求和特點(diǎn)選擇適合的 Web 容器。例如,如果對性能有較高要求,可以考慮使用 Undertow;如果需要與現(xiàn)有 Tomcat 環(huán)境集成,則可以選擇 Tomcat。
以jetty為例,我們只需要將默認(rèn)的tomcat依賴排除,并將jetty依賴引入,即可完成內(nèi)嵌web容器的切換。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!--1、移除tomcat依賴(exclusions:排除)--> <exclusions> <exclusion> <artifactId>spring-boot-starter-tomcat</artifactId> <groupId>org.springframework.boot</groupId> </exclusion> </exclusions></dependency><!--2、加入jetty依賴--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId></dependency>
啟動項(xiàng)目,我們可以看到,jetty確實(shí)啟動了。
Spring Boot 內(nèi)嵌 Web 容器的啟動原理可以概括為以下幾個步驟:
相關(guān)源碼如下:
SpringApplication類createApplicationContext方法,根據(jù)當(dāng)前web應(yīng)用的類型選擇匹配的應(yīng)用上下文類型,這邊會創(chuàng)建AnnotationConfigServletWebServerApplicationContext。
protected ConfigurableApplicationContext createApplicationContext() { Class<?> contextClass = this.applicationContextClass; if (contextClass == null) { try { switch (this.webApplicationType) { case SERVLET: contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS); break; case REACTIVE: contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS); break; default: contextClass = Class.forName(DEFAULT_CONTEXT_CLASS); } } catch (ClassNotFoundException ex) { throw new IllegalStateException( "Unable create a default ApplicationContext, please specify an ApplicationContextClass", ex); } } return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass); }
AnnotationConfigServletWebServerApplicationContext類createWebServer方法,會創(chuàng)建我們配置的web容器。
private void createWebServer() { WebServer webServer = this.webServer; ServletContext servletContext = getServletContext(); if (webServer == null && servletContext == null) { ServletWebServerFactory factory = getWebServerFactory(); this.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); } } initPropertySources(); }
這邊使用了工廠模式,不同的web容器有自己的工廠。
這邊我們以TomcatServletWebServerFactory為例,看下它的getWebServerFactory方法。
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); }
這邊創(chuàng)建了tomcat容器并初始化,然后返回。
本文鏈接:http://www.tebozhan.com/showinfo-26-88925-0.htmlSpring Boot 內(nèi)嵌 Web 容器啟動原理,驚爆你的眼球!
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com
上一篇: 關(guān)于 Python 的十個核心概念精講
下一篇: 一文徹底搞明白享元模式