springboot转war放入tomcat

作者:故事我忘了
个人微信公众号:程序猿的月光宝盒

1. 修改pom文件,打包形式改为war

<packaging>war</packaging>

图片

2.移除内嵌的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>
		<!--引入tomcat依赖-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
            <!--只在编译时有效-->
			<scope>provided</scope>
		</dependency>
		<!--添加tomcat-servelt-api依赖-->
		<dependency>
			<groupId>org.apache.tomcat</groupId>
			<artifactId>tomcat-servlet-api</artifactId>
			<version>7.0.42</version>
            <!--只在编译时有效-->
			<scope>provided</scope>
		</dependency>

3.修改启动类,并重写configure 方法

在原本的启动类同级下创建ServletInitializer类,继承org.springframework.boot.web.servlet.support.SpringBootServletInitializer并重写configure 方法

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        // 指向启动类class
        return application.sources(DemoApplication.class);
    }

}

图片

这样就可以直接放在tomcat运行了

原文地址:https://www.cnblogs.com/jsccc520/p/13364923.html