Spring Boot 的嵌入式服务器(如 Tomcat)是如何启动的?如何替换为 Jetty 或 Undertow?

Spring Boot 的嵌入式服务器启动过程

Spring Boot 提供了一种快速和简单的方式来启动 Java 应用程序,尤其是通过嵌入式服务器(如 Tomcat, Jetty, Undertow)实现这一点。这种架构允许开发者将 Web 应用程序打包成一个独立的可执行 JAR 文件,其中包含了需要的 Web 服务器和依赖项。

启动流程
  1. 创建 Spring Boot 应用

    当你创建 Spring Boot 应用时,通常会有一个入口类,这个类会使用 @SpringBootApplication 注解标记,它是一个复合注解,结合了 @Configuration、@EnableAutoConfiguration 和 @ComponentScan。

    java 复制代码
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class MySpringBootApplication {
        public static void main(String[] args) {
            SpringApplication.run(MySpringBootApplication.class, args);
        }
    }
  2. 自动配置的处理

    Spring Boot 在启动时会通过 @EnableAutoConfiguration 自动配置应用,根据应用依赖项和环境,自动选择合适的嵌入式服务器,并设置其默认配置。

  3. 初始化嵌入式服务器

    Spring Boot 根据 spring.main.web-application-type 设置,决定应用的类型,如果是 Web 应用,则会配置并启动嵌入式服务器。对于传统的 Servlet 应用,默认会使用 Tomcat。

  4. 启动服务器

    一旦 Spring Boot 准备好了所有的 Servlet 和上下文,它会使用嵌入式服务器的 API 启动服务器。对于 Tomcat,代码类似于:

    java 复制代码
    public class TomcatServletWebServerFactory implements WebServerFactory {
        @Override
        public WebServer getWebServer(ServletContextInitializer... initializers) {
            // 实际的 Tomcat 服务器启动逻辑
            Tomcat tomcat = new Tomcat();
            // 配置和初始化
            tomcat.start();
            return new TomcatWebServer(tomcat);
        }
    }

替换嵌入式服务器

Spring Boot 非常灵活,支持通过不同的 Maven 或 Gradle 依赖来替换嵌入式服务器。以下是如何替换 Tomcat 为 Jetty 或 Undertow 的步骤。

1. 替换为 Jetty

如果你想用 Jetty 替换 Tomcat,需要在你的 pom.xml 或 build.gradle 文件中进行如下更改。

Maven:

首先,删除 Tomcat 的依赖:

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope> <!-- 将 Tomcat 的作用域改为 provided -->
</dependency>

然后,添加 Jetty 依赖:

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

Gradle:

同样,删除 Tomcat 的依赖:

groovy 复制代码
implementation ('org.springframework.boot:spring-boot-starter-tomcat') {
    exclude module: 'spring-boot-starter-tomcat'
}

然后,添加 Jetty 依赖:

groovy 复制代码
implementation 'org.springframework.boot:spring-boot-starter-jetty'
2. 替换为 Undertow

替换为 Undertow 的方式与 Jetty 相似。

Maven:

同样,删除 Tomcat 的依赖:

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

然后,添加 Undertow 依赖:

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

Gradle:

在构建文件中实现类似的步骤:

groovy 复制代码
implementation ('org.springframework.boot:spring-boot-starter-tomcat') {
    exclude module: 'spring-boot-starter-tomcat'
}
implementation 'org.springframework.boot:spring-boot-starter-undertow'

代码示例

假设我们要创建一个简单的 Spring Boot 应用并将嵌入式 Tomcat 替换为 Jetty,代码结构如下:

java 复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class MySpringBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(MySpringBootApplication.class, args);
    }

    @GetMapping("/")
    public String home() {
        return "Hello, Spring Boot with Jetty!";
    }
}

启动应用

在 IDE 中运行 MySpringBootApplication 类,Spring Boot 会启动嵌入式 Jetty 服务器(如果你已经按照上面的步骤进行了配置)。打开浏览器并访问 http://localhost:8080,你应该能看到 "Hello, Spring Boot with Jetty!" 的输出。

总结

Spring Boot 的嵌入式服务器的启动过程较为简单,主要依赖于自动配置功能。使用不同的嵌入式服务器也是非常容易的,只需更改 Maven 或 Gradle 中的依赖即可。这使得开发者可以根据项目需求灵活选择合适的服务器。无论是使用 Tomcat、Jetty 还是 Undertow,Spring Boot 都能有效支持并优化 Java Web 应用程序的开发和部署。

相关推荐
土星云SaturnCloud2 小时前
土星云智慧服务区方案:AI视觉+边缘计算+视频存管一体化的全场景智能化落地解析
服务器·人工智能·ai·边缘计算
liqingdi4373 小时前
[SWPUCTF 2023 秋季新生赛]Easy SMC
服务器·前端·javascript
自养Agent4 小时前
MCP Server Boundary Audit Report
linux·服务器·人工智能·windows·大模型·llm
派小心.4 小时前
网站统计采样数据的误差核对
大数据·服务器·前端·数据库·人工智能·小程序·数据分析
A心有千千结5 小时前
通过 SSH 反向代理让服务器借用本机网络访问 GitHub 的最佳实践
linux·服务器·网络·git·ssh·github
fengkai45455 小时前
十三、Zabbix 监控
运维·服务器·zabbix·监控·网络设备
数字新视界5 小时前
机房动环监控助力智能管理
大数据·服务器·数据库·动环监控系统·动力与环境监控系统
算力小木6 小时前
#标题:5090 vs 4090D 到底差多少?我拿八卡实测了一周
服务器