SpringBoot高级-自动配置之切换内置web服务器

前言:本篇介绍,通过利用SpringBoot的自动配置原理,实现切换内置web服务器

我们都了解,当导入web起步依赖后,SpringBoot程序启动的时候,默认加载的就是tomcat服务器,实际上SpringBoot默认为我们提供了四种(Jetty、Netty、Tomcat、Undertow)服务器,我们可以很方便的切换服务器。

查找源码路径

这里我们重点关于EmbeddedWebServerFactoryCustomizerAutoConfiguration这个类,之前我有提到凡是SpringBoot提供的自动配置类,名称命名时,规则都是*AutoConfiguration。

自动配置类释义:所谓自动配置类,就是当程序启动时,IOC容器会自动加载配置类,初始化配置类中的Bean,但是配置类中有些Bean是有条件成立时才能初始化

继续回到解释EmbeddedWebServerFactoryCustomizerAutoConfiguration 这个类,此类上有一个注解@ConditionalOnWebApplication,表明是web程序时,方可生效,即:有web依赖时,此类才会在IOC容器自动加载。

@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);
		}

	}

}

接着看我们都熟悉的关于初始化tomcat这个Bean,这个Bean上有一个注解@ConditionalOnClass({ Tomcat.class, UpgradeProtocol.class }),表示,当含有Tomcat和UpgradeProtocol这两个字节码文件时,才会初始化tomcat,这时,您心中就有疑问了,我只是引入了web依赖,并没有单独引入tomcat依赖啊,怎么会有这两个字节码文件呢?下面我们再看maven依赖传递图。

一、首先导入web依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

二、打开maven依赖图

三、分析本项目的依赖图,解释了为什么tomcat会自动变为默认的web服务器了,因为导入web依赖后,web依赖默认就包含了tomcat相关的jar,自然也包含那两个字节码文件了。

下面,实现切换web服务器

一、移除tomcat依赖,操作如下

二、查看pom.xml中web依赖的变化,并导入jetty服务器依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!--1、移除tomcat依赖(exclusions:排除)-->
    <exclusions>
        <exclusion>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <groupId>org.springframework.boot</groupId>
        </exclusion>
    </exclusions>
</dependency>

<!--2、加入jetty依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

三、启动程序,测试,如下,证明我们已经成功切换web服务器了。

小结:切换web服务器,本质上还是利用了SpringBoot自动配置的原理实现的。

posted @ 2020-10-09 22:15  温森  阅读(2383)  评论(0编辑  收藏  举报