The SpringBoot project is deployed to the external tomcat in the form of a war package

Step 1: Inherit SpringBootServletInitializer

If the external container is deployed, you can't rely on the main function of Application, but start the Spring application context in a way similar to the web.xml file configuration. At this time, we need to inherit SpringBootServletInitializer in the startup class and implement the configure method

package com.test.chaosblade;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

/**
 * @description:
 * @time: 2020-08-28 15:49
 */
@SpringBootApplication(exclude = {
    
     DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, SecurityAutoConfiguration.class })
public class ChaosbladeApplication extends SpringBootServletInitializer {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(ChaosbladeApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    
    
        return builder.sources(this.getClass());
        //此处的ChaosbladeApplication是springboot启动类
    }
}

Remarks:
This class is similar to the configuration of the listener responsible for initializing the Spring application context in web.xml, but it avoids the trouble of writing additional XML files

Step 2: modify tomcat related configuration in pom.xml

If you want to wrap the project into a war package and deploy it to an external tomcat, you need to modify the pom.xml file. The introduced spring-boot-starter-web contains an embedded tomcat container. If it is directly deployed in an external tomcat, it will cause conflicts and errors. To solve this problem, the following two methods can be used:

Method 1: Exclude
the war package that is typed out like embedded tomcat , and the lib directory will not contain the Tomcat-related jar package

<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>

Since SpringBootServletInitializer needs to rely on javax.servlet, there is javax.servlet in tomcat-embed-core under tomcat-embed-jasper. The scope in tomcat-embed-jasper must be provided, and the function of provided can be found in my XXXX article. If provided is not used, then the servlet-api jar will be included in the final war, which will conflict with the tomcat itself.

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

Method 2: Increase the configuration
spring-boot-starter-tomcat is configured by Spring Boot by default. The purpose of setting it to provided is to exclude the package (dependency) during packaging

<!--部署成war包时开启【同时支持war和jar启动部署】↓↓↓↓-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>                
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>
<!--部署成war包时开启【同时支持war和jar启动部署】↑↑↑↑-->

Step 3: Pom.xml settings to fight war package

<packaging>war</packaging>

⚠️Note:

  1. The project name and package name need to be consistent.
    If they are inconsistent, the context will change after the project is published to the webapps directory
<build>
    <finalName>demo</finalName>
</build>

Guess you like

Origin blog.csdn.net/sinat_34241861/article/details/115290040