From 1a05305b31f966f64e0f8e5fe1ea06ea26f62be2 Mon Sep 17 00:00:00 2001 From: lor6 Date: Mon, 3 Apr 2017 05:18:07 +0300 Subject: [PATCH] custom failureAnalyzer (#1559) --- .../FailureAnalyzerApplication.java | 15 +++++++++++ ...yBeanNotOfRequiredTypeFailureAnalyzer.java | 25 +++++++++++++++++++ .../com/baeldung/failureanalyzer/MyDAO.java | 5 ++++ .../baeldung/failureanalyzer/MySecondDAO.java | 8 ++++++ .../baeldung/failureanalyzer/MyService.java | 13 ++++++++++ .../main/resources/META-INF/spring.factories | 1 + 6 files changed, 67 insertions(+) create mode 100644 spring-boot/src/main/java/com/baeldung/failureanalyzer/FailureAnalyzerApplication.java create mode 100644 spring-boot/src/main/java/com/baeldung/failureanalyzer/MyBeanNotOfRequiredTypeFailureAnalyzer.java create mode 100644 spring-boot/src/main/java/com/baeldung/failureanalyzer/MyDAO.java create mode 100644 spring-boot/src/main/java/com/baeldung/failureanalyzer/MySecondDAO.java create mode 100644 spring-boot/src/main/java/com/baeldung/failureanalyzer/MyService.java create mode 100644 spring-boot/src/main/resources/META-INF/spring.factories diff --git a/spring-boot/src/main/java/com/baeldung/failureanalyzer/FailureAnalyzerApplication.java b/spring-boot/src/main/java/com/baeldung/failureanalyzer/FailureAnalyzerApplication.java new file mode 100644 index 0000000000..3489732b6f --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/failureanalyzer/FailureAnalyzerApplication.java @@ -0,0 +1,15 @@ +package com.baeldung.failureanalyzer; + +import javax.annotation.security.RolesAllowed; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class FailureAnalyzerApplication { + @RolesAllowed("*") + public static void main(String[] args) { + System.setProperty("security.basic.enabled", "false"); + SpringApplication.run(FailureAnalyzerApplication.class, args); + } +} diff --git a/spring-boot/src/main/java/com/baeldung/failureanalyzer/MyBeanNotOfRequiredTypeFailureAnalyzer.java b/spring-boot/src/main/java/com/baeldung/failureanalyzer/MyBeanNotOfRequiredTypeFailureAnalyzer.java new file mode 100644 index 0000000000..2f83ad6b57 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/failureanalyzer/MyBeanNotOfRequiredTypeFailureAnalyzer.java @@ -0,0 +1,25 @@ +package com.baeldung.failureanalyzer; + +import org.springframework.beans.factory.BeanNotOfRequiredTypeException; +import org.springframework.boot.diagnostics.AbstractFailureAnalyzer; +import org.springframework.boot.diagnostics.FailureAnalysis; + +public class MyBeanNotOfRequiredTypeFailureAnalyzer extends AbstractFailureAnalyzer { + + @Override + protected FailureAnalysis analyze(Throwable rootFailure, BeanNotOfRequiredTypeException cause) { + return new FailureAnalysis(getDescription(cause), getAction(cause), cause); + } + + private String getDescription(BeanNotOfRequiredTypeException ex) { + return "The bean " + ex.getBeanName() // + + " could not be injected as " + ex.getRequiredType().getName() // + + " because it is of type " + ex.getActualType().getName(); + } + + private String getAction(BeanNotOfRequiredTypeException ex) { + return "Consider creating a bean with name "+ ex.getBeanName() // + + " of type " + ex.getRequiredType().getName(); + } + +} diff --git a/spring-boot/src/main/java/com/baeldung/failureanalyzer/MyDAO.java b/spring-boot/src/main/java/com/baeldung/failureanalyzer/MyDAO.java new file mode 100644 index 0000000000..ddaeb28574 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/failureanalyzer/MyDAO.java @@ -0,0 +1,5 @@ +package com.baeldung.failureanalyzer; + +public class MyDAO { + +} diff --git a/spring-boot/src/main/java/com/baeldung/failureanalyzer/MySecondDAO.java b/spring-boot/src/main/java/com/baeldung/failureanalyzer/MySecondDAO.java new file mode 100644 index 0000000000..12dd73a05b --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/failureanalyzer/MySecondDAO.java @@ -0,0 +1,8 @@ +package com.baeldung.failureanalyzer; + +import org.springframework.stereotype.Repository; + +@Repository("myDAO") +public class MySecondDAO { + +} diff --git a/spring-boot/src/main/java/com/baeldung/failureanalyzer/MyService.java b/spring-boot/src/main/java/com/baeldung/failureanalyzer/MyService.java new file mode 100644 index 0000000000..72334ca8fa --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/failureanalyzer/MyService.java @@ -0,0 +1,13 @@ +package com.baeldung.failureanalyzer; + +import javax.annotation.Resource; + +import org.springframework.stereotype.Service; + +@Service +public class MyService { + + @Resource(name = "myDAO") + private MyDAO myDAO; + +} diff --git a/spring-boot/src/main/resources/META-INF/spring.factories b/spring-boot/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000000..e3d3aa4c8e --- /dev/null +++ b/spring-boot/src/main/resources/META-INF/spring.factories @@ -0,0 +1 @@ +org.springframework.boot.diagnostics.FailureAnalyzer=com.baeldung.failureanalyzer.MyBeanNotOfRequiredTypeFailureAnalyzer \ No newline at end of file