Spring Boot Error ApplicationContextException – Spring Boot错误ApplicationContextException

最后修改: 2021年 6月 5日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’re going to take a close look at the Spring Boot error “ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean“.

在这个快速教程中,我们将仔细研究Spring Boot错误”ApplicationContextException:无法启动ServletWebServerApplicationContext,因为缺少ServletWebServerFactory Bean“。

First of all, we’re going to shed light on the main causes behind this error. Then, we’ll dive into how to reproduce it using a practical example and finally how to solve it.

首先,我们要阐明这个错误背后的主要原因。然后,我们将深入探讨如何使用一个实际的例子来重现它,最后是如何解决它。

2. Possible Causes

2.可能的原因

First, let’s try to understand what the error message means. “Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean” says it all. It simply tells us that there is no configured ServletWebServerFactory bean in the ApplicationContext.

首先,让我们试着理解这个错误信息的含义。”由于缺少ServletWebServerFactory bean,无法启动ServletWebServerApplicationContext“说明了一切。它简单地告诉我们,在ServletWebServerFactory 中没有配置ApplicationContext的bean。

The error comes up mainly when Spring Boot fails to start the ServletWebServerApplicationContext. Why? Because the ServletWebServerApplicationContext uses a contained ServletWebServerFactory bean to bootstrap itself.

该错误主要出现在Spring Boot无法启动ServletWebServerApplicationContext时。为什么呢?因为ServletWebServerApplicationContext使用一个包含的ServletWebServerFactorybean来引导自己。

In general, Spring Boot provides the SpringApplication.run method to bootstrap Spring applications.

一般来说,Spring Boot提供SpringApplication.run方法来引导Spring应用程序。

The SpringApplication class will attempt to create the right ApplicationContext for us, depending on whether we are developing a web application or not.

SpringApplication类将尝试为我们创建正确的ApplicationContext取决于我们是否正在开发一个Web应用程序

For example, the algorithm used to determine if a web application comes from some dependencies like spring-boot-starter-web. With that being said, the absence of these dependencies can be one of the reasons behind our error.

例如,用于确定一个网络应用程序是否来自一些依赖性的算法,如spring-boot-starter-web.说到这里,这些依赖性的缺失可能是我们错误背后的原因之一。

Another cause would be missing the @SpringBootApplication annotation in the Spring Boot entry point class.

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

3. Reproducing the Error

3.重现错误

Now, let’s see an example where we can produce the Spring Boot error. The simplest way to achieve this is to create a main class without the @SpringBootApplication annotation.

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

First, let’s create an entry point class and deliberately forget to annotate it with @SpringBootApplication:

首先,让我们创建一个入口类,并故意忘记用@SpringBootApplication来注释它。

public class MainEntryPoint {

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

Now, let’s run our sample Spring Boot application and see what happens:

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

22:20:39.134 [main] ERROR o.s.boot.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.>
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:209)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179)
	... 

As shown above, we get “ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean” error.

如上所示,我们得到”ApplicationContextException:由于缺少ServletWebServerFactory Bean,无法启动ServletWebServerApplicationContext“错误。

4. Fixing the Error

4.修复错误

The simple solution to fix our error would be to annotate our MainEntryPoint class with the @SpringBootApplication annotation.

解决错误的简单方法是用@SpringBootApplication 注解我们的MainEntryPoint类。

By using this annotation, we tell Spring Boot to auto-configure the necessary beans and register them in the context.

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

Similarly, we can avoid the error for non-web applications by disabling the web environment. To do so, we can use the spring.main.web-application-type property.

同样地,我们可以通过禁用网络环境来避免非网络应用的错误。要做到这一点,我们可以使用spring.main.web-application-typeproperty。

In application.properties:

application.properties中。

spring.main.web-application-type=none

Likewise, in our application.yml:

同样地,在我们的application.yml中。

spring: 
    main: 
        web-application-type: none

none means that the application should not run as a web application. It’s used to disable the webserver.

none意味着该应用程序不应作为一个网络应用程序运行。它是用来禁用网络服务器的

Bear in mind that starting from Spring Boot 2.0, we can also use SpringApplicationBuilder to explicitly define a specific type of web application:

请记住,从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);
    }
}

For a WebFlux project, we can use WebApplicationType.REACTIVE. Another solution could be to exclude the spring-webmvc dependency.

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

The presence of this dependency in the classpath tells Spring Boot to treat the project as a servlet application and not as a reactive web application. As a result, Spring Boot fails to start the ServletWebServerApplicationContext.

这个依赖关系在classpath中的存在告诉Spring Boot将该项目视为一个Servlet应用程序,而不是一个反应式Web应用程序。因此Spring Boot无法启动ServletWebServerApplicationContext

5. Conclusion

5.总结

In this short article, we discussed in detail what causes Spring Boot to fail at startup with this error: “ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean“.

在这篇短文中,我们详细讨论了导致Spring Boot在启动时出现这种错误的原因。”ApplicationContextException。由于缺少ServletWebServerFactory bean,无法启动ServletWebServerApplicationContext“。

Along the way, we explained, through a practical example, how to produce the error and how to fix it.

一路走来,我们通过一个实际的例子解释了如何产生错误以及如何修复它。

As always, the full source code of the examples is available over on GitHub.

一如既往,这些示例的完整源代码可在GitHub上获得over