使用外部容器運行spring-boot項目:不使用spring-boot內置容器讓spring-boot項目運行在外部tomcat容器中
來源:程序員人生 發布時間:2016-10-04 12:05:08 閱讀次數:2715次
前言:本項目基于maven構建
spring-boot項目可以快速構建web利用,其內置的tomcat容器也10分方便我們的測試運行;
spring-boot項目需要部署在外部容器中的時候,spring-boot導出的war包沒法再外部容器(tomcat)中運行或運行報錯,本章就是詳細講授如何解決這個問題
1、pom.xml1覽
<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⑷.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>項目名稱自行自行定義</groupId>
<artifactId>項目名稱自行定義</artifactId>
<version>1.1.2-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包可讓外部容器運行spring-boot項目-->
<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>
2、排除org.springframework.boot依賴中的tomcat內置容器
注意:只有排除內置容器,才能讓外部容器運行spring-boot項目
org.springframework.boot依賴中的排除項,測試時不要排除內置容器,會致使springboot沒法測試運行,導出war包時再加上該項便可
<!-- spring-boot web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 排除內置容器,排除內置容器導出成war包可讓外部容器運行spring-boot項目-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
3、實現SpringBootServletInitializer接口
spring-boot入口類必須實現SpringBootServletInitializer接口的configure方法才能讓外部容器運行spring-boot項目
注意:SpringBootServletInitializer接口需要依賴 javax.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
// 開啟通用注解掃描
@ComponentScan
public class Application extends SpringBootServletInitializer {
/**
* 實現SpringBootServletInitializer可讓spring-boot項目在web容器中運行
*/
@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);
}
}
總結:
外部容器運行spring-boot項目,只需要在原項目上做兩件事
1、在pom.xml中排除org.springframework.boot的內置tomcat容器
2、spring-boot入口實現SpringBootServletInitializer接口
補充:SpringBootServletInitializer接口依賴javax.servlet包,需要在pom.xml中引入該包
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈