Skip to content

Commit 1a05305

Browse files
lor6KevinGilmore
authored andcommitted
custom failureAnalyzer (eugenp#1559)
1 parent 77d270a commit 1a05305

6 files changed

Lines changed: 67 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.failureanalyzer;
2+
3+
import javax.annotation.security.RolesAllowed;
4+
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
8+
@SpringBootApplication
9+
public class FailureAnalyzerApplication {
10+
@RolesAllowed("*")
11+
public static void main(String[] args) {
12+
System.setProperty("security.basic.enabled", "false");
13+
SpringApplication.run(FailureAnalyzerApplication.class, args);
14+
}
15+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.failureanalyzer;
2+
3+
import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
4+
import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
5+
import org.springframework.boot.diagnostics.FailureAnalysis;
6+
7+
public class MyBeanNotOfRequiredTypeFailureAnalyzer extends AbstractFailureAnalyzer<BeanNotOfRequiredTypeException> {
8+
9+
@Override
10+
protected FailureAnalysis analyze(Throwable rootFailure, BeanNotOfRequiredTypeException cause) {
11+
return new FailureAnalysis(getDescription(cause), getAction(cause), cause);
12+
}
13+
14+
private String getDescription(BeanNotOfRequiredTypeException ex) {
15+
return "The bean " + ex.getBeanName() //
16+
+ " could not be injected as " + ex.getRequiredType().getName() //
17+
+ " because it is of type " + ex.getActualType().getName();
18+
}
19+
20+
private String getAction(BeanNotOfRequiredTypeException ex) {
21+
return "Consider creating a bean with name "+ ex.getBeanName() //
22+
+ " of type " + ex.getRequiredType().getName();
23+
}
24+
25+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.failureanalyzer;
2+
3+
public class MyDAO {
4+
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.failureanalyzer;
2+
3+
import org.springframework.stereotype.Repository;
4+
5+
@Repository("myDAO")
6+
public class MySecondDAO {
7+
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.failureanalyzer;
2+
3+
import javax.annotation.Resource;
4+
5+
import org.springframework.stereotype.Service;
6+
7+
@Service
8+
public class MyService {
9+
10+
@Resource(name = "myDAO")
11+
private MyDAO myDAO;
12+
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.springframework.boot.diagnostics.FailureAnalyzer=com.baeldung.failureanalyzer.MyBeanNotOfRequiredTypeFailureAnalyzer

0 commit comments

Comments
 (0)