Create exception reporters when needed so they pick up current state

Fixes gh-25691
pull/25731/head
Andy Wilkinson 4 years ago
parent df1d1dbaa9
commit 226ee61dea

@ -299,7 +299,6 @@ public class SpringApplication {
StopWatch stopWatch = new StopWatch(); StopWatch stopWatch = new StopWatch();
stopWatch.start(); stopWatch.start();
ConfigurableApplicationContext context = null; ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty(); configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args); SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting(); listeners.starting();
@ -309,8 +308,6 @@ public class SpringApplication {
configureIgnoreBeanInfo(environment); configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment); Banner printedBanner = printBanner(environment);
context = createApplicationContext(); context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
prepareContext(context, environment, listeners, applicationArguments, printedBanner); prepareContext(context, environment, listeners, applicationArguments, printedBanner);
refreshContext(context); refreshContext(context);
afterRefresh(context, applicationArguments); afterRefresh(context, applicationArguments);
@ -322,7 +319,7 @@ public class SpringApplication {
callRunners(context, applicationArguments); callRunners(context, applicationArguments);
} }
catch (Throwable ex) { catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners); handleRunFailure(context, ex, listeners);
throw new IllegalStateException(ex); throw new IllegalStateException(ex);
} }
@ -330,7 +327,7 @@ public class SpringApplication {
listeners.running(context); listeners.running(context);
} }
catch (Throwable ex) { catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null); handleRunFailure(context, ex, null);
throw new IllegalStateException(ex); throw new IllegalStateException(ex);
} }
return context; return context;
@ -800,7 +797,7 @@ public class SpringApplication {
} }
private void handleRunFailure(ConfigurableApplicationContext context, Throwable exception, private void handleRunFailure(ConfigurableApplicationContext context, Throwable exception,
Collection<SpringBootExceptionReporter> exceptionReporters, SpringApplicationRunListeners listeners) { SpringApplicationRunListeners listeners) {
try { try {
try { try {
handleExitCode(context, exception); handleExitCode(context, exception);
@ -809,7 +806,7 @@ public class SpringApplication {
} }
} }
finally { finally {
reportFailure(exceptionReporters, exception); reportFailure(context, exception);
if (context != null) { if (context != null) {
context.close(); context.close();
} }
@ -821,9 +818,9 @@ public class SpringApplication {
ReflectionUtils.rethrowRuntimeException(exception); ReflectionUtils.rethrowRuntimeException(exception);
} }
private void reportFailure(Collection<SpringBootExceptionReporter> exceptionReporters, Throwable failure) { private void reportFailure(ApplicationContext context, Throwable failure) {
try { try {
for (SpringBootExceptionReporter reporter : exceptionReporters) { for (SpringBootExceptionReporter reporter : getExceptionReporters(context)) {
if (reporter.reportException(failure)) { if (reporter.reportException(failure)) {
registerLoggedException(failure); registerLoggedException(failure);
return; return;
@ -839,6 +836,19 @@ public class SpringApplication {
} }
} }
private Collection<SpringBootExceptionReporter> getExceptionReporters(ApplicationContext context) {
try {
if (context != null) {
return getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
}
}
catch (Throwable ex) {
// Continue
}
return Collections.emptyList();
}
/** /**
* Register that the given exception has been logged. By default, if the running in * Register that the given exception has been logged. By default, if the running in
* the main thread, this method will suppress additional printing of the stacktrace. * the main thread, this method will suppress additional printing of the stacktrace.

Loading…
Cancel
Save