Spring Boot – Deploy WAR file to Tomcat

原文地址:https://www.mkyong.com/spring-boot/spring-boot-deploy-war-file-to-tomcat/

For Spring Boot WAR deployment, you need to do three steps:

  1. Extends SpringBootServletInitializer.
  2. Marked the embedded servlet container as provided.
  3. Update packaging to war.

1. Extends SpringBootServletInitializer

Make the existing @SpringBootApplication class extends SpringBootServletInitializer

1.1 Classic Spring Boot JAR deployment. (Update this file to support WAR deployment)

SpringBootWebApplication.java

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootWebApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootWebApplication.class, args);
    }

}
复制代码

1.2 Spring Boot WAR deployment.

SpringBootWebApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootWebApplication.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootWebApplication.class, args);
    }

}

/*@SpringBootApplication
public class SpringBootWebApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootWebApplication.class, args);
    }

}*/
复制代码

If you create a extra SpringBootWebApplication class for deployment, make sure tell Spring Boot which main class to start :

pom.xml

  <properties>
        <start-class>com.mkyong.NewSpringBootWebApplicationForWAR</start-class>
  </properties>
复制代码

2. Marked the embedded servlet container as provided

pom.xml

<dependencies>

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>

	<!-- marked the embedded servlet container as provided -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-tomcat</artifactId>
		<scope>provided</scope>
	</dependency>

</dependencies>
复制代码

3. Update packaging to war

pom.xml

  <packaging>war</packaging>
复制代码

Done, mvn package and copy the $project/target/xxx.war to Tomcat for deployment.

如果读完觉得有收获的话,欢迎点赞、关注、加公众号【牛觅技术】,查阅更多精彩历史!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值