custom failureAnalyzer (#1559)
This commit is contained in:
parent
77d270a4b1
commit
1a05305b31
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<BeanNotOfRequiredTypeException> {
|
||||
|
||||
@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();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.failureanalyzer;
|
||||
|
||||
public class MyDAO {
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.failureanalyzer;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository("myDAO")
|
||||
public class MySecondDAO {
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
org.springframework.boot.diagnostics.FailureAnalyzer=com.baeldung.failureanalyzer.MyBeanNotOfRequiredTypeFailureAnalyzer
|
Loading…
Reference in New Issue