Publish the spring-boot project to the tomcat container as usual web projects
1. Modify the packaging form
Set <packaging>war</packaging> in pom.xml
2. Remove the embedded tomcat plug-in
Find the spring-boot-starter-web dependency node in pom.xml, add the following code to it.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- Remove embedded tomcat plugin--> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions></dependency>
3. Add servlet-api dependencies
Both of the following methods are OK, choose one of them
<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 rewrite the initialization method
We usually use the main method to start, and we have an App startup class, the code is as follows:
@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}We need a configuration similar to web.xml to start the spring context. Add a SpringBootStartApplication class to its simultaneous level of the Application class, and its code is as follows:
/** * Modify the startup class, inherit SpringBootServletInitializer and overwrite the configure method */public class SpringBootStartApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { // Note that here we point to the Application startup class that was originally executed with the main method return builder.sources(Application.class); }}5. Package and deployment
In the project root directory (that is, the directory containing pom.xml), enter:
mvn clean package is enough. Wait for the packaging to be completed. [INFO] BUILD SUCCESS appears and the packaging is successful.
Then put the war package in the target directory into the webapps directory of tomcat, start to tomcat, and automatically decompress and deploy.
Finally enter in the browser
http://localhost:[port number]/[package project name]/
Release was successful.
PS: Let's take a look at the solution to the SpringBoot war package tomcat run startup error (Cannot determined embedded database driver class for database type NONE)
Our project has always been run directly by jar packages. Yesterday, my colleague said he wanted to run it in Tomcat, so I helped him make a fortune for a long time. I will introduce it to you in the article. Friends who need it can click to view it.
Then it was successfully packaged and deployed into the local tomcat and reported a title error when it was started. After searching online, it was said that annotations were added to the startup class :@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class)
My personal test is invalid.
Solution:
The annotation for starting class :@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) is valid for personal testing.