The deployment method that Springboot packages into war and runs in tomcat

  • 2020-12-26 05:43:57
  • OfStack

Publish the ES1en-ES2en project to the tomcat container as usual web project 1

1. Modify the packaging

Set it in pom.xml <packaging>war</packaging>

2. Remove the embedded tomcat plug-in

Find the spring-ES20en-ES21en-ES22en dependency node in ES17en.xml and add the following code.


<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 <!--  Remove embedment tomcat The plug-in  -->
 <exclusions>
  <exclusion>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-tomcat</artifactId>
  </exclusion>
 </exclusions>
</dependency>

3. Add servlet-ES27en dependencies

You can do either of the following. You can choose 1


<dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>javax.servlet-api</artifactId>
 <version>3.1.0</version>
 <scope>provided</scope>
</dependency>
<dependency>
 <groupId>org.apache.tomcat</groupId>
 <artifactId>tomcat-servlet-api</artifactId>
 <version>8.0.36</version>
 <scope>provided</scope>
</dependency>

4. Modify the startup class and override the initialization methods

We usually start with the main method, there is a startup class of App, the code is as follows:


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

We need a configuration similar to web.xml to launch the spring context. Add one SpringBootStartApplication class to the sibling of the Application class, as follows:


/**
 *  Modify the startup class, inherit  SpringBootServletInitializer  Pay equal attention to writing  configure  methods 
 */
public class SpringBootStartApplication extends SpringBootServletInitializer {
 @Override
 protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
  //  Notice that this is pointing to the original main Method-Executed Application Start the class 
  return builder.sources(Application.class);
 }
}

5. Package and deploy

In the project root directory (the directory containing pom.xml), type on the command line:
mvn clean package, wait for the completion of packaging, appear [INFO] BUILD SUCCESS package is successful.
Then put the war package under target into tomcat's webapps directory, start tomcat, and automatically unzip the deployment.
Finally, type in the browser

http://localhost:[port number]/[package project name]/

Successful launch.

PS: Take a look at the solution for SpringBoot war package tomcat running startup error (Cannot determine database driver class for type NONE)

Our project 1 has always been run directly by jar package. Yesterday, my colleague said that it would be run in tomcat, so I helped him for a long time. Introduced to you in the article, the need of friends can click to view.

Then successfully package it and deploy it to the local tomcat startup with a headline error, and a web search tells you to add annotations to the startup class :@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class)

My personal test is invalid.

Solutions:

Launch the class with annotations :@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) It is effective by personal measurement.


Related articles: