SpringBoot工程使用本地Tomcat发布服务

这是我在第一家面试的时候,面试官问我的问题,当时我没有回答出来,所以回来百度了一下,并且自己实操一下,步骤很简单。

一、修改打包方式

springboot项目默认的打包方式是打成jar包,所有我们要在pom文件里改成war包形式打包

<packaging>war</packaging>

二、移除自带的Tomcat

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!--排除springBoot默认的logBack依赖包-->
            <!--题外话:真正引入spring-boot-starter-logging包的是spring-boot-starter依赖,所以                        我们也可以直接在spring-boot-starter下排除即可-->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
                <!--移除tomcat插件,将项目部署到本地tomcat-->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

三、添加servlet依赖

        <!--第一种 将项目部署到本地tomcat-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!--可选项     第二种 将项目部署到本地tomcat-->
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-servlet-api</artifactId>
            <version>8.0.36</version>
            <scope>provided</scope>
        </dependency>

四、重写主函数

类继承SpringBootServletInitializer,重写configure方法

package com.leo.vhr;

import org.mybatis.spring.annotation.MapperScan;
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.context.WebApplicationContext;

@MapperScan(basePackages = "com.leo.vhr.mapper")
public class LearnApplication extends SpringBootServletInitializer
{
    
    
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder)
    {
    
    
        return builder.sources(LearnApplication.class);
    }
}

这里可以再说一下idea打包的方式,操作很简单

在这里插入图片描述
执行命令:

java -jar jar包名

Author By 朝花不迟暮

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Curtisjia/article/details/105000008