SpringBoot打包war发布到外部的Tomcat容器后无法启动
tomcat启动成功,访问8080可以看到大猫,但是不启动springboot项目
原因是springboot在外部的容器启动(如tomcat)时启动类需要继承
SpringBootServletInitializer并重写configure方法
第一步:
package com.skxx.vrxmty;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
@Controller
public class VrxmtyApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(VrxmtyApplication.class, args);
}
//打成war包需要继承SpringBootServletInitializer并重写configure方法
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(VrxmtyApplication.class);
}
/**
* mvn打包后,如果运行容器容器不是tomcat(如果是tomcat则不用配,默认是tomcat),需要配置这里返回对应的EmbeddedServletContainerFactory
* 比如使用jetty容器应该用下面的配置
* @return
*/
/*@Bean
public EmbeddedServletContainerFactory servletContainer() {
JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory();
return factory;
}*/
@RequestMapping(value={"","/index"})
public String index(){
System.out.println("进入主页");
return "index";
}
}
第二步;注意你的pom文件的打包方式和去除默认的tomcat
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.skxx</groupId>
<artifactId>vrxmty</artifactId>
<packaging>war</packaging><!--外部启动tomcat时要以war方式打包-->
<version>0.0.1-SNAPSHOT</version>
<name>vrxmty</name>
<description>SkxxTyVr project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- provided 表明该包只在编译和测试的时候使用,去除默认的tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>8.5.43</version>
<scope>provided</scope>
</dependency>
<!--<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<fork>true</fork>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>