Spring Boot Tomcat部署

通過使用Spring Boot應用程序,可以創建一個war文件以部署到Web服務器中。在本章中,將學習如何創建WAR文件並在Tomcat Web服務器中部署Spring Boot應用程序。

Spring Boot Servlet初始化程序

傳統的部署方式是使Spring Boot應用程序[@SpringBootApplication](https://github.com/SpringBootApplication "@SpringBootApplication")類擴展SpringBootServletInitializer類。 SpringBootServletInitializer類文件允許在使用Servlet容器啓動時配置應用程序。

下面給出了用於JAR文件部署的Spring Boot應用程序類文件的代碼 -

package com.yiibai.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
}

需要擴展類SpringBootServletInitializer以支持WAR文件部署。 Spring Boot應用程序類文件的代碼如下 -

package com.yiibai.demo;

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 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);
   }
}

設置Main類

在Spring Boot中,需要在構建文件中指定啓動的主類。
對於Maven,在pom.xml屬性中添加start類,如下所示 -

<start-class>com.yiibai.demo.DemoApplication</start-class>

對於Gradle,在build.gradle中添加主類名,如下所示 -

mainClassName="com.yiibai.demo.DemoApplication"

將打包JAR更新爲WAR

使用以下代碼將包裝JAR更新爲WAR。

對於Maven,在pom.xml 中將包裝添加爲WAR,如下所示 -

<packaging>war</packaging>

對於Gradle,在build.gradle 中添加應用程序插件和war插件,如下所示 -

apply plugin: 'war'
apply plugin: 'application'

對於GradlNow,編寫一個簡單的Rest端點來返回字符串:"Hello World from Tomcat"。 要編寫Rest端點,需要將Spring Boot Web starter依賴項添加到構建文件中。

對於Maven,使用如下所示的代碼在pom.xml 中添加Spring Boot啓動程序依賴項 -

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

對於Gradle,使用如下所示的代碼在build.gradle 中添加Spring Boot starter依賴項 -

dependencies {
   compile('org.springframework.boot:spring-boot-starter-web')
}

現在,使用如下所示的代碼在Spring Boot Application類文件中編寫一個簡單的Rest端點 -

package com.yiibai.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public 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文件,可以在目標目錄中找到它,如下面給出的屏幕截圖所示 -

Spring Boot Tomcat部署

對於Gradle,使用命令gradle clean build打包應用程序。 然後,將創建WAR文件,可以在build/libs目錄下找到它。觀察此處給出的屏幕截圖以便更好地理解 -

Spring Boot Tomcat部署

部署到Tomcat

現在,運行Tomcat服務器,並在webapps目錄下部署WAR文件。觀察此處顯示的屏幕截圖以便更好地理解 -

Spring Boot Tomcat部署

成功部署後,點擊網頁瀏覽器中的URL => http://localhost:8080/demo-0.0.1-SNAPSHOT/,觀察輸出結果如下圖所示 -

Spring Boot 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>

   <groupId>com.yiibai</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.yiibai.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.yiibai'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
mainClassName = "com.yiibai.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.yiibai.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public 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";
   }
}