Quick revision
The short answer
We extend AbstractFailureAnalyzer<LicenseMissingException> and implement analyze(Throwable rootFailure, LicenseMissingException cause), returning new FailureAnalysis(description, action, cause) with clear, human-readable text. We register the class in META-INF/spring.factories under the key org.springframework.boot.diagnostics.FailureAnalyzer. Boot then applies it whenever a LicenseMissingException appears anywhere in the cause chain of a startup failure.
What to remember
In this answer, we wrote a failure analyzer for a custom LicenseMissingException. We extended AbstractFailureAnalyzer, which walks the cause chain and calls analyze(rootFailure, cause) only when our exception is present, returned a FailureAnalysis with a precise description and action, registered the class in META-INF/spring.factories under org.springframework.boot.diagnostics.FailureAnalyzer, and tested it by analysing a wrapped exception.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We extend AbstractFailureAnalyzer<LicenseMissingException> and implement analyze(Throwable rootFailure, LicenseMissingException cause), returning new FailureAnalysis(description, action, cause) with clear, human-readable text. We register the class in META-INF/spring.factories under the key org.springframework.boot.diagnostics.FailureAnalyzer. Boot then applies it whenever a LicenseMissingException appears anywhere in the cause chain of a startup failure.
Why this question is asked
When our own code can stop an application from starting, the person who sees the failure is often not the person who wrote the code. An operator at 3 a.m. should read what is wrong and what to do, not dig through a stack trace for a nested cause. Failure analyzers give our exceptions the same friendly treatment Spring Boot gives its own. Interviewers ask this to see whether we know the extension point and how it is registered. In this answer, we will recap what a failure analyzer does, create the exception, write the analyzer, register it, and test it.
1A Quick Recap of Failure Analyzers
When SpringApplication.run fails, Boot collects all FailureAnalyzer implementations listed in spring.factories and asks each one, in order, to analyze the exception. The first analyzer that returns a FailureAnalysis wins, and Boot prints its description and action under the APPLICATION FAILED TO START banner. If none matches, Boot falls back to logging the stack trace.
The exception a startup failure throws is usually wrapped several times. A bean that throws in its constructor produces a BeanCreationException, which may be wrapped in an UnsatisfiedDependencyException, and so on. So an analyzer cannot just check the top-level type. AbstractFailureAnalyzer solves this: it walks the cause chain and calls our method only when it finds our exception type, wherever it is.
2The Exception We Want to Explain
Our example is a service that checks a license file at startup. The exception carries the facts the message needs, so the analyzer does not have to guess.
Notice that the exception stores the path as a field. Putting the important data on the exception, rather than only in the message string, lets the analyzer build precise text.
3Writing the Analyzer
The analyzer extends AbstractFailureAnalyzer with our exception as its type parameter. The base class finds the cause for us and passes both the original failure and our exception.
The description says what is wrong in the reader's terms. The action names the exact steps, including the property that changes the location and who to ask. That is the whole value of an analyzer, so the text deserves as much care as the code. Returning null from analyze is also allowed; it means this analyzer declines and Boot tries the next one.
4Registering the Analyzer
Failure analyzers are not beans. They are needed when startup has failed, possibly before the context exists, so Boot loads them through the spring.factories mechanism.
- LicenseChecker throws
LicenseMissingException - Wrapped by bean creation
BeanCreationException-> ... - Boot loads analyzersfrom
spring.factories - Our exception in the cause chain?Yesanalyze() returns FailureAnalysisNoTry the next analyzer
- Print Description and Actioncontext closed, exit
5 steps in order. Play them, or step through them one at a time.
If the analyzer needs information from the application, such as the Environment or the BeanFactory, it can declare a constructor with those parameters and Boot passes them in; check your version's documentation for the exact supported parameters. Older code implemented BeanFactoryAware or EnvironmentAware instead, which newer versions discourage. Putting the analyzer and its spring.factories entry in a shared library means every service that uses the license checker gets the readable message for free.
5Testing the Analyzer
Analyzers are plain classes, so a unit test can build a wrapped exception and check the result. A second test can start the application with a missing file and check the output.
Notice that we call the public analyze(Throwable) of the base class with the wrapped exception, so the test also proves that the analyzer finds our exception inside the chain, which is the behaviour that matters in a real startup.
6Follow-up questions
What if two analyzers match the same failure?
Boot asks them in order and uses the first non-null result. Analyzers are sorted by their order, which can be set with @Order or Ordered. In practice, analyzers for specific exception types rarely overlap.
Can a failure analyzer recover from the failure?
No. It only describes the failure; the application still exits. If the application should start without the license, the checker should handle that itself, for example by entering a limited mode.
Does the analyzer run for exceptions thrown after startup?
No. Failure analyzers apply only to failures during SpringApplication.run. Exceptions during request handling go through the web layer's error handling instead.
7Conclusion
In this answer, we wrote a failure analyzer for a custom LicenseMissingException. We extended AbstractFailureAnalyzer, which walks the cause chain and calls analyze(rootFailure, cause) only when our exception is present, returned a FailureAnalysis with a precise description and action, registered the class in META-INF/spring.factories under org.springframework.boot.diagnostics.FailureAnalyzer, and tested it by analysing a wrapped exception.
Open with
I would extend AbstractFailureAnalyzer with LicenseMissingException as the type parameter and implement the protected analyze method, which receives the root failure and my exception. The base class does the useful work of walking the cause chain, so even though my exception arrives wrapped in a BeanCreationException or something deeper, my method is called whenever it appears anywhere in the chain.
Then, if they want more
I return new FailureAnalysis with a description, an action and the cause.
The text is the point, so I would make the exception carry the expected path as a field, and write a description like the application needs a license file at this path but none exists, and an action that says where to copy it, which property changes the location and who issues licenses.
Analyzers are not beans, because they must work even if the context never started, so I register the class in META-INF/spring.factories under the key org.springframework.boot.diagnostics.FailureAnalyzer. If it needs the Environment, it can take it as a constructor parameter. Then Boot prints my description and action under APPLICATION FAILED TO START instead of a stack trace.
For testing, I would wrap my exception in a BeanCreationException, call analyze on it and assert on the text.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Boot reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write a FailureAnalyzer for a custom LicenseMissingException and explain how it is...": https://scrivio-interview-library.netlify.app/spring-boot/boot-startup/c02fa28823/