共计 6030 个字符,预计需要花费 16 分钟才能阅读完成。
【注】本文译自: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 中减少启动类属性,如下所示:
<start-class>com.tutorialspoint.demo.DemoApplication</start-class>
对于 Gradle,在 build.gradle 中退出主类名,如下所示:
mainClassName=”com.tutorialspoint.demo.DemoApplication”
更新 JAR 包到 WAR 中
咱们还要用上面的代码片段更新 JAR 包到 WAR 中:
对于 Maven,pom.xml 中打包 WAR 的插件如下:
<packaging>war</packaging>
对于 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>
对于 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
<?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.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”;
}
}