15-SpringBoot自动配置-切换内置web服务器

15-SpringBoot自动配置-切换内置web服务器

切换内置web服务器

SpringBoot的Web环境中默认使用tomcat作为内置服务器,其实SpringBoot提供了4种内置服务器供我们选择,我们可以很方便的切换。

演示案例

1.我们首先还是使用上一篇的项目,启动看看 Tomcat 作为内置 web 服务启动

首先要确认一下项目的 pom.xml 是否有 web 启动依赖:

15-SpringBoot自动配置-切换内置web服务器
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>

2.确认了存在依赖之后,我们在从源码查看内置的web服务器

我们首先打开外部库,依然是找到 autoconfigure 的包:

15-SpringBoot自动配置-切换内置web服务器

再找到 web 包,如下:

15-SpringBoot自动配置-切换内置web服务器

可以看到内置了几种web服务器。

15-SpringBoot自动配置-切换内置web服务器
package org.springframework.boot.autoconfigure.web.embedded;import io.undertow.Undertow;import org.apache.catalina.startup.Tomcat;import org.apache.coyote.UpgradeProtocol;import org.eclipse.jetty.server.Server;import org.eclipse.jetty.util.Loader;import org.eclipse.jetty.webapp.WebAppContext;import org.xnio.SslClientAuthMode;import reactor.netty.http.server.HttpServer;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;import org.springframework.boot.autoconfigure.web.ServerProperties;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.env.Environment;      @Configuration@ConditionalOnWebApplication@EnableConfigurationProperties(ServerProperties.class)public class EmbeddedWebServerFactoryCustomizerAutoConfiguration {              @Configuration   @ConditionalOnClass({ Tomcat.class, UpgradeProtocol.class })    public static class TomcatWebServerFactoryCustomizerConfiguration {      @Bean      public TomcatWebServerFactoryCustomizer tomcatWebServerFactoryCustomizer(Environment environment,            ServerProperties serverProperties) {         return new TomcatWebServerFactoryCustomizer(environment, serverProperties);      }   }              @Configuration   @ConditionalOnClass({ Server.class, Loader.class, WebAppContext.class })   public static class JettyWebServerFactoryCustomizerConfiguration {      @Bean      public JettyWebServerFactoryCustomizer jettyWebServerFactoryCustomizer(Environment environment,            ServerProperties serverProperties) {         return new JettyWebServerFactoryCustomizer(environment, serverProperties);      }   }              @Configuration   @ConditionalOnClass({ Undertow.class, SslClientAuthMode.class })   public static class UndertowWebServerFactoryCustomizerConfiguration {      @Bean      public UndertowWebServerFactoryCustomizer undertowWebServerFactoryCustomizer(Environment environment,            ServerProperties serverProperties) {         return new UndertowWebServerFactoryCustomizer(environment, serverProperties);      }   }              @Configuration   @ConditionalOnClass(HttpServer.class)   public static class NettyWebServerFactoryCustomizerConfiguration {      @Bean      public NettyWebServerFactoryCustomizer nettyWebServerFactoryCustomizer(Environment environment,            ServerProperties serverProperties) {         return new NettyWebServerFactoryCustomizer(environment, serverProperties);      }   }}

也就是说,我们只要改变类的依赖,那么就应该可以切换内置的web服务器了。

3.查看 spring-boot-starter-web 启动依赖的内部继承关系

15-SpringBoot自动配置-切换内置web服务器

4.排除 tomcat 的依赖

15-SpringBoot自动配置-切换内置web服务器

image-20220224213305912

设置了排除之后,我们回头看看 pom.xml 里面的配置如下:

15-SpringBoot自动配置-切换内置web服务器
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcatartifactId>
<groupId>org.springframework.bootgroupId>
exclusion>
exclusions>
dependency>

可以看到已经排除了依赖。

5.增加 jetty 依赖,使用 jetty 启动web服务

15-SpringBoot自动配置-切换内置web服务器
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>

<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcatartifactId>
<groupId>org.springframework.bootgroupId>
exclusion>
exclusions>
dependency>


<dependency>
<artifactId>spring-boot-starter-jettyartifactId>
<groupId>org.springframework.bootgroupId>
dependency>

Original: https://blog.51cto.com/u_11239407/5435198
Author: 海洋的渔夫
Title: 15-SpringBoot自动配置-切换内置web服务器

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/516862/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球