AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當(dāng)前位置:首頁(yè) > 科技  > 軟件

Spring Boot 內(nèi)嵌 Web 容器啟動(dòng)原理,驚爆你的眼球!

來源: 責(zé)編: 時(shí)間:2024-05-17 17:46:49 167觀看
導(dǎo)讀一、spring boot內(nèi)嵌web容器介紹Spring Boot 支持以下內(nèi)嵌的 Web 容器:Tomcat:Spring Boot 默認(rèn)使用的 Web 容器,也是最常用的選擇。Tomcat 是一個(gè)流行的開源 Servlet 容器,具有廣泛的應(yīng)用和良好的性能。Jetty:另一個(gè)常用

一、spring boot內(nèi)嵌web容器介紹

Spring Boot 支持以下內(nèi)嵌的 Web 容器:OtU28資訊網(wǎng)——每日最新資訊28at.com

  1. Tomcat:Spring Boot 默認(rèn)使用的 Web 容器,也是最常用的選擇。Tomcat 是一個(gè)流行的開源 Servlet 容器,具有廣泛的應(yīng)用和良好的性能。
  2. Jetty:另一個(gè)常用的 Web 容器,它具有輕量級(jí)和高效的特點(diǎn)。Spring Boot 也提供了對(duì) Jetty 的支持。
  3. Undertow:一個(gè)高性能的 Web 容器,特別適合處理高并發(fā)和大規(guī)模的應(yīng)用。Spring Boot 也可以與 Undertow 集成。

這些內(nèi)嵌的 Web 容器都可以在 Spring Boot 應(yīng)用中直接使用,無(wú)需額外的安裝和配置。Spring Boot 會(huì)自動(dòng)根據(jù)項(xiàng)目的依賴和配置來選擇合適的 Web 容器,并進(jìn)行相應(yīng)的配置和啟動(dòng)。OtU28資訊網(wǎng)——每日最新資訊28at.com

你可以根據(jù)項(xiàng)目的需求和特點(diǎn)選擇適合的 Web 容器。例如,如果對(duì)性能有較高要求,可以考慮使用 Undertow;如果需要與現(xiàn)有 Tomcat 環(huán)境集成,則可以選擇 Tomcat。OtU28資訊網(wǎng)——每日最新資訊28at.com

二、如何切換spring boot內(nèi)嵌web容器

以jetty為例,我們只需要將默認(rèn)的tomcat依賴排除,并將jetty依賴引入,即可完成內(nèi)嵌web容器的切換。OtU28資訊網(wǎng)——每日最新資訊28at.com

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

啟動(dòng)項(xiàng)目,我們可以看到,jetty確實(shí)啟動(dòng)了。OtU28資訊網(wǎng)——每日最新資訊28at.com

OtU28資訊網(wǎng)——每日最新資訊28at.com

OtU28資訊網(wǎng)——每日最新資訊28at.com

三、spring boot內(nèi)嵌web容器啟動(dòng)原理

Spring Boot 內(nèi)嵌 Web 容器的啟動(dòng)原理可以概括為以下幾個(gè)步驟:OtU28資訊網(wǎng)——每日最新資訊28at.com

  1. 依賴注入:Spring Boot 在啟動(dòng)時(shí),會(huì)自動(dòng)掃描項(xiàng)目中的依賴,并將相關(guān)的 Web 容器依賴注入到應(yīng)用程序中。
  2. 容器初始化:Spring Boot 會(huì)根據(jù)配置文件或默認(rèn)設(shè)置,初始化所選的內(nèi)嵌 Web 容器。這包括創(chuàng)建容器實(shí)例、設(shè)置端口號(hào)、上下文路徑等。
  3. 組件掃描和注冊(cè):Spring Boot 會(huì)掃描項(xiàng)目中的組件(如控制器、服務(wù)等),并將它們注冊(cè)到 Web 容器中,以便處理 HTTP 請(qǐng)求。
  4. 配置加載:Spring Boot 會(huì)加載應(yīng)用程序的配置信息,包括端口號(hào)、上下文路徑、靜態(tài)資源路徑等,并將這些配置應(yīng)用到 Web 容器中。
  5. 啟動(dòng)容器:一旦容器初始化完成并配置好,Spring Boot 會(huì)啟動(dòng)內(nèi)嵌的 Web 容器,使其開始監(jiān)聽指定的端口,并準(zhǔn)備處理 HTTP 請(qǐng)求。
  6. 應(yīng)用程序運(yùn)行:此時(shí),應(yīng)用程序已經(jīng)在所選的內(nèi)嵌 Web 容器中運(yùn)行,可以通過訪問指定的端口來訪問應(yīng)用程序的功能。

相關(guān)源碼如下:OtU28資訊網(wǎng)——每日最新資訊28at.com

SpringApplication類createApplicationContext方法,根據(jù)當(dāng)前web應(yīng)用的類型選擇匹配的應(yīng)用上下文類型,這邊會(huì)創(chuàng)建AnnotationConfigServletWebServerApplicationContext。OtU28資訊網(wǎng)——每日最新資訊28at.com

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方法,會(huì)創(chuàng)建我們配置的web容器。OtU28資訊網(wǎng)——每日最新資訊28at.com

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容器有自己的工廠。OtU28資訊網(wǎng)——每日最新資訊28at.com

OtU28資訊網(wǎng)——每日最新資訊28at.com

OtU28資訊網(wǎng)——每日最新資訊28at.com

OtU28資訊網(wǎng)——每日最新資訊28at.com

這邊我們以TomcatServletWebServerFactory為例,看下它的getWebServerFactory方法。OtU28資訊網(wǎng)——每日最新資訊28at.com

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容器并初始化,然后返回。OtU28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-88925-0.htmlSpring Boot 內(nèi)嵌 Web 容器啟動(dòng)原理,驚爆你的眼球!

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com

上一篇: 關(guān)于 Python 的十個(gè)核心概念精講

下一篇: 一文徹底搞明白享元模式

標(biāo)簽:
  • 熱門焦點(diǎn)
Top