SpringBoot如何切換成其它的嵌入式Servlet容器(Jetty和Undertow)
如何切換成其它的嵌入式Servlet容器(Jetty和Undertow)
SpringBoot默認使用的內置Servlet容器是Tomcat
然而 SpringBoot還支持其它的Servlet容器 默認的Tomcat只是其中一種
SpringBoot還支持Jetty和Undertow
Jetty適合用于長鏈接應用 例如聊天Undertow是一個高性能非阻塞的Servlet容器 并發(fā)性能非常好 但不支持jsp

若要切換 只需要在pom文件中排除自帶的Tomcat啟動器 再引入其它Servlet容器的啟動器即可
spring-boot-starter-web里面引入了spring-boot-starter-tomcat 因此默認使用Tomcat
因此 只需排除原先的Tomcat 再引入要添加的Servlet容器的依賴 即可:
切換成Jetty:
<!-- 引入Web模塊 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- 排除tomcat容器 -->
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入其它的Servlet容器 -->
<dependency>
<artifactId>spring-boot-starter-jetty</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
Jetty啟動成功
同理 切換成undertow:
<!-- 引入Web模塊 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- 排除tomcat容器 -->
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入其它的Servlet容器 -->
<dependency>
<artifactId>spring-boot-starter-undertow</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
Undertow啟動成功
SpringBoot web開發(fā)_嵌入式Servlet容器
1、切換嵌入式Servlet容器
1.1、SpringBoot支持的Servlet種類及其切換
1)默認支持的webServer:Tomcat、Jrtty、Undertow
2)切換服務器

<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>1.2、原理
1)SpringBoot應用啟動發(fā)現(xiàn)當前是Web應用。web場景包-導入tomcat
2)web應用會創(chuàng)建一個web版的ioc容器 ServletWebServerApplicationContext
3)ServletWebServerApplicationContext 啟動的時候尋找 ServletWebServerFactory(Servlet 的web服務器工廠---> Servlet 的web服務器)
4)SpringBoot底層默認有很多的WebServer工廠;
TomcatServletWebServerFactoryJettyServletWebServerFactoryUndertowServletWebServerFactory
5)底層直接會有一個自動配置類。
ServletWebServerFactoryAutoConfiguration導入了ServletWebServerFactoryConfiguration(配置類)
6)ServletWebServerFactoryConfiguration 配置類 根據(jù)動態(tài)判斷系統(tǒng)中到底導入了那個Web服務器的包。(默認是web-starter導入tomcat包),容器中就有 TomcatServletWebServerFactory
7)TomcatServletWebServerFactory 創(chuàng)建出Tomcat服務器并啟動;TomcatWebServer 的構造器擁有初始化方法initialize---this.tomcat.start();
8)內嵌服務器,就是手動把啟動服務器的代碼調用(tomcat核心jar包存在)
2、定制Servlet容器
2.1、修改配置文件
通過對application.properties配置文件的設置,定制Servlet容器
#修改servlet容器 server.port=8000 #server.undertow.accesslog.dir=/tmp
2.2、實現(xiàn) WebServerFactoryCustomizer
xxxxxCustomizer:定制化器,可以改變xxxx的默認規(guī)則
通過WebServerFactoryCustomizer修改 Servlet 容器的響應端口號:
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;
@Component
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
@Override
public void customize(ConfigurableServletWebServerFactory server) {
server.setPort(9000);
}
}2.3、ConfigurableServletWebServerFactory
直接自定義 ConfigurableServletWebServerFactory 的接口實現(xiàn)類
public interface ConfigurableWebServerFactory extends WebServerFactory, ErrorPageRegistry {
void setPort(int port);
void setAddress(InetAddress address);
void setErrorPages(Set<? extends ErrorPage> errorPages);
void setSsl(Ssl ssl);
void setSslStoreProvider(SslStoreProvider sslStoreProvider);
void setHttp2(Http2 http2);
void setCompression(Compression compression);
void setServerHeader(String serverHeader);
default void setShutdown(Shutdown shutdown) {
}
}ConfigurableServletWebServerFactory 接口實現(xiàn)類(框架中默認實現(xiàn)類):

通過自定義 ConfigurableServletWebServerFactory 接口的實現(xiàn)類,即可定制Servlet容器
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- SpringBoot集成WebServlet出現(xiàn)自定義servlet請求失敗的問題解決方案
- SpringBoot里使用Servlet進行請求的實現(xiàn)示例
- springboot掃描自定義的servlet和filter代碼詳解
- Springboot注入成員變量HttpServletRequest的原理分析
- SpringBoot3.1.2 引入Swagger報錯Type javax.servlet.http.HttpServletRequest not present解決辦法
- 解決IDEA啟動springboot項目報錯java.lang.ClassNotFoundException:?javax.servlet.ServletContext
- SpringBoot獲取HttpServletRequest的3種方式總結
- Springboot如何添加server.servlet.context-path相關使用
- SpringBoot項目找不到javax.servlet.Filter的問題及解決
相關文章
Spring Boot application.yml配置文件示例詳解
本文詳細介紹了SpringBootapplication.yml配置文件的使用和配置項,通過學習本文,您應該已經(jīng)掌握了如何使用application.yml文件來配置SpringBoot應用程序的不同組件,如數(shù)據(jù)源、數(shù)據(jù)庫、緩存、郵件服務等,感興趣的朋友一起看看吧2025-02-02
Mybatis配置之properties和settings標簽的用法
這篇文章主要介紹了Mybatis配置之properties和settings標簽的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
Java超詳細教你寫一個斗地主洗牌發(fā)牌系統(tǒng)
這篇文章主要介紹了怎么用Java來你寫一個斗地主種洗牌和發(fā)牌的功能,斗地主相信大家都知道,同時也知道每一局都要洗牌打亂順序再發(fā)牌,本篇我們就來實現(xiàn)這個功能,感興趣的朋友跟隨文章往下看看吧2022-03-03
Intellij IDEA 錄制快捷鍵實現(xiàn)自動格式化的方法
這篇文章主要介紹了Intellij IDEA 錄制快捷鍵實現(xiàn)自動格式化的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
詳解Spring框架下向異步線程傳遞HttpServletRequest參數(shù)的坑
這篇文章主要介紹了詳解Spring框架下向異步線程傳遞HttpServletRequest參數(shù)的坑,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03

