

Table of Contents
1. Overview
In this article, We are going to discuss about how we can implement spring boot startup failer listener or Spring boot startup failure analyzer example. For any reason, if the application startup gets failed then we need to write our custom message to more specific and human-readable message. FailureAnalyzer provide a way to customize our message based on startup failed exception.
Step to Configure startup failed analyzer
- Let’s define
CustomFailedAnalyzerwhich implementsFailureAnalyzerand overrideanalyze()method. When application startup failed at that timeanalyze()method will be called automatically by spring boot where return newFailureAnalysiswhich accept action, custom message andThrowableobject: - If you can not handle exception then return
nullso spring boot will try to handle exception from other implementation.
package com.javadeveloperzone;
import org.springframework.boot.diagnostics.FailureAnalysis;
import org.springframework.boot.diagnostics.FailureAnalyzer;
/**
* Created by JavaDeveloperZone on 03-05-2018.
* Here we have implements FailureAnalyzer and override analyze method which will be
* called automatically when application startup will get failed.
*/
public class CustomFailedAnalyzer implements FailureAnalyzer {
@Override
public FailureAnalysis analyze(Throwable failure) {
return new FailureAnalysis("Custom Failed Message : ",failure.getMessage(),failure);
}
}
2. Create spring.factories inside resources/META-INF/ and register org.springframework.boot.diagnostics.FailureAnalyzer listener:
org.springframework.boot.diagnostics.FailureAnalyzer=com.javadeveloperzone.CustomFailedAnalyzer
2. Example

Spring boot startup failure analyzer example
2.1 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spring-boot-example</groupId>
<artifactId>spring-boot-example</artifactId>
<version>1.0-SNAPSHOT</version>
<description>Spring boot startup failure analyzer example</description>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>2.2 application.properties
server.port=8181
2.3 spring.factories
spring.factories must be inside resources/META-INF
org.springframework.boot.diagnostics.FailureAnalyzer=com.javadeveloperzone.CustomFailedAnalyzer
2.4 CustomFailedAnalyzer
Here we have defined CustomFailedAnalyzer and implements FailureAnalyzer interface. analyze() method will automatically call when application startup will be failed for any reason.
package com.javadeveloperzone;
import org.springframework.boot.diagnostics.FailureAnalysis;
import org.springframework.boot.diagnostics.FailureAnalyzer;
/**
* Created by JavaDeveloperZone on 03-05-2018.
* Here we have implements FailureAnalyzer and override analyze method which will be
* called automatically when application startup will get failed.
*/
public class CustomFailedAnalyzer implements FailureAnalyzer {
@Override
public FailureAnalysis analyze(Throwable failure) {
return new FailureAnalysis("Custom Failed Message : ",failure.getMessage(),failure);
}
}
2.5 SpringBootConfig
package com.javadeveloperzone;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/**
* Created by JavaDeveloperZone on 19-07-2017.
*/
@SpringBootApplication
@ComponentScan // Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute
public class SpringBootConfig {
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootConfig.class, args); // it wil start application
}
}
2.6 Output
Let’s run same spring application two times so it second time application will fail because of the port is used.
> java -jar spring-boot-example-1.0-SNAPSHOT.jar
> java -jar spring-boot-example-1.0-SNAPSHOT.jar
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.4.RELEASE) 2018-05-03 21:25:49.996 INFO 47856 --- [ main] com.javadeveloperzone.SpringBootConfig : Starting SpringBootConfig on Mahesh with PID 47856 (F:\extrawork\spring-boot\spring-boot-failure-analyzer-example\target\classes started by Lenovo in F:\extrawork\spring-boot\spring-boot-failure-analyzer-example) 2018-05-03 21:25:50.000 INFO 47856 --- [ main] com.javadeveloperzone.SpringBootConfig : No active profile set, falling back to default profiles: default *************************** APPLICATION FAILED TO START *************************** Description: Custom Failed Message : Action: Connector configured to listen on port 8181 failed to start
3. Conclusion
We have learned here about how we can write our custom code while spring boot application will get failed. especially for microservice architecture we so many services are available in that case custom messages will make issue finding issue while application startup failed.
4. References
5. Source Code
spring-boot-failure-analyzer-example (176 KB)
