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