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

Loading…
Cancel
Save