拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 Spring Boot ApplicationContextException 异常

Spring Boot ApplicationContextException 异常

白鹭 - 2021-11-24 417 0 0

1. 概述

在本快速教程中,我们将仔细研究 Spring Boot 错误“ ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean ”。

首先,我们将阐明此错误背后的主要原因。然后,我们将深入研究如何使用实际示例重现它,最后如何解决它。

2. 可能的原因

首先,让我们尝试了解错误消息的含义。 “ Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean ”说明了一切。它只是告诉我们ApplicationContext ServletWebServerFactory bean .

该错误主要出现在 Spring Boot 无法启动ServletWebServerApplicationContext 。为什么?因为ServletWebServerApplicationContext使用包含的ServletWebServerFactory bean 来引导自身。

通常,Spring Boot 提供SpringApplication.run方法来引导 Spring 应用程序。

SpringApplication类将尝试为我们ApplicationContext ,具体取决于我们是否正在开发 Web 应用程序

例如,用于确定 Web 应用程序是否来自某些依赖项(如spring-boot-starter-web.话虽如此,缺乏这些依赖关系可能是我们错误背后的原因之一。

另一个原因是缺少 Spring Boot 入口点类中@SpringBootApplication

3. 重现错误

现在,让我们看一个可以产生 Spring Boot 错误的示例。实现这一点的最简单方法是创建一个没有@SpringBootApplication注释的主类。

首先,让我们创建一个入口点类,并故意忘记使用@SpringBootApplication进行注释:

public class MainEntryPoint {

 public static void main(String[] args) {
 SpringApplication.run(MainEntryPoint.class, args);
 }
 }

现在,让我们运行示例 Spring Boot 应用程序,看看会发生什么:

22:20:39.134 [main] ERROR osboot.SpringApplication - 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 com.baeldung.applicationcontextexception.MainEntryPoint.main(MainEntryPoint.java:10)
 <strong>Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.</strong>

 at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:209)

 at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179)

 ...

如上所示,我们得到“ ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean ”错误。

4. 修复错误

修复我们错误的简单解决方案是使用@SpringBootApplication注释MainEntryPoint .

通过使用这个注解,我们告诉 Spring Boot 自动配置必要的 bean 并在 context 中注册它们

同样,我们可以通过禁用 Web 环境来避免非 Web 应用程序的错误。为此,我们可以使用spring.main.web-application-type属性。

application.properties

spring.main.web-application-type=none

同样,在我们的application.yml

spring:
 main:
 web-application-type: none

none表示应用程序不应作为 Web 应用程序运行。它用于禁用网络服务器

请记住,从Spring Boot 2.0 开始,我们还可以使用SpringApplicationBuilder显式定义特定类型的 Web 应用程序:

@SpringBootApplication
 public class MainClass {

 public static void main(String[] args) {
 new SpringApplicationBuilder(MainClass.class)
 .web(WebApplicationType.NONE)
 .run(args);
 }
 }

对于 WebFlux 项目,我们可以使用**WebApplicationType.REACTIVE** .另一种解决方案可能是排除spring-webmvc依赖项。

类路径中存在的这种依赖告诉 Spring Boot 将项目视为 servlet 应用程序而不是响应式 Web 应用程序。因此, Spring Boot 无法启动ServletWebServerApplicationContext

5. 结论

在这篇简短的文章中,我们详细讨论了导致 Spring Boot 在启动时失败并出现以下错误的原因:“ ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean ”。

在此过程中,我们通过一个实际示例解释了如何产生错误以及如何修复它。

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *