Spring BootでカスタムFailureAnalyzerを作成する

Spring BootでカスタムFailureAnalyzerを作成する

1. 概要

Spring BootのFailureAnalyzerは、アプリケーションの起動エラーを引き起こすintercept exceptions that occur during the startup of an applicationへの方法を提供します。

FailureAnalyzerは、例外のスタックトレースを、エラーの説明と推奨される一連のアクションを含むFailureAnalysisオブジェクトで表されるより読みやすいメッセージに置き換えます。

ブートには、PortInUseExceptionNoUniqueBeanDefinitionExceptionUnsatisfiedDependencyException. These can be found in the org.springframework.boot.diagnostics package.などの一般的な起動例外用の一連のアナライザーが含まれています

このクイックチュートリアルでは、既存のものに対するhow we can add our own custom FailureAnalyzerを見ていきます。

2. カスタムFailureAnalyzerの作成

カスタムFailureAnalyzerを作成するには、抽象クラスAbstractFailureAnalyzerを拡張するだけです。これは、指定された例外タイプをインターセプトし、analyze()APIを実装します。

フレームワークは、注入されるBeanが動的プロキシクラスである場合にのみ例外BeanNotOfRequiredTypeExceptionを処理するBeanNotOfRequiredTypeFailureAnalyzer実装を提供します。

タイプBeanNotOfRequiredTypeException.のすべての例外を処理するカスタムFailureAnalyzerを作成しましょう。クラスは例外をインターセプトし、役立つ説明とアクションメッセージを含むFailureAnalysisオブジェクトを作成します。

public class MyBeanNotOfRequiredTypeFailureAnalyzer
  extends AbstractFailureAnalyzer {

    @Override
    protected FailureAnalysis analyze(Throwable rootFailure,
      BeanNotOfRequiredTypeException cause) {
        return new FailureAnalysis(getDescription(cause), getAction(cause), cause);
    }

    private String getDescription(BeanNotOfRequiredTypeException ex) {
        return String.format("The bean %s could not be injected as %s "
          + "because it is of type %s",
          ex.getBeanName(),
          ex.getRequiredType().getName(),
          ex.getActualType().getName());
    }

    private String getAction(BeanNotOfRequiredTypeException ex) {
        return String.format("Consider creating a bean with name %s of type %s",
          ex.getBeanName(),
          ex.getRequiredType().getName());
    }
}

3. カスタムFailureAnalyzerの登録

カスタムFailureAnalyzerがSpring Bootによって考慮されるためには、クラスのフルネームの値を持つorg.springframework.boot.diagnostics.FailureAnalyzerキーを含む標準のresources/META-INF/spring.factoriesファイルにそれを登録することが必須です。

org.springframework.boot.diagnostics.FailureAnalyzer=\
  com.example.failureanalyzer.MyBeanNotOfRequiredTypeFailureAnalyzer

4. 動作中のカスタムFailureAnalyzer

間違ったタイプのBeanを注入して、カスタムFailureAnalyzerがどのように動作するかを確認する、非常に簡単な例を作成しましょう。

2つのクラスMyDAOMySecondDAOを作成し、2番目のクラスにmyDAOというBeanとして注釈を付けましょう。

public class MyDAO { }
@Repository("myDAO")
public class MySecondDAO { }

次に、MyServiceクラスで、タイプMySecondDAOmyDAO BeanをタイプMyDAOの変数に注入しようとします。

@Service
public class MyService {

    @Resource(name = "myDAO")
    private MyDAO myDAO;
}

Spring Bootアプリケーションを実行すると、次のコンソール出力で起動が失敗します。

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean myDAO could not be injected as com.example.failureanalyzer.MyDAO
  because it is of type com.example.failureanalyzer.MySecondDAO$$EnhancerBySpringCGLIB$$d902559e

Action:

Consider creating a bean with name myDAO of type com.example.failureanalyzer.MyDAO

5. 結論

このクイックチュートリアルでは、カスタムSpring BootFailureAnalyzerを実装する方法に焦点を当てました。

いつものように、例の完全なソースコードはover on GitHubにあります。