0x01. Overview
SpringBoot is usually used so cool that it even integrates Tomcat itself. We can directly write the SBT startup class and then enable the built-in Tomcat container service with one click. It is really easy to get started. But considering the actual situation, our Tomcat server is generally deployed separately and has a special maintenance method. At this time, we need to strip away the built-in Tomcat server of the SBT application, and then publish and deploy the application to the external Tomcat container. This article will practice this.
0x02. Modify the packaging method
To modify the project's pom.xml configuration, we modify its packaging method to war, such as:
<groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging>
0x03. Remove the embedded Tomcat that comes with SBT
Modify pom.xml to remove the embedded tomcat plugin that comes with springboot from maven's pom
<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>
0x04. Add servlet-api dependency
Modify pom.xml and add servlet-api dependencies in maven's pom
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope></dependency>
0x05. Modify the startup class and override the initialization method
In SpringBoot, we usually start the main method, and we have a SpringBootApplication startup class, similar code is as follows:
@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}Now we need a configuration similar to web.xml to start the spring application. To this end, we add a SpringBootStartApplication class to the same level of the Application class, and the code is as follows:
// Modify the startup class, inherit the SpringBootServletInitializer and override the configure method public class SpringBootStartApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { // Note that here must point to the Application startup class that was originally executed with the main method. Return builder.sources(Application.class); }}0x06. Deploy to external Tomcat container and verify
Memorize the maven packaging operation in the project root directory (that is, the directory containing pom.xml):
mvn clean package
Waiting for packaging to be completed, [INFO] BUILD SUCCESS appears to be packaging successfully
Then we put the war package generated in the target directory into the webapps directory of tomcat, start to tomcat, and automatically decompress and deploy.
Finally verify in the browser:
http://YOUR_IP:[端口号]/[打包项目名]
You can also directly name the project ROOT, so that you can access the SpringBoot application in tomcat
http://YOUR_IP:[端口号]
Summarize
The above is the SpringBoot application introduced by the editor to you is deployed in an external Tomcat container. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!