Spring Boot启动报错:缺失ServletWebServerFactory类型Bean
解决Spring Boot启动报错:未找到
org.springframework.boot.web.servlet.server.ServletWebServerFactory类型Bean的问题 1. 检查并补全Maven依赖
确保pom.xml中包含Spring Boot Web Starter依赖,这是自动配置ServletWebServerFactory的核心:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
如果使用外部Tomcat(而非内嵌Tomcat),需将web starter中的Tomcat依赖设为provided:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
同时确认Spring Boot版本与Tomcat 8.5.8兼容:Spring Boot 2.1.x至2.3.x版本适配Tomcat 8.5,避免使用2.4+版本(默认适配Tomcat 9+)。
2. 确认启动类配置
启动类必须标注@SpringBootApplication注解,且所在包需覆盖控制器的包路径(控制器应放在启动类的同级或子包下):
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } }
若控制器不在默认扫描路径,需手动添加@ComponentScan指定扫描范围:
@SpringBootApplication @ComponentScan(basePackages = "com.yourpackage.controller") public class YourApplication { // ... }
3. 排除手动配置冲突
检查是否存在自定义WebMvc配置类,避免标注@EnableWebMvc——该注解会禁用Spring Boot的自动Web配置,包括ServletWebServerFactory的自动创建。
4. 清理Maven缓存并重新构建
执行以下命令清理本地Maven缓存并重新下载依赖:
mvn clean install -U
在Eclipse中,可通过Project > Clean清理项目,再右键项目选择Maven > Update Project更新依赖。
内容的提问来源于stack exchange,提问作者Virtual Genius... Kinda
相关产品推荐
相关产品推荐

