SpringBoot去除内嵌tomcat的实现

时间:2022-08-23 14:05:05

SpringBoot内嵌tomcat,直接run Application即可,那么我们如何去除内嵌的tomcat,使用自己的呢?

一、POM(去除内嵌tomcat后,需要添加servlet依赖)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 <!-- 去除内嵌tomcat -->
 <exclusions>
  <exclusion>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-tomcat</artifactId>
  </exclusion>
 </exclusions>
</dependency>
<!--添加servlet的依赖-->
<dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>javax.servlet-api</artifactId>
 <version>3.1.0</version>
 <scope>provided</scope>
</dependency>
 
 <plugin>
  <artifactId>maven-war-plugin</artifactId>
  <version>3.0.0</version>
 </plugin>

打包方式设置成war

?
1
<packaging>war</packaging>

二、继承SpringBootServletInitializer重写configure方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.example.export;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
 
@SpringBootApplication
public class ExportApplication extends SpringBootServletInitializer {
 public static void main(String[] args) {
 SpringApplication.run(ExportApplication.class, args);
 }
 
 @Override
 protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
 return builder.sources(this.getClass());
 }
 
}

三、添加到tomcat容器、run 即可

到此这篇关于SpringBoot去除内嵌tomcat的实现的文章就介绍到这了,更多相关SpringBoot去除内嵌tomcat内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/chenhao_c_h/article/details/79992947