springboot-切换内置web服务器

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

在springboot的web环境中默认使用tomcat作为内置服务器,其实springboot提供了4种的内置服务器供我们选择,可以切换

一、

在外部库中找到org.springframework.boot:spring-boot-autuconfigure:2.7.17点击打开

在这里插入图片描述

在org.springframework.boot.autoconfigure下面的web包下找到embedded点击打开

在这里插入图片描述

包中的五个文件,下面四个是springboot内置的四种服务器,分别是:
1、jetty
2Notty
3、tomcat
4、undertow

二、怎么切换这四种服务器???

1.原理

打开第一个文件
在这里插入图片描述
下面是其中一个的代码,表示引入了Undertow的坐标,就会启动这个服务器

@Configuration(
        proxyBeanMethods = false
    )
    @ConditionalOnClass({Undertow.class, SslClientAuthMode.class})
    public static class UndertowWebServerFactoryCustomizerConfiguration {
        public UndertowWebServerFactoryCustomizerConfiguration() {
        }

        @Bean
        public UndertowWebServerFactoryCustomizer undertowWebServerFactoryCustomizer(Environment environment, ServerProperties serverProperties) {
            return new UndertowWebServerFactoryCustomizer(environment, serverProperties);
        }
    }

所以需要切换时,是需要在pom.xml中写入对应内置服务器的坐标
另外

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
              //排除tomcat的依赖
            <exclusions>
                <exclusion>
                    <groupId>spring-boot-stater-tomcat</groupId>
                    <artifactId>org.springframework.boot</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

在加入依赖前,需要在web依赖下面排除掉tomcat的默认
点开pom.xml的web依赖就会发现默认依赖了tomcat
在这里插入图片描述

总结

直接在pom.xml引入服务器的依赖即可
注意,注意,注意!!!
在web依赖中排除tomcat的默认,加入的依赖才会失效

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            //排除tomcat的依赖
            <exclusions>
                <exclusion>
                    <groupId>spring-boot-stater-tomcat</groupId>
                    <artifactId>org.springframework.boot</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值