springboot打包war部署至tomcat

时间:2022-08-27 09:57:06

参考文章在这里

1、把pom.xml文件中打包结果由jar改成war:

<modelVersion>4.0.0</modelVersion>  
<groupId>spring-boot-panminlan-mybatis-test</groupId>  
<artifactId>mybatis-test</artifactId>  
<packaging>war</packaging>  
<version>0.0.1-SNAPSHOT</version> 

2、排除org.springframework.boot依赖中的tomcat内置容器

<dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-web</artifactId>  
        <exclusions>  
            <exclusion>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-starter-tomcat</artifactId>  
            </exclusion>  
        </exclusions>  
    </dependency> 
3、添加对servlet API的依赖

<dependency>  
            <groupId>javax.servlet</groupId>  
            <artifactId>javax.servlet-api</artifactId>  
        </dependency> 
4、继承SpringBootServletInitializer ,并覆盖它的 configure 方法,如果我们构建的是war包并部署到外部tomcat则需要使用它,如果使用内置servlet容器则不需要,外置tomcat环境的配置需要这个类的configure方法来指定初始化资源。
@SpringBootApplication
public class GuideserverApplication extends SpringBootServletInitializer {

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

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(GuideserverApplication.class);
	}
}