java boot tomcat,Spring Boot 教程:Tomcat 部署

【注】本文译自:https://www.tutorialspoint.com/spring_boot/spring_boot_tomcat_deployment.htm

e057e62bd64c503a45766bad684142a8.png

使用 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 目录下找到它,如下图:

b3e284aed2ae6098987fac8908be877d.png

43f4762e4852e92537530eb63d02d979.png

对于 Gradle,用 gradle clean build 来打包应用。之后,会创建一个 WAR 文件,可以在 build/libs 目录下找到它,如下图:

a9a3eb6b3227bf2f3f746d911a31aa9e.png

43f4762e4852e92537530eb63d02d979.png

部署到 Tomcat

现在,运行 Tomcat 服务器,在 webapps 目录下部署 WAR 文件,如下图所示:

8c7df4b1c0672ee13856facafdb35cc6.png

4972e6eaeb5a038357fe46823e60a1c0.png

部署成功之后,在web 浏览器中敲入 http://localhost:8080/demo-0.0.1-SNAPSHOT/,屏幕截图如下:

b90f94d4f5dfb39e1c9c4d5fe40f01ba.png

全部代码如下:

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";

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值