一、springboot中内嵌式Web容器
- Tomcat (默认)
- Jetty
- undertow
springboot既然支持三种容器,再加上springboot插拔式设计的关系,所以在修改web容器也是很容易的,只要在pom.xml文件中引入依赖即可。
二、Tomcat容器
引入spring-boot-starter-web依赖,由于Tomcat属于默认容器,这样配置即可,也可以修改tomcat版本
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
tomcat默认版本是随着springboot的版本变动的,那么如何查看springboot引入的是什么版本的tomcat呢?
1、查看springboot下tomcat版本号
首先需要了解一下springboot父依赖,这个指定的是当前springboot版本,spring-boot-starter-parent是一个特殊的starter,它用来提供相关的Maven默认依赖,使用它之后,常用的包依赖可以省去version标签。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
我们应该查找maven仓库中引入的springboot位置,默认在C盘用户.M2下,然后找到对应的springboot版本的pom文件搜索<tomcat.version>节点就行
C:\Users\用户.m2\repository\org\springframework\boot\spring-boot-dependencies\版本号\xxx.pom
但是由于我修改了maven仓库路径,打开pom文件
E:\apache-maven-3.2.5\maven-repository\org\springframework\boot\spring-boot-dependencies\2.1.8.RELEASE\spring-boot-dependencies-2.1.8.RELEASE.pom
会发现 节点下好多引入默认的版本信息,寻找tomcat即可。这种方式很麻烦,其实有更简单的方式,就是springboot启动项目,查看控制台tomcat栈信息就行,像这么一行。
2019-09-25 14:45:14.037 INFO 7260 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.24]
2、修改默认的tomcat版本
① 如果是2.0以后的springboot版本,pom直接添加tomcat.version节点就行
<properties>
<tomcat.version>9.0.26</tomcat.version>
</properties>
② 若是2.0之前的版本,像1.5.6,不能添加tomcat9.0以上版本,会报错,具体为什么我也没研究,只添加上述节点会报错,缺少jar文件,添org.apache.juli.logging.LogFactory这个依赖
<properties>
<tomcat.version>8.0.23</tomcat.version>
</properties>
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) ~[spring-boot-1.5