Spring Boot將WAR文件部署到Tomcat

在本文中,將演示如何將Spring Boot WAR文件部署到Tomcat servlet容器中。

對於Spring Boot WAR部署,需要執行三個步驟:

  1. 擴展SpringBootServletInitializer
  2. 根據提供標記嵌入式servlet容器。
  3. 更新包裝爲 War

測試工具:

  • Spring Boot 1.4.2.RELEASE
  • Tomcat 8.5.9
  • Maven 3

注意
在Spring Boot中,具有嵌入服務器解決方案的最終可執行JAR文件可能不適合所有生產環境,特別是部署團隊(具有良好的服務器優化和監控技能知識,但缺乏開發經驗的團隊),他們希望完全控制服務器,並且不能接觸代碼。

1. 推展 SpringBootServletInitializer

使現有的 [@SpringBootApplication](https://github.com/SpringBootApplication "@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</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <!-- Package as an executable jar/war -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

4.4 - 這是可選的,將contextPath更新爲/yiibai以方便稍後作爲演示。準備好WAR部署文件。

application.properties 文件的完整內容如下所示 -

welcome.message: Hello Yiibai

server.contextPath=/yiibai

4.5 - 獲取Tomcat並部署WAR文件,執行以下命令 -

F:\worksp\springboot\spring-boot-war-tomcat>mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Spring Boot Web Thymeleaf Example 1.0
[INFO] ------------------------------------------------------------------------
Downloading: http://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-thymeleaf/1.4.2.RELEASE/spring-boot-starter-thymeleaf-1.4.2.RELEASE.pom
Downloaded: http://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-thymeleaf/1.4.2.RELEASE/spring-boot-starter-thymeleaf-1.4.2.RELEASE.pom (2 KB at 0.7 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/thymeleaf/thymeleaf-spring4/2.1.5.RELEASE/thymeleaf-spring4-2.1.5.RELEASE.pom
....
.... ...

將生成的 spring-boot-web-thymeleaf-1.0.war 拷貝到 Tomcat 發佈目錄下 -

Spring

例如,寫本文章時,Tomcat 發佈目錄使用的是 - F:\worksp\springmvc\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\webapps

4.6 - 現在啓動 Tomcat,訪問:http://localhost:8080/yiibai/ ,程序沒有錯誤問題,應該會看到以下結果 -

Spring