spring boot using tomcat start

In springboot, the embedded tomcat server, use the main function to start the project by default. When we want to use your own tomcat to start the project how to do?

 1, tomcat war running inside the container, and the default spring boot is generated jar package, first it is necessary to change the way war packetizing jar package

Add in the pom file

<packaging>war</packaging>

2, springboot tomcat which will built a vessel, the vessel tomcat added if an error occurs, so first remove built tomcat

Add in the pom file

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>        

Or pom file

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>    

Replaced with the following code

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

3, add servlet dependence

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>

4, edit start classes in the spring boot program is started by the boot main method, and is activated by using an external tomcat web.xml manner, so to modify the startup class

@SpringBootApplication
public class TestApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder springApplicationBuilder){
        return springApplicationBuilder.sources(TestApplication.class);
    }
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}

5, in the run maven install, package, wrapped in looking for war target folder, put the tomcat

Guess you like

Origin www.cnblogs.com/sunshine-2018/p/11274127.html