项目结构图:

pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
</parent>
<packaging>war</packaging>
<groupId>com.kaven</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot</name>
<description>springboot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>hello-world</finalName>
</build>
</project>
打包成WAR:
<packaging>war</packaging>
声明Spring Boot应用的内嵌Tomcat容器的作用域,在应用运行时不使用内嵌Tomcat容器,因为需要使用外部Tomcat容器。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
application.properties:
server.port=8080
Spring Boot应用的默认启动端口也是8080,这里只是为了演示。
HelloWorldController:
package com.kaven.springboot.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@GetMapping("/hello-world")
public String helloWorld() {
return "Hello World";
}
}
启动类:
package com.kaven.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
ApplicationInitializer(继承SpringBootServletInitializer抽象类,和启动类同级):
package com.kaven.springboot;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ApplicationInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SpringbootApplication.class);
}
}
SpringBootServletInitializer抽象类是一个WebApplicationInitializer,以传统的WAR部署运行SpringApplication。将Servlet、Filter和ServletContextInitializer这些bean从应用程序上下文绑定到服务器。要配置应用程序,需要重写configure(SpringApplicationBuilder)方法(调用SpringApplicationBuilder.sources(Class...))。如果将SpringBootServletInitializer与其他WebApplicationInitializer结合使用,可能还需要添加@Ordered注解来配置特定的启动顺序。WebApplicationInitializer仅在构建war文件并部署它时才需要。
开始打包。


下载Tomcat:
- 下载地址
博主下载的版本是9.0.58。

2.6.2版本的Spring Boot内嵌的Tomcat版本为9.0.56,为了避免麻烦,可以使用和内嵌Tomcat一样的版本。

将WAR文件复制到Tomcat的webapps文件夹下。

启动Tomcat,双击下图标记的脚本。

Tomcat的启动日志输出如下图所示(存在乱码),可见Started ApplicationInitializer in 2.55 seconds (JVM running for 5.401),说明实现的SpringBootServletInitializer被使用了。

Tomcat启动成功。

接口可以正常访问。

解决Tomcat乱码,需要添加一条配置到conf文件夹下的logging.properties配置文件中。
java.util.logging.ConsoleHandler.encoding = GBK

重新启动Tomcat,Tomcat的启动日志输出就没有乱码了。

到这里就结束了,如果博主有说错的地方或者大家有不同的见解,欢迎大家评论补充。










