custom failureAnalyzer (#1559)

This commit is contained in:
lor6 2017-04-03 05:18:07 +03:00 committed by KevinGilmore
parent 77d270a4b1
commit 1a05305b31
6 changed files with 67 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.failureanalyzer;
public class MyDAO {
}

View File

@ -0,0 +1,8 @@
package com.baeldung.failureanalyzer;
import org.springframework.stereotype.Repository;
@Repository("myDAO")
public class MySecondDAO {
}

View File

@ -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;
}

View File

@ -0,0 +1 @@
org.springframework.boot.diagnostics.FailureAnalyzer=com.baeldung.failureanalyzer.MyBeanNotOfRequiredTypeFailureAnalyzer