1. 程式人生 > 程式設計 >SpringBoot去除內嵌tomcat的實現

SpringBoot去除內嵌tomcat的實現

SpringBoot內嵌tomcat,直接run Application即可,那麼我們如何去除內嵌的tomcat,使用自己的呢?

一、POM(去除內嵌tomcat後,需要新增servlet依賴)

   <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   <!-- 去除內嵌tomcat -->
   <exclusions>
    <exclusion>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-tomcat</artifactId>
    </exclusion>
   </exclusions>
  </dependency>
  <!--新增servlet的依賴-->
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <version>3.1.0</version>
   <scope>provided</scope>
  </dependency>

   <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.0.0</version>
   </plugin>

打包方式設定成war

<packaging>war</packaging>

二、繼承SpringBootServletInitializer重寫configure方法

package com.example.export;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class ExportApplication extends SpringBootServletInitializer {
 public static void main(String[] args) {
 SpringApplication.run(ExportApplication.class,args);
 }

 @Override
 protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
 return builder.sources(this.getClass());
 }

}

三、新增到tomcat容器、run 即可

到此這篇關於SpringBoot去除內嵌tomcat的實現的文章就介紹到這了,更多相關SpringBoot去除內嵌tomcat內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!