Spring Boot 内置容器详解:有哪些?默认用哪个?如何切换?

Spring Boot 的一个核心特性就是内嵌 Web 容器(Embedded Servlet Container),让我们无需部署 WAR 包到外部服务器,直接 main 方法启动即可运行 Web 应用。

本文带你彻底搞清:

  • Spring Boot 支持哪些内置容器

  • 默认使用的是哪个

  • 如何切换容器(含完整步骤)

  • 常见坑与最佳实践


一、Spring Boot 内置容器有哪些?

Spring Boot(基于 Servlet 规范)支持三大主流内置容器:

1️⃣ Apache Tomcat(默认)

  • 最常用、最成熟

  • Spring Boot 默认内置

特点:

  • 生态完善

  • 社区活跃

  • 与 Spring 兼容性最好


2️⃣ Jetty

特点:

  • 轻量级

  • 启动速度快

  • 更适合微服务/嵌入式场景


3️⃣ Undertow

特点:

  • 高性能(基于 NIO)

  • 低内存占用

  • 并发能力强


👉 总结对比:

容器特点适用场景
Tomcat稳定、成熟通用项目(默认)
Jetty轻量、快微服务、小型应用
Undertow高性能高并发系统

二、默认使用哪个容器?

👉 答案:

Spring Boot 默认使用 Tomcat

原因:

  • 最广泛使用

  • 与 Spring 生态最匹配

  • 稳定性高


依赖体现(核心)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

👉 这个 starter 内部已经包含:

  • spring-boot-starter-tomcat


三、如何切换内置容器?

核心思路就一句话:

排除默认 Tomcat,引入目标容器依赖


四、切换为 Jetty

✅ Maven 写法

<!-- 1. 排除 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>

<!-- 2. 引入 Jetty -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

五、切换为 Undertow

<!-- 1. 排除 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>

<!-- 2. 引入 Undertow -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

六、Gradle 写法

Jetty

implementation('org.springframework.boot:spring-boot-starter-web') {
    exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
}
implementation 'org.springframework.boot:spring-boot-starter-jetty'

Undertow

implementation('org.springframework.boot:spring-boot-starter-web') {
    exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
}
implementation 'org.springframework.boot:spring-boot-starter-undertow'

七、验证容器是否切换成功

启动项目,看日志:

Tomcat

Tomcat started on port 8080

Jetty

Started Jetty Server

Undertow

Undertow started on port

八、常见问题(很重要)

❗1. 没排除 Tomcat

👉 结果:

  • 可能仍然使用 Tomcat

  • 或出现冲突


❗2. 同时引入多个容器

👉 Spring Boot 行为:

  • 只会选择一个

  • 但不保证是你想要的

👉 建议:

只保留一个容器依赖


❗3. WebFlux 项目不适用

如果你用的是:

spring-boot-starter-webflux

👉 默认容器是:

  • Netty(不是 Tomcat)


九、什么时候需要切换容器?

✔ 建议切换的场景

  • 高并发系统 → Undertow

  • 资源受限 → Jetty

  • 特殊性能调优 → Jetty / Undertow


❌ 不建议切换

  • 普通业务系统

  • 对性能没有极端要求

👉 默认 Tomcat 足够强大


十、总结

记住三点:

1️⃣ Spring Boot 默认使用 Tomcat
2️⃣ 切换容器本质是“替换 starter”
3️⃣ 一定要排除原有容器依赖


一句话总结:

选 Tomcat 保稳定,选 Undertow 拼性能,选 Jetty 求轻量。


更多推荐