17:26:36.498 [main] ERROR o.s.b.SpringApplication - [reportFailure,856] - Application run failed org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean. at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:163) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:577) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:447) at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) at com.joyo.odcmp.mais.OdcmpMaisApplication.main(OdcmpMaisApplication.java:42) Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean. at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:210) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:180) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:160) ... 6 common frames omitted Disconnected from the target VM, address: '127.0.0.1:14719', transport: 'socket'
时间: 2025-06-01 17:58:36 浏览: 35
### 解决方案
当遇到 `Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean` 的错误时,通常是因为 Spring Boot 应用程序未能找到合适的嵌入式服务器配置[^2]。
以下是可能的原因以及解决方案:
#### 1. **未引入任何嵌入式服务器依赖**
如果项目中没有引入 Tomcat、Jetty 或 Undertow 等嵌入式服务器的相关依赖,则会抛出此异常。默认情况下,Spring Boot 使用的是 Tomcat 嵌入式容器。因此可以尝试在项目的构建文件(Maven 或 Gradle)中添加以下依赖项来解决问题。
对于 Maven 构建工具:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
```
对于 Gradle 构建工具:
```groovy
implementation 'org.springframework.boot:spring-boot-starter-web'
```
以上依赖包含了必要的嵌入式 Tomcat 配置和支持 Web 功能的核心组件[^3]。
#### 2. **手动排除了嵌入式服务器依赖**
有时开发者可能会通过显式的 exclude 排除掉某些默认的嵌入式服务器支持。例如,在使用 `spring-boot-starter-web` 时,Tomcat 是默认启用的。但如果将其排除并忘记提供其他替代品,则会出现该问题。
检查是否存在如下类似的排除操作,并移除它或者替换为其他服务器实现:
```xml
<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>
```
如果确实需要切换到 Jetty 或 Undertow,请确保正确引入对应的 starter 并调整相关设置[^4]。
#### 3. **自定义 Bean 导致冲突**
如果有自定义实现了 `ServletWebServerFactory` 类型的 Bean 而且其逻辑存在问题也可能引发此类错误。建议审查代码中的所有 @Bean 定义部分是否有不当之处。比如下面的例子展示了如何创建一个简单的定制化配置类而不会干扰正常流程:
```java
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CustomWebServerConfig {
@Bean
public TomcatServletWebServerFactory servletContainer() {
return new TomcatServletWebServerFactory();
}
}
```
注意这里只是简单返回了一个标准工厂实例;实际开发过程中可以根据需求进一步扩展功能[^5]。
---
### 总结
综上所述,要解决由于缺失 `ServletWebServerFactory` Bean 所引起的 Spring Boot 启动失败问题,可以从以下几个方面入手:确认是否遗漏了基础框架包导入、排查有无不必要的模块屏蔽行为以及核查个人编写的服务端组件是否存在隐患等。
阅读全文
相关推荐



















