1. 程式人生 > >spring boot 外接tomcat 獨立部署

spring boot 外接tomcat 獨立部署

1.pom檔案中spring-boot-starter-web maven依賴排除tomcat,新增依賴javax.servlet-api
2.啟動Application 中繼承SpringBootServletInitializer類,重寫configure方法,
  return builder.sources(Application.class);

以下為具體程式碼:

pom.xml:

 


<!-- 使用外部tomcat容器部署 -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   <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>
   <version>${servlet.version}</version>
   <scope>provided</scope>
</dependency>

 Application.java:




package com.ex.gays;

import org.mybatis.spring.annotation.MapperScan;
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 GaysApplication extends SpringBootServletInitializer{

   @Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
      return builder.sources(GaysApplication.class);
   }

   public static void main(String[] args) {
      SpringApplication.run(GaysApplication.class, args);
   }

}