Spring Boot取消默认tomcat启动,打成war包发布到服务器运行
一、设置打包方式
在pom.xml中设置打包格式
<packaging>war</packaging>
二、取消Spring Boot的tomcat
<!--部署成war包时开启↓↓↓↓-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!--部署成war包时开启↑↑↑↑-->
scope的provided参数可以参考我另一篇博客provided参数
取消默认的tomcat之后,还需要再排除spring-boot-starter-web
中的一些web包
否则会启动失败
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
三、重写启动类SpringBootApplication.java
继承 SpringBootServletInitializer
重写 configure
方法
@SpringBootApplication
public class SpringBootApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SpringBootApplication.class, args);
}
}
四、修改打包的名称(可选)
application.yml的
server:
servlet:
context-path: /springboot
和pom.xml
<build>
<finalName>springboot</finalName>
</build>
必须保持一致
作者:梦里梦外
--------------------------------------------------------------------------------------------------------------------
个性签名:以梦为马,驰骋岁月;以梦为马,诗酒趁年华!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个 “推荐” 哦,博主在此感谢!