【注】本文译自: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;
@SpringBootApplication
public 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;
@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);
}
}
设置主类
在 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 依赖,如下所示:
org.springframework.boot
spring-boot-starter-web
对于 Gradle,在 build.gradle 中加上 Spring Boot starter 依赖,如下所示:
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
}
现在,在 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
@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 文件,可以在 target 目录下找到它,如下图:
对于 Gradle,用 gradle clean build 来打包应用。之后,会创建一个 WAR 文件,可以在 build/libs 目录下找到它,如下图:
部署到 Tomcat
现在,运行 Tomcat 服务器,在 webapps 目录下部署 WAR 文件,如下图所示:
部署成功之后,在web 浏览器中敲入 http://localhost:8080/demo-0.0.1-SNAPSHOT/,屏幕截图如下:
全部代码如下:
pom.xml
4.0.0
com.tutorialspoint
demo
0.0.1-SNAPSHOT
war
demo
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.8.RELEASE
UTF-8
UTF-8
1.8
com.tutorialspoint.demo.DemoApplication
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
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.8
mainClassName = "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
@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";
}
}