SpringBootExceptionReporter
用于捕获和处理启动期间的异常,例如应用程序上下文的初始化失败。我们业务中的异常处理一般使用拦截器进行拦截处理业务异常。
LoggingFailureAnalysisReport
异常处理入口handleRunFailure()
方法
try {
......
}
catch (Throwable ex) {
handleRunFailure()
}
Runtime.getRuntime().addShutdownHook
Runtime.getRuntime().removeShutdownHook
@Component
public class MyExitCodeExceptionMapper implements ExitCodeExceptionMapper {
@Override
public int getExitCode(Throwable exception) {
if (exception instanceof ConnectorStartFailedException) {
return 10;
}
return 0;
}
}
public class MyExceptionReporter implements SpringBootExceptionReporter {
private ConfigurableApplicationContext context;
public MyExceptionReporter(ConfigurableApplicationContext context) {
this.context = context;
}
@Override
public boolean reportException(Throwable failure) {
if (failure instanceof UnsatisfiedDependencyException) {
UnsatisfiedDependencyException exception = (UnsatisfiedDependencyException) failure;
System.out.println("no such bean " + exception.getInjectionPoint().getField().getName() );
}
return false;
}
}