> For the complete documentation index, see [llms.txt](https://jack80342.gitbook.io/spring-boot/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jack80342.gitbook.io/spring-boot/ix.-how-to-guides/73.-spring-boot-application/73.1-create-your-own-failureanalyzer.md).

# 73.1 创建自己的FailureAnalyzer

[`FailureAnalyzer`](https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/api/org/springframework/boot/diagnostics/FailureAnalyzer.html)是拦截启动时的异常并将它转换为可读消息的很好方式(包装进[`FailureAnalysis`](https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/api/org/springframework/boot/diagnostics/FailureAnalysis.html))。Spring Boot为应用上下文相关异常，JSR-303校验等提供分析器。实际上创建你自己的分析器也相当简单。

`AbstractFailureAnalyzer`是`FailureAnalyzer`的一个方便扩展，根据指定类型的异常是否出现来进行处理。你可以继承它，这样就可以处理实际出现的异常。如果出于某些原因，不能处理该异常，那就返回`null`让其他实现处理。

`FailureAnalyzer`的实现必须注册到`META-INF/spring.factories`。以下注册了`ProjectConstraintViolationFailureAnalyzer`：

```
org.springframework.boot.diagnostics.FailureAnalyzer=\
com.example.ProjectConstraintViolationFailureAnalyzer
```

**注** 如果你需要访问`BeanFactory`或`Environment`，你的`FailureAnalyzer`可以简单地分别实现`BeanFactoryAware`或`EnvironmentAware`。
