使用外部容器運行spring


前言:本項目基於maven構建

spring-boot項目可以快速構建web應用,其內置的tomcat容器也十分方便我們的測試運行;


spring-boot項目需要部署在外部容器中的時候,spring-boot導出的war包無法再外部容器(tomcat)中運行或運行報錯,本章就是詳細講解如何解決這個問題

1、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>項目名稱自行自行定義</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中引入該包



注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com