Spring Boot Tomcat 部署



  • Tomcat 部署

    通过使用Spring Boot应用程序,我们可以创建一个war文件来部署到Web服务器中。在本章中,您将学习如何创建WAR文件以及如何在Tomcat Web服务器中部署Spring Boot应用程序。
  • Spring Boot Servlet初始化器

    传统的部署方式是让Spring引导应用程序SpringBootApplication类扩展SpringBootServletInitializer类。Spring Boot Servlet初始化器类文件允许您在使用Servlet容器启动应用程序时配置它。下面给出了用于JAR文件部署的Spring Boot Application类文件的代码-
    
    package com.jc2182.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 Application类文件的代码如下
    
    package com.jc2182.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类,如下所示-
    
    <start-class>com.jc2182.demo.DemoApplication</start-class>
    
    
    对于Gradle,在build.gradle中添加主类名称,如下所示-
    
    mainClassName="com.tutorialspoint.demo.DemoApplication"
    
    
  • 将JAR包更新为WAR包

    我们必须使用以下代码将打包的JAR更新为WAR- 对于Maven,将包装作为WAR添加到pom.xml中,如下所示
     
    <packaging>war</packaging>
    
    
    对于Gradle,在build.gradle中添加application插件和war插件,如下所示-
     
    apply plugin: ‘war’
    apply plugin: ‘application’
    
    
    现在,让我们编写一个简单的Rest Endpoint来返回字符串“Hello World from Tomcat”。要编写一个Rest Endpoint,我们需要将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启动器依赖项-
     
    dependencies {
       compile('org.springframework.boot:spring-boot-starter-web')
    }
    
    
    现在,使用如下所示的代码在Spring Boot Application类文件中编写一个简单的Rest Endpoint-
     
    package com.jc2182.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服务器,并将WAR文件部署在webapps目录下。 成功部署后,在Web浏览器中单击URL 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
            <modelVersion>4.0.0</modelVersion>
            <parent>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-parent</artifactId>
                    <version>2.3.0.BUILD-SNAPSHOT</version>
                    <relativePath/> <!-- lookup parent from repository -->
            </parent>
            <groupId>com.jc2182</groupId>
            <artifactId>demo</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <packaging>war</packaging>
            <name>demo</name>
            <description>Demo project for Spring Boot</description>
    
            <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.jc2182.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>
                            <exclusions>
                                    <exclusion>
                                            <groupId>org.junit.vintage</groupId>
                                            <artifactId>junit-vintage-engine</artifactId>
                                    </exclusion>
                            </exclusions>
                    </dependency>
            </dependencies>
    
            <build>
                    <plugins>
                            <plugin>
                                    <groupId>org.springframework.boot</groupId>
                                    <artifactId>spring-boot-maven-plugin</artifactId>
                            </plugin>
                    </plugins>
            </build>
    
            <repositories>
                    <repository>
                            <id>spring-milestones</id>
                            <name>Spring Milestones</name>
                            <url>https://repo.spring.io/milestone</url>
                    </repository>
                    <repository>
                            <id>spring-snapshots</id>
                            <name>Spring Snapshots</name>
                            <url>https://repo.spring.io/snapshot</url>
                            <snapshots>
                                    <enabled>true</enabled>
                            </snapshots>
                    </repository>
            </repositories>
            <pluginRepositories>
                    <pluginRepository>
                            <id>spring-milestones</id>
                            <name>Spring Milestones</name>
                            <url>https://repo.spring.io/milestone</url>
                    </pluginRepository>
                    <pluginRepository>
                            <id>spring-snapshots</id>
                            <name>Spring Snapshots</name>
                            <url>https://repo.spring.io/snapshot</url>
                            <snapshots>
                                    <enabled>true</enabled>
                            </snapshots>
                    </pluginRepository>
            </pluginRepositories>
    
    </project>
    
    
    build.gradle
    
    plugins {
            id 'org.springframework.boot' version '2.3.0.BUILD-SNAPSHOT'
            id 'io.spring.dependency-management' version '1.0.9.RELEASE'
            id 'java'
            id 'war'
            id 'application'
    }
    
    group = 'com.jc2182'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = '1.8'
    mainClassName = "com.jc2182.demo.DemoApplication"
    
    repositories {
            mavenCentral()
            maven { url 'https://repo.spring.io/milestone' }
            maven { url 'https://repo.spring.io/snapshot' }
    }
    
    dependencies {
            implementation 'org.springframework.boot:spring-boot-starter-web'
            testImplementation('org.springframework.boot:spring-boot-starter-test') {
                    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
            }
    }
    
    test {
            useJUnitPlatform()
    }
    
    
    下面给出了主要的Spring Boot应用程序类文件的代码-
    
    package com.jc2182.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";
       }
    }