Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

A FailureAnalyzer in Spring Boot offers a way to intercept exceptions that occur during the startup of an application causing an application startup failure.

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.

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.

In this quick tutorial, we’re going to take a look at how we can add our own custom FailureAnalyzer to the existing ones.

2. Creating a Custom FailureAnalyzer

To create a custom FailureAnalyzer, we’ll simply extend the abstract class AbstractFailureAnalyzer – which intercepts a specified exception type and implement 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.

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:

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

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:

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

4. Custom FailureAnalyzer in Action

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.

Let’s create two classes MyDAO and MySecondDAO and annotate the second class as a bean called myDAO:

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:

@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:

***************************
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

In this quick tutorial, we’ve focused on how implementing a custom Spring Boot FailureAnalyzer.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW