SpringBoot默认使用的内置Servlet容器是Tomcat
然而 SpringBoot还支持其它的Servlet容器 默认的Tomcat只是其中一种
SpringBoot还支持Jetty和Undertow
- Jetty适合用于长链接应用 例如聊天
- Undertow是一个高性能非阻塞的Servlet容器 并发性能非常好 但不支持jsp
若要切换 只需要在pom文件中排除自带的Tomcat启动器 再引入其它Servlet容器的启动器即可
spring-boot-starter-web里面引入了spring-boot-starter-tomcat 因此默认使用Tomcat
因此 只需排除原先的Tomcat 再引入要添加的Servlet容器的依赖 即可:
切换成Jetty:
<!-- 引入Web模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- 排除tomcat容器 -->
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入其它的Servlet容器 -->
<dependency>
<artifactId>spring-boot-starter-jetty</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
Jetty启动成功
同理 切换成undertow:
<!-- 引入Web模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- 排除tomcat容器 -->
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入其它的Servlet容器 -->
<dependency>
<artifactId>spring-boot-starter-undertow</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
Undertow启动成功