当前位置: 首页 > news >正文

58、嵌入式Servlet容器-【源码分析】切换web服务器与定制化

58、嵌入式Servlet容器切换web服务器与定制化

# 嵌入式Servlet容器切换与定制化

## 切换Web服务器

### 1. 原理

Spring Boot默认使用Tomcat作为嵌入式Servlet容器。切换其他容器(如Jetty或Undertow)的原理如下:

#### 自动配置类

- `ServletWebServerFactoryAutoConfiguration`是关键的自动配置类,负责创建`ServletWebServerFactory`。

#### 条件判断

- 根据项目中引入的依赖,自动配置类会判断系统中存在哪些Web服务器相关的类。

#### 工厂类

- Spring Boot提供了多个`ServletWebServerFactory`实现:

  - `TomcatServletWebServerFactory`

  

  - `JettyServletWebServerFactory`

  

  - `UndertowServletWebServerFactory`

- 根据条件判断,选择相应的工厂类创建对应的Web服务器。

### 2. 切换步骤

#### 排除默认Tomcat依赖

在`pom.xml`中排除`spring-boot-starter-tomcat`:

```xml

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

```

#### 添加目标服务器依赖

- **切换为Jetty**:

  ```xml

  <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-jetty</artifactId>

  </dependency>

  ```

- **切换为Undertow**:

  ```xml

  <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-undertow</artifactId>

  </dependency>

  ```

### 3. 示例

#### 切换为Jetty

```xml

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-web</artifactId>

    <exclusions>

        <!-- 排除Tomcat -->

        <exclusion>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-tomcat</artifactId>

        </exclusion>

    </exclusions>

</dependency>

<!-- 引入Jetty -->

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-jetty</artifactId>

</dependency>

```

重新启动应用,Spring Boot将使用Jetty作为嵌入式Servlet容器。

## 定制嵌入式Servlet容器

### 1. 通过配置文件

在`application.properties`或`application.yml`中配置服务器属性:

```properties

# 修改端口

server.port=8081

# 设置上下文路径

server.servlet.context-path=/myapp

# Tomcat特有配置

server.tomcat.uri-encoding=UTF-8

```

### 2. 实现定制器接口

实现`WebServerFactoryCustomizer`接口,定制`ServletWebServerFactory`:

```java

@Configuration

public class CustomServletContainerConfig implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

    @Override

    public void customize(ConfigurableServletWebServerFactory factory) {

        // 设置端口

        factory.setPort(8082);

        

        // 设置访问日志

        factory.setAccessLogEnabled(true);

        

        // 其他定制...

    }

}

```

### 3. 示例

#### 定制Jetty容器

```java

@Configuration

public class JettyCustomizer implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

    @Override

    public void customize(ConfigurableServletWebServerFactory factory) {

        if (factory instanceof JettyServletWebServerFactory) {

            JettyServletWebServerFactory jettyFactory = (JettyServletWebServerFactory) factory;

            

            // 设置Jetty特有配置

            jettyFactory.addServerCustomizers(server -> {

                // 添加连接器配置

                ServerConnector connector = new ServerConnector(server);

                connector.setPort(8083);

                server.addConnector(connector);

            });

        }

    }

}

```

通过以上方式,可以灵活地切换和定制Spring Boot的嵌入式Servlet容器,满足不同的应用需求。

相关文章:

  • UDS协议中0x34、0x36、0x37服务详解及应用
  • 表达式的自动类型转换
  • HashMap vs LinkedHashMap
  • 短剧热浪,席卷海内外。
  • 2025企业级BI产品评测和推荐
  • 论坛系统自动化测试
  • GRUB2 启动配置的工作原理与优先级规则详解
  • SSH远程连接到Windows服务器
  • 概率基础——不确定性的数学
  • 1.3、SDH光接口类型
  • 批处理实现:自动抓取perfetto日志 自动导出到当前文件夹 自动打开分析页面
  • (nice!!!)(LeetCode 每日一题) 2616. 最小化数对的最大差值 (二分查找)
  • 落地 DDD 领域模型(常见的实现模式)
  • 单项链表的操作及其实现
  • 【二叉树】(四)二叉搜索树的基础修改构造及属性求解1
  • Spring 事务传播行为详解
  • AbMole小课堂:从肿瘤研究到体内模型构建,Mitomycin C一“剂”搞
  • 寻找区域中的面积和中心点
  • java哨兵底层原理
  • linux下安装所有用户能共享的anaconda
  • wordpress主题 v2ex/周口seo公司
  • 做动态网站文字显示问号/seo是什么品牌
  • 做网站用什么空间/百度怎么优化关键词排名
  • 网站开发专业有哪些/深圳百度网站排名优化
  • 移动端网站一般宽做多大/海外推广服务
  • 酒店如何做网站/营销网站推荐