Spring Boot 教程:Tomcat 部署

486 阅读2分钟
【注】本文译自:[https://www.tutorialspoint.com/spring_boot/spring_boot_tomcat_deployment.htm](https://www.tutorialspoint.com/spring_boot/spring_boot_tomcat_deployment.htm)
使用 Spring Boot 应用,我们可以创建一个 war 文件部署到 web 服务器中。本文讲解如何创建 war 文件并将 Spring Boot 应用部署到 Tomcat web 服务器中。

Spring Boot Servlet 初始化器

传统的部署方式是将 Spring Boot 应用中 @SpringBootApplication 注解的类扩展自 SpringBootServletInitializer 类。Spring Boot Servlet Initializer 类文件允许你在使用 Servlet 窗口启动时配置应用。
用于 JAR 文件部署的 Spring Boot 应用类文件示例如下:
package com.tutorialspoint.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class DemoApplication {   public static void main(String[] args) {      SpringApplication.run(DemoApplication.class, args);   }}
    我们需要扩展类 SpringBootServletInitializer 以支持 WAR 文件部署。对应的 Spring Boot 应用类文件如下:
package com.tutorialspoint.demo;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;@SpringBootApplicationpublic class DemoApplication  extends SpringBootServletInitializer {   @Override   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {      return application.sources(DemoApplication.class);   }   public static void main(String[] args) {      SpringApplication.run(DemoApplication.class, args);   }}

设置主类

在 Spring Boot 中,我们需要在构件文件中指定主类。可使用下面的代码片段:
对于 Maven,在 pom.xml 中增加启动类属性,如下所示:
com.tutorialspoint.demo.DemoApplication
对于Gradle,在 build.gradle 中加入主类名,如下所示:
mainClassName="com.tutorialspoint.demo.DemoApplication"

更新 JAR 包到 WAR 中

我们还要用下面的代码片段更新 JAR 包到 WAR 中:
对于 Maven,pom.xml 中打包 WAR 的插件如下:
war
对于 Gradle,在 build.gradle 中加上 应用和 war 插件,如下所示:
apply plugin: ‘war’apply plugin: ‘application’
   现在,让我们编写一个简单的 Rest 端点,返回 “Hello World from Tomcat” 字符串。要编写 Rest Endpoint,我们要在构建文件中加上 Spring Boot web starter 依赖。
对于 Maven,在 pom.xml 中加上 Spring Boot starter 依赖,如下所示:
<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-web</artifactId></dependency>
   现在,在 Spring Boot 应用类文件中编写 Rest 端点,示例代码如下:
package com.tutorialspoint.demo;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;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication@RestControllerpublic class DemoApplication extends SpringBootServletInitializer {   @Override   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {      return application.sources(DemoApplication.class);   }   public static void main(String[] args) {      SpringApplication.run(DemoApplication.class, args);   }      @RequestMapping(value = "/")   public String hello() {      return "Hello World from Tomcat";   }}

打包应用

如下所示,使用Maven 或 Gradle 命令将应用打包成 WAR 文件以部署到 Tomcat 服务器:
对于 Maven,用 mvn package 来打包应用。之后,会创建一个 WAR 文件,可以在 target 目录下找到它,如下图:

![](https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/cead9399edf842fd85305e685f9f7976~tplv-k3u1fbpfcp-zoom-1.image)
对于 Gradle,用 gradle clean build 来打包应用。之后,会创建一个 WAR 文件,可以在 build/libs 目录下找到它,如下图:
![](https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/2a9b65e25e5a4ee899f7a6d8e36ffd6b~tplv-k3u1fbpfcp-zoom-1.image)

部署到 Tomcat

现在,运行 Tomcat 服务器,在 webapps 目录下部署 WAR 文件,如下图所示:
![](https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6c5b9a4445ea46328beb1c7974d8946e~tplv-k3u1fbpfcp-zoom-1.image)

部署成功之后,在web 浏览器中敲入 [http://localhost:8080/demo-0.0.1-SNAPSHOT/](http://localhost:8080/demo-0.0.1-SNAPSHOT/),屏幕截图如下:
![](https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/8d7e4fb2164b44e3b2cead4d29cb1253~tplv-k3u1fbpfcp-zoom-1.image)
全部代码如下:

**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>

    <groupId>com.tutorialspoint</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <start-class>com.tutorialspoint.demo.DemoApplication</start-class>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
 **build.gradle**
buildscript {   ext {      springBootVersion = '1.5.8.RELEASE'   }   repositories {      mavenCentral()   }dependencies {      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")   }}apply plugin: 'java'apply plugin: 'eclipse'apply plugin: 'org.springframework.boot'apply plugin: 'war'apply plugin: 'application'group = 'com.tutorialspoint'version = '0.0.1-SNAPSHOT'sourceCompatibility = 1.8mainClassName = "com.tutorialspoint.demo.DemoApplication"repositories {   mavenCentral()}dependencies {   compile('org.springframework.boot:spring-boot-starter-web')   testCompile('org.springframework.boot:spring-boot-starter-test')}
    主 Spring Boot 应用类文件如下:
package com.tutorialspoint.demo;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;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication@RestControllerpublic class DemoApplication  extends SpringBootServletInitializer {   @Override   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {      return application.sources(DemoApplication.class);   }   public static void main(String[] args) {      SpringApplication.run(DemoApplication.class, args);   }      @RequestMapping(value = "/")   public String hello() {      return "Hello World from Tomcat";   }}