1、pom.xml修改如下:
<packaging>war</packaging>
<!-- spring boot排除内嵌tomcat,由外部tomcat启动,
provided表示本地开发编译期间可用,依然可以使用main方法启动,但不会打到war包中 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
2、启动类Application继承SpringBootServletInitializer
覆盖方法:
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
我们一起来了解一下其中的原理
根据Servlet3.0规范,web应用启动时会实例化每个jar包中的ServletContainerInitializer提供者(SPI)。实现了ServletContainerInitializer的jar包会在META-INF/services目录下维护一个文件javax.servlet.ServletContainerInitializer,其内容指向ServletContainerInitializer的实现类。如spring-web中:
看看SpringServletContainerInitializer做了哪些事情:
@HandlesTypes注解表示SpringServletContainerInitializer能处理的类型。
SpringBootServletInitializer实现了WebApplicationInitializer