前言:本篇介紹,通過利用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服務器了。