Preface:
This project is built based on Maven.
The Spring-Boot project can quickly build web applications, and its built-in Tomcat container is very convenient for our test runs. When the Spring-Boot project needs to be deployed in an external container, the war package exported by spring-Boot cannot run in the external container (Tomcat) or an error occurs. This chapter explains how to solve this problem in detail.
1. Overview of POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>The project name shall be defined by yourself</groupId>
<artifactId>The project name is customized</artifactId>
<version>1.1.2 - the SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0. RELEASE</version>
</parent>
<dependencies>
<! -- spring-boot web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<! War package can be exported to external containers to run spring-boot projects.
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<! -- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<! -- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<! -- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
Copy the code
2. Exclude the tomcat built-in container in the org.springFramework. boot dependency
Note: External containers can only run spring-boot projects if built-in containers are excluded
In org.SpringFramework. boot, do not exclude the built-in container during the test. As a result, SpringBoot cannot be tested
<! -- spring-boot web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<! War package can be exported to external containers to run spring-boot projects.
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
Copy the code
3, implement SpringBootServletInitializer interface
Spring – the boot entry class must implement SpringBootServletInitializer the configure method of the interface to external container running spring – the boot program
Note: SpringBootServletInitializer interfaces need to rely on javax.mail. Servlet
package cn.eguid.Run;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import cc.eguid.livepush.PushManager;
import cc.eguid.livepush.PushManagerImpl;
import cn.eguid.livePushServer.redisManager.RedisMQHandler;
@SpringBootApplication
// Enable general annotation scanning
@ComponentScan
public class Application extends SpringBootServletInitializer {
/ * * * implementation SpringBootServletInitializer can let spring - the boot program running in the web container * /
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
builder.sources(this.getClass());
return super.configure(builder);
}
public static void main(String[] args) { SpringApplication.run(Application.class, args); }}Copy the code
Conclusion:
The external container running the Spring-Boot project only needs to do two things on the original project
1. Exclude the built-in Tomcat container for org.springFramework. boot from pom.xml
2, spring – the boot entry implementation SpringBootServletInitializer interface added: rely on javax.mail SpringBootServletInitializer interface. The servlet package, need in pom. XML is introduced in the package