让SpringBoot应用同时可以嵌入式和部署到Tomcat容器来运行详解

2021-01-06 10:25:37 浏览数 (1)

让SpringBoot应用同时可以嵌入式和部署到Tomcat容器来运行详解

使用 Spring Boot 应用,我们可以jar方式启动,可以创建一个war文件部署到web服务器中。

扩展SpringBootServletInitializer类

在原本注解为@SpringBootApplication的Spring Boot App上扩展一个SpringBootServletInitializer 类。Spring Boot Servlet Initializer 类允许你的SpringBoot应用在使用 Web容器启动时配置应用。示例如下:

代码语言:javascript复制
package org.wjw.sboot;

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(FavoritesApplication.class);
  }

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

设置主类

在 Spring Boot 中,我们需要在构件文件中指定主类。可使用下面的代码片段:

对于Maven,在 pom.xml 中增加启动类属性,如下所示:

代码语言:javascript复制
<start-class>org.wjw.sboot.DemoApplication</start-class>

对于Gradle,在 build.gradle 中加入主类名,如下所示:

代码语言:javascript复制
springBoot {
  mainClass = "org.wjw.sboot.DemoApplication"
}

加入打包到war中

对于Mavenpom.xml中打包 WAR 的插件如下:

代码语言:javascript复制
<packaging>war</packaging>

对于Gradle,在build.gradle中加上 应用 war 插件,如下所示:

代码语言:javascript复制
apply plugin: 'war'

编写Rest 端点

现在,让我们编写一个简单的 Rest 端点,返回 “Hello World from Spring Boot” 字符串。要编写 Rest Endpoint,我们要在构建文件中加上 Spring Boot web starter 依赖。

对于Maven,在pom.xml中加上 Spring Boot starter 依赖,如下所示:

代码语言:javascript复制
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

对于 Gradle,在 build.gradle 中加上 Spring Boot starter 依赖,如下所示:

代码语言:javascript复制
dependencies {
  compile("org.springframework.boot:spring-boot-starter-web")
}

现在,在 Spring Boot 应用类文件中编写 Rest 端点,示例代码如下:

代码语言:javascript复制
package org.wjw.sboot;

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;


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

  @RequestMapping(value = "/")
  public String hello() {
    return "Hello World from Spring Boot";
  }
}

打包应用

使用Maven 或 Gradle 命令将应用打包成独立的自包含Jar文件和可以部署到Tomcat服务器的war文件:

对于Maven,用 mvn package 来打包应用。之后,会创建一个war文件,可以在 target 目录下找到它

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

全部构建配置文件

pom.xml

代码语言:javascript复制
<?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>org.wjw.sboot</groupId>
  <artifactId>demo</artifactId>
  <version>>1.0.0</version>
  
  <packaging>war</packaging>
  <packaging>jar</packaging>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.8.RELEASE</version>
    <relativePath/>
  </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>org.wjw.sboot.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

代码语言:javascript复制
buildscript {
  ext {
    springBootVersion = '1.5.8.RELEASE'
  }
  
  repositories {
    maven { url "http://repo2.maven.org/maven2" }
    mavenLocal()
  }
  dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
  }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
//apply plugin: 'idea'
//apply plugin: 'eclipse'

war {
  baseName = 'demo'
  version =  '1.0.0'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
  mavenCentral()
  maven { url "http://repo2.maven.org/maven2" }
}

dependencies {
    compile "org.springframework.boot:spring-boot-starter-web:${springBootVersion}"
    compile 'org.springframework.boot:spring-boot-starter-tomcat'
}

bootRun {
  addResources = true
}

springBoot {
  mainClass = "org.wjw.sboot.DemoApplication"
}

参考:

  1. https://segmentfault.com/a/1190000023502713
  2. https://www.jb51.net/article/136912.htm

0 人点赞