其他分享
首页 > 其他分享> > IntellJ IDEA下SpringBoot项目启用本地Tomcat

IntellJ IDEA下SpringBoot项目启用本地Tomcat

作者:互联网

1、修改pom.xml文件,配置<packaging>war</packaging>,使其发布的时候打包成war包

<groupId>com.owlforest</groupId>
<artifactId>home</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>home</name>
<packaging>war</packaging>
<description>home of linfu</description>

2、修改pom.xml文件,移除tomcat插件

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!-- 移除嵌入式tomcat插件 -->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
    </exclusions>
</dependency>

3、修改SpringBoot启动类

3.1修改类定义,使其继承自SpringBootServletInitializer

3.2重写configure方法

原代码

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

修改后

@SpringBootApplication
public class HomeApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // 注意这里要指向原先用main方法执行的Application启动类
        return builder.sources(HomeApplication.class);
    }

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

}

4、在IDEA中配置本地Tomcat即可

添加本地Tomcat,配置Server与Deployment两项

Server配置

Deployment配置

 5、启动测试

启动选项改为刚刚新增的Tomcat,启动即可

 

标签:SpringBoot,Tomcat,IntellJ,args,boot,public,HomeApplication,class
来源: https://www.cnblogs.com/wsfu/p/10355922.html