zoukankan      html  css  js  c++  java
  • Spring Boot 项目发布到 Tomcat 服务器

    第 1 步:将这个 Spring Boot 项目的打包方式设置为 war。
    <packaging>war</packaging>

    SpringBoot 默认有内嵌的 tomcat 模块,因此,我们要把这一部分排除掉。
    即:我们在 spring-boot-starter-web 里面排除了 spring-boot-starter-tomcat ,但是我们为了在本机测试方便,我们还要引入它,所以我们这样写:
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <artifactId>log4j-over-slf4j</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    第 2 步:提供一个 SpringBootServletInitializer 子类,并覆盖它的 configure 方法。我们可以把应用的主类改为继承 SpringBootServletInitializer。或者另外写一个类。
    @SpringBootApplication
    public class Application extends SpringBootServletInitializer {
        /**
         * 如果要发布到自己的Tomcat中的时候,需要继承SpringBootServletInitializer类,并且增加如下的configure方法。
         * 如果不发布到自己的Tomcat中的时候,就无需上述的步骤
         */
        @Override
        protected SpringApplicationBuilder configure(
                SpringApplicationBuilder application) {
            return application.sources(Application.class);
        }

        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    第3步:测试
    使用内嵌的Tomcat测试:
    进入项目的根目录,输入mvn spring-boot:run,开启项目。
    在浏览器中输入
    http://localhost:8080/stumybatis/likename?name=m
    结果如下:
    [{"id":1,"name":"tom","sumScore":null,"avgScore":null,"age":25}]

    使用发布到自己的Tomcat测试:
    先进入项目根目录,执行 mvn package ,将项目打包为war包。
    然后将target 目录中的SpringBootTest-0.0.1-SNAPSHOT.war文件复制到你所安装的tomcat目录中webapps目录中。然后去掉工程后边的后缀。
    然后开启Tomcat,并在浏览器中输入:http://localhost:8088/SpringBootTest/stumybatis/likename?name=m(我的tomcat的端口修改为了8088)
    结果如下:
    [{"id":1,"name":"tom","sumScore":null,"avgScore":null,"age":25}]

  • 相关阅读:
    IServiceBehavior, IOperationBehavior,IParameterInspector
    System.IO.Pipelines——高性能IO(三)
    System.IO.Pipelines——高性能IO(二)
    System.IO.Pipelines——高性能IO(一)
    背包问题 —— 四种解法解题
    波音,自动驾驶bug未修复,致346人丧生!5个月内两次坠毁!其中,包括8名中国公民
    2018年Java生态行业报告
    为什么大公司一定要使用DevOps?
    设计微服务的最佳实践
    Spring Boot面试题
  • 原文地址:https://www.cnblogs.com/web424/p/6757814.html
Copyright © 2011-2022 走看看