提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
在springboot的web环境中默认使用tomcat作为内置服务器,其实springboot提供了4种的内置服务器供我们选择,可以切换
一、
在外部库中找到org.springframework.boot:spring-boot-autuconfigure:2.7.17点击打开
在org.springframework.boot.autoconfigure下面的web包下找到embedded点击打开
包中的五个文件,下面四个是springboot内置的四种服务器,分别是:
1、jetty
2、Notty
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>