美文网首页
Spring Boot快速入门之(四):Tomcat 部署

Spring Boot快速入门之(四):Tomcat 部署

作者: 码者无疆 | 来源:发表于2020-08-10 13:45 被阅读0次

    【注】本文译自: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.0http://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";

       }

    }

    相关文章

      网友评论

          本文标题:Spring Boot快速入门之(四):Tomcat 部署

          本文链接:https://www.haomeiwen.com/subject/pjjnrktx.html