Spring Boot: WEB project, deployed in WAR package to the external form of tomcat deployment

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/fly910905/article/details/90519424

Configuration steps:

1, inheritance SpringBootServletInitializer

  • Deployment outside of the container, then it can not rely on the Application of the main function, but rather in a manner similar to the web.xml configuration file to start the Spring application context , then we need to start classes in the inheritance SpringBootServletInitializer configure and implement methods:

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 SdkCmsApplication extends SpringBootServletInitializer {
    /**
     * @Title: 使用外置tomcat部署
     * @MethodName:  configure
     * @param application
     * @Return org.springframework.boot.builder.SpringApplicationBuilder
     * @Exception
     * @Description:
     *
     * @author: FLY
     * @date:  2019-05-24 14:02
     */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

        return application.sources(SdkCmsApplication.class);
    }

    public static void main(String[] args) {

        SpringApplication.run(SdkCmsApplication.class, args);
    }

}

 

  • The role of this type of configuration is responsible for initializing the Spring application context in web.xml listener role similar, but there is no need to write in additional XML files.

 2, pom.xml modify the relevant configuration tomcat

It introduces the concept of the scope of the dependency maven scope, for the follow-up involves a problem.

Dependent relationship range is used and three kinds of control relies CLASSPATH (compiler classpath, test classpath, run CLASSPATH) is, Maven there are several dependent range is as follows:

  • compile: Compile dependent range . If not specified, the default will depend on the scope. Use this dependence range Maven dependency to compile, test, Operation of Three classpath are valid. A typical example is the spring-code, need to use the dependency in the time to compile, test, and run.

  • test: testing relies range . Use time-dependent range of Maven dependency is valid only for the test classpath, you can not use this relies upon the use of compiled code or run the main project. A typical example is Jnuit, it is only required to compile and test code when running tests .

  • provided: provided dependent range . Use this dependence range Maven dependency effective for compiling and testing classpath, but not in time to run . A typical example is the servlet-api, compile and test items that rely when needed, but when the program is running, since the container and to provide, there is no need to repeatedly Maven introduced again.

  • More scope description, reference: https://blog.csdn.net/fly910905/article/details/78089359

If you want to change the form of the final package of war you would also need to modify the pom.xml file because spring-boot-starter-web contains embedded tomcat container, so the error directly deployed outside the container conflict.

There are two ways to resolve, as follows

method one:

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

Here need to remove the dependency on embedded Tomcat, so play the war package, it will not in the lib directory jar package contains Tomcat-related, or startup errors will occur.

There is also a critical key point is that tomcat-embed-jasper in scope must be provided.

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

Because SpringBootServletInitializer need to rely on javax.servlet, but tomcat-embed-jasper following tomcat-embed-core in there this javax.servlet, if useless provided, there will be the final fight of the war this servlet-api jar, so conflict with the tomcat itself .

The key point is also adapted to the second method the following to say.

Method Two:

It can directly add the following configuration:

spring-boot-starter-tomcat will be the default configuration of the Boot Spring, Spring embedded tomcat i.e. the Boot and set it to be provided at the time of packing the packet (dependent) negative, because on separate the tomcat run, Spring Boot embedded Tomcat is not need to use the .

        <!--部署成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启动部署】↑↑↑↑-->

The above description has been provided of the role is very thorough, and there is not long-winded,

The advantage of this approach is that packaged the war package suitable for both java -jar command to start and deploy to an external container.

3, a jar become war

<packaging>war</packaging>

4, attention to the problem

4.1, the project name and package name of consistency

At this time the name of the package should be labeled and the application.properties 

server.context-path=/sdk-cms

Note SpringBooot2.x, as written here server.servlet.context-path =

be consistent

<build>
    <finalName>sdk-cms</finalName>
</build>

If you do not release the webapps to tomcat context will change as

4.2, Tomcat version

SpringBoot2.x, please use the above version of Tomcat8.5

The default Tomcat version 9.0.14 is SpringBoot2.1.2
SpringBoot1.5.6 default Tomcat version is 8.5.16

Guess you like

Origin blog.csdn.net/fly910905/article/details/90519424