Publish the Spring-boot project in the same way as a normal Web project under the Tomcat container
First, modify the packaging form
Set in Pom.xml<packaging>war</packaging>
Second, remove the embedded Tomcat plugin
Find the spring-boot-starter-web
dependency node in Pom.xml and add the following code in it
<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>
Third, add the dependency of Servlet-api
Here are two ways to choose one
<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>
Iv. Modifying the Startup class and overriding the initialization method
We usually start with the main method, there is an app launch 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 the application class, with the following code:
/** * 修改启动类,继承 SpringBootServletInitializer 并重写 configure 方法 */public class SpringBootStartApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { // 注意这里要指向原先用main方法执行的Application启动类 return builder.sources(Application.class); }}
Five, packaging deployment
At the root of the project (that is, the directory containing the Pom.xml), enter it at the command line:
mvn clean package
Ready, wait for package to complete, appear [INFO] BUILD SUCCESS
to be packaged successfully.
Then put the war package in the target directory under Tomcat's WebApps directory and launch Tomcat to automatically unzip the deployment.
Finally, enter in the browser
http://localhost:[端口号]/[打包项目名]/
Publishing success
-
Top
-
5
-
Step
Deploy the Spring-boot project to the Tomcat container