Spring Boot ApplicationContextException 異常
- java
- Spring Boot
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
”。
在此過程中,我們通過一個實際示例解釋瞭如何產生錯誤以及如何修復它。