SpringBoot嵌入式Servlet容器

一、切换嵌入式 Servlet 容器

默认的 WebServer 有:Tomcat、Jetty、Undertow;如果需要切换 Web 服务器,只需要排除默认的 Tomcat 依赖,添加对应要切换的 WebServer 的依赖即可;例如:我们切换 Tomcat 为 Undertow 的话,只需要在 pom.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>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

二、自定义 WebServer 配置

默认情况下,我们通过在 application.properties 配置文件中添加 server.xxx 配置即可修改 WebServer 的默认配置;如果要自定义修改配置,可参考如下实现方法

新建 CustomServer.class 并实现 WebServerFactoryCustomizer<ConfigurableWebServerFactory> 接口,重写 customize() 方法即可

@Component
public class CustomServer implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
    @Override
    public void customize(ConfigurableWebServerFactory factory) {
        // 修改服务启动端口
        factory.setPort(9090);
    }
}

如果 application.properties 中也配置了 WebServer 配置,则自定义的 CustomServer 优先级更高

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值