1. 概要

このクイックチュートリアルでは、 Spring Bootエラー「ApplicationContextException:ServletWebServerFactory bean がないため、ServletWebServerApplicationContextを開始できません」を詳しく見ていきます。

まず、このエラーの背後にある主な原因を明らかにします。 次に、実際の例を使用してそれを再現する方法、そして最後にそれを解決する方法について詳しく説明します。

2. 考えられる原因

まず、エラーメッセージの意味を理解してみましょう。 「ServletWebServerFactorybean がないため、ServletWebServerApplicationContextを開始できません」とすべてが述べています。 ApplicationContextに構成済みのServletWebServerFactorybeanがないことを示しているだけです。

このエラーは、主にSpringBootがServletWebServerApplicationContextの起動に失敗した場合に発生します。 なんで? ServletWebServerApplicationContextは含まれているServletWebServerFactoryBeanを使用してそれ自体をブートストラップするためです。

一般に、Spring Bootは、SpringアプリケーションをブートストラップするためのSpringApplication.runメソッドを提供します。

SpringApplication クラスは、Webアプリケーションを開発しているかどうかに応じて、適切なApplicationContextを作成しようとします

たとえば、Webアプリケーションが spring-boot-starter-webなどの依存関係に由来するかどうかを判断するために使用されるアルゴリズム。そうは言っても、これらの依存関係がないことが、エラーの背後にある理由の1つである可能性があります。 。

もう1つの原因は、SpringBootエントリポイントクラスの@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)
<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:ServletWebServerFactory bean がないため、ServletWebServerApplicationContextを開始できません」というエラーが発生します。

4. エラーの修正

エラーを修正する簡単な解決策は、MainEntryPointクラスに@SpringBootApplicationアノテーションでアノテーションを付けることです。

このアノテーションを使用することで、必要なBeanを自動構成し、コンテキストに登録するようにSpringBootに指示します。

同様に、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アプリケーションとして実行してはならないことを意味します。 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。 別の解決策は、 春-webmvc 依存。

クラスパスにこの依存関係が存在することで、Spring Bootは、プロジェクトをリアクティブWebアプリケーションとしてではなくサーブレットアプリケーションとして扱うようになります。 その結果、SpringBootはServletWebServerApplicationContextの開始に失敗します。

5. 結論

この短い記事では、起動時にSpring Bootが次のエラーで失敗する原因について詳しく説明しました:「 ApplicationContextException:ServletWebServerFactory bean がないため、ServletWebServerApplicationContextを開始できません」。

その過程で、実際の例を通して、エラーを生成する方法とそれを修正する方法を説明しました。

いつものように、例の完全なソースコードは、GitHubから入手できます。