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

网友投稿 251 2022-11-16

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

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

切换内置web服务器

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

演示案例

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

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

org.springframework.boot spring-boot-starter-web

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

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

再找到 web 包,如下:

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

/* * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */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.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;/** * {@link EnableAutoConfiguration Auto-configuration} for embedded servlet and reactive * web servers customizations. * * @author Phillip Webb * @since 2.0.0 */@Configuration@ConditionalOnWebApplication@EnableConfigurationProperties(ServerProperties.class)public class EmbeddedWebServerFactoryCustomizerAutoConfiguration { /** * Nested configuration if Tomcat is being used. */ @Configuration @ConditionalOnClass({ Tomcat.class, UpgradeProtocol.class }) // 只有配置了这些类的依赖,才会自动加载 public static class TomcatWebServerFactoryCustomizerConfiguration { @Bean public TomcatWebServerFactoryCustomizer tomcatWebServerFactoryCustomizer(Environment environment, ServerProperties serverProperties) { return new TomcatWebServerFactoryCustomizer(environment, serverProperties); } } /** * Nested configuration if Jetty is being used. */ @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); } } /** * Nested configuration if Undertow is being used. */ @Configuration @ConditionalOnClass({ Undertow.class, SslClientAuthMode.class }) public static class UndertowWebServerFactoryCustomizerConfiguration { @Bean public UndertowWebServerFactoryCustomizer undertowWebServerFactoryCustomizer(Environment environment, ServerProperties serverProperties) { return new UndertowWebServerFactoryCustomizer(environment, serverProperties); } } /** * Nested configuration if Netty is being used. */ @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 启动依赖的内部继承关系

4.排除 tomcat 的依赖

image-20220224213305912

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

org.springframework.boot spring-boot-starter-web spring-boot-starter-tomcat org.springframework.boot

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

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

org.springframework.boot spring-boot-starter-web spring-boot-starter-tomcat org.springframework.boot spring-boot-starter-jetty org.springframework.boot

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring Cloud Alibaba依赖管理
下一篇:4g dtu和4g路由器之间的区别都有哪些
相关文章

 发表评论

暂时没有评论,来抢沙发吧~