Spring Boot将WAR文件部署到Tomcat
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot将WAR文件部署到Tomcat相关的知识,希望对你有一定的参考价值。
在本文中,将演示如何将Spring Boot WAR文件部署到Tomcat servlet容器中。
对于Spring Boot WAR部署,需要执行三个步骤:
- 扩展
SpringBootServletInitializer
- 根据提供标记嵌入式servlet容器。
- 更新包装为
War
测试工具:
- Spring Boot 1.4.2.RELEASE
- Tomcat 8.5.9
- Maven 3
注意
在Spring Boot中,具有嵌入服务器解决方案的最终可执行JAR
文件可能不适合所有生产环境,特别是部署团队(具有良好的服务器优化和监控技能知识,但缺乏开发经验的团队),他们希望完全控制服务器,并且不能接触代码。
1. 推展 SpringBootServletInitializer
使现有的 @SpringBootApplication
类扩展 SpringBootServletInitializer
1.1 - 经典Spring Boot JAR部署(更新此文件以支持WAR部署)。
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 部署
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);
}
}*/
如果要为部署创建了一个额外的SpringBootWebApplication
类,请确保告诉Spring Boot要从哪个主类开始启动程序:
在 pom.xml
文件中增加以下内容指定 -
<properties>
<start-class>com.yiibai.NewSpringBootWebApplicationForWAR</start-class>
</properties>
2.提供标记嵌入式servlet容器
在 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.更新包装为War
在 pom.xml
文件中增加以下内容指定 -
<packaging>war</packaging>
完成,mvn
打包并将$project/target/xxx.war
复制到Tomcat
发布目录进行部署。
4. 完整的Spring Boot WAR + Tomcat部署
4.1 - 以Spring Boot Thymeleaf
为例,更新它并手动部署到Tomcat。
4.2 - 更新现有的SpringBootWebApplication
并让它扩展SpringBootServletInitializer
类。
SpringBootWebApplication.java
文件的完整代码如下所示 -
package com.yiibai;
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);
}
}
4.3 - 更新打包包,并按提供的标记spring-boot-starter-tomcat
。pom.xml
文件的完整代码如下所示 -
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-web-thymeleaf</artifactId>
<packaging>war</packaging>
<name>Spring Boot Web Thymeleaf Example</name>
<description>Spring Boot Web Thymeleaf Example</description>
<url>http://www.yiibai.com</url>
<version>1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<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>
<!-- hot swapping, disable cache for template, enable live reload -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- Optional, for bootstrap -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7