1. 概述
在本快速教程中,我們將深入研究 Spring Boot 錯誤“ApplicationContextException: 由於缺少 ServletWebServerFactory bean,無法啓動 ServletWebServerApplicationContext“。
首先,我們將闡明導致此錯誤的主要原因。然後,我們將通過一個實際示例演示如何重現該錯誤,最後將提供解決方案。
2. 可能原因
首先,讓我們嘗試理解錯誤信息意味着什麼。“Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean” 就説明了一切。它簡單地告訴我們,在 ServletWebServerFactory bean 沒有在 ApplicationContext 中配置。
該錯誤主要出現在 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 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)
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)
... 如上所示,我們得到“ApplicationContextException: 由於缺少 ServletWebServerFactory bean,無法啓動 ServletWebServerApplicationContext”錯誤。
4. 修復錯誤
解決我們錯誤的簡單方法是為我們的 MainEntryPoint 類添加 @SpringBootApplication 註解。
通過使用此註解,我們告訴 Spring Boot 自動配置必要的 Bean 並將其註冊到上下文中。
同樣,我們可以通過禁用 Web 環境來避免非 Web 應用程序中的錯誤。 要做到這一點,可以使用 spring.main.web-application-type 屬性。
在 application.properties 中:
spring.main.web-application-type=none同樣,在我們的 application.yml 中:
spring:
main:
web-application-type: nonenone 表示應用程序不應作為 Web 應用程序運行。它用於禁用 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”。
在過程中,我們通過一個實際示例,解釋瞭如何產生該錯誤以及如何解決它。