1. Overview
1.概述
A FailureAnalyzer in Spring Boot offers a way to intercept exceptions that occur during the startup of an application causing an application startup failure.
Spring Boot中的FailureAnalyzer提供了一种方法,可以拦截应用程序启动过程中发生的异常,导致应用程序启动失败。
The FailureAnalyzer replaces the stack trace of the exception with a more readable message represented by a FailureAnalysis object that contains a description of the error and a suggested course of action.
FailureAnalyzer用一个FailureAnalysis对象代表的更易读的消息取代了异常的堆栈跟踪,该对象包含错误的描述和建议的行动方案。
Boot contains a series of analyzers for common startup exceptions such as PortInUseException, NoUniqueBeanDefinitionException, and UnsatisfiedDependencyException. These can be found in the org.springframework.boot.diagnostics package.
Boot包含一系列针对常见启动异常的分析器,如PortInUseException、NoUniqueBeanDefinitionException和UnsatisfiedDependencyException。这些可以在org.springframework.boot.diagnostics包中找到。
In this quick tutorial, we’re going to take a look at how we can add our own custom FailureAnalyzer to the existing ones.
在这个快速教程中,我们将看看如何在现有的FailureAnalyzer中添加我们自己的自定义FailureAnalyzer。
2. Creating a Custom FailureAnalyzer
2.创建一个自定义的FailureAnalyzer
To create a custom FailureAnalyzer, we’ll simply extend the abstract class AbstractFailureAnalyzer – which intercepts a specified exception type and implement analyze() API.
为了创建一个自定义的FailureAnalyzer,我们将简单地扩展抽象类AbstractFailureAnalyzer–它拦截一个指定的异常类型并实现analyze() API。
The framework provides a BeanNotOfRequiredTypeFailureAnalyzer implementation that deals with the exception BeanNotOfRequiredTypeException only if the bean being injected is of a dynamic proxy class.
该框架提供了一个BeanNotOfRequiredTypeFailureAnalyzer实现,该实现只在被注入的Bean是动态代理类时处理异常BeanNotOfRequiredTypeException。
Let’s create a custom FailureAnalyzer that deals with all exceptions of type BeanNotOfRequiredTypeException. Our class intercepts the exception and creates a FailureAnalysis object with helpful description and action messages:
让我们创建一个自定义的FailureAnalyzer,处理所有BeanNotOfRequiredTypeException类型的异常。我们的类拦截异常,并创建一个FailureAnalysis对象,其中包含有用的描述和操作信息。
public class MyBeanNotOfRequiredTypeFailureAnalyzer
extends AbstractFailureAnalyzer<BeanNotOfRequiredTypeException> {
@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. Registering the Custom FailureAnalyzer
3.注册自定义FailureAnalyzer
For the custom FailureAnalyzer to be considered by Spring Boot, it is mandatory to register it in a standard resources/META-INF/spring.factories file that contains the org.springframework.boot.diagnostics.FailureAnalyzer key with a value of the full name of our class:
为了使自定义的FailureAnalyzer被Spring Boot考虑,必须在标准的resources/META-INF/spring.plants文件中注册,该文件包含org.springframework.boot.diagnostics.FailureAnalyzer键,其值为我们类的全名。
org.springframework.boot.diagnostics.FailureAnalyzer=\
com.baeldung.failureanalyzer.MyBeanNotOfRequiredTypeFailureAnalyzer
4. Custom FailureAnalyzer in Action
4.自定义FailureAnalyzer的操作
Let’s create a very simple example in which we attempt to inject a bean of an incorrect type to see how our custom FailureAnalyzer behaves.
让我们创建一个非常简单的例子,我们试图注入一个不正确类型的Bean,看看我们的自定义FailureAnalyzer是如何表现的。
Let’s create two classes MyDAO and MySecondDAO and annotate the second class as a bean called myDAO:
让我们创建两个类MyDAO和MySecondDAO,并将第二个类注释为名为myDAO的bean。
public class MyDAO { }
@Repository("myDAO")
public class MySecondDAO { }
Next, in a MyService class, we will attempt to inject the myDAO bean, which is of type MySecondDAO, into a variable of type MyDAO:
接下来,在MyService类中,我们将尝试把myDAObean(其类型为MySecondDAO)注入到MyDAO类型的变量。
@Service
public class MyService {
@Resource(name = "myDAO")
private MyDAO myDAO;
}
Upon running the Spring Boot application, the startup will fail with the following console output:
运行Spring Boot应用程序时,启动将失败,控制台输出如下。
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean myDAO could not be injected as com.baeldung.failureanalyzer.MyDAO
because it is of type com.baeldung.failureanalyzer.MySecondDAO$$EnhancerBySpringCGLIB$$d902559e
Action:
Consider creating a bean with name myDAO of type com.baeldung.failureanalyzer.MyDAO
5. Conclusion
5.结论
In this quick tutorial, we’ve focused on how implementing a custom Spring Boot FailureAnalyzer.
在这个快速教程中,我们重点介绍了如何实现一个自定义的Spring Boot FailureAnalyzer。
As always, the full source code of the example can be found over on GitHub.
一如既往,该示例的完整源代码可以在GitHub上找到over。