Skip to content

Commit 226ee61

Browse files
committed
Create exception reporters when needed so they pick up current state
Fixes gh-25691
1 parent df1d1db commit 226ee61

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,6 @@ public ConfigurableApplicationContext run(String... args) {
299299
StopWatch stopWatch = new StopWatch();
300300
stopWatch.start();
301301
ConfigurableApplicationContext context = null;
302-
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
303302
configureHeadlessProperty();
304303
SpringApplicationRunListeners listeners = getRunListeners(args);
305304
listeners.starting();
@@ -309,8 +308,6 @@ public ConfigurableApplicationContext run(String... args) {
309308
configureIgnoreBeanInfo(environment);
310309
Banner printedBanner = printBanner(environment);
311310
context = createApplicationContext();
312-
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
313-
new Class[] { ConfigurableApplicationContext.class }, context);
314311
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
315312
refreshContext(context);
316313
afterRefresh(context, applicationArguments);
@@ -322,15 +319,15 @@ public ConfigurableApplicationContext run(String... args) {
322319
callRunners(context, applicationArguments);
323320
}
324321
catch (Throwable ex) {
325-
handleRunFailure(context, ex, exceptionReporters, listeners);
322+
handleRunFailure(context, ex, listeners);
326323
throw new IllegalStateException(ex);
327324
}
328325

329326
try {
330327
listeners.running(context);
331328
}
332329
catch (Throwable ex) {
333-
handleRunFailure(context, ex, exceptionReporters, null);
330+
handleRunFailure(context, ex, null);
334331
throw new IllegalStateException(ex);
335332
}
336333
return context;
@@ -800,7 +797,7 @@ private void callRunner(CommandLineRunner runner, ApplicationArguments args) {
800797
}
801798

802799
private void handleRunFailure(ConfigurableApplicationContext context, Throwable exception,
803-
Collection<SpringBootExceptionReporter> exceptionReporters, SpringApplicationRunListeners listeners) {
800+
SpringApplicationRunListeners listeners) {
804801
try {
805802
try {
806803
handleExitCode(context, exception);
@@ -809,7 +806,7 @@ private void handleRunFailure(ConfigurableApplicationContext context, Throwable
809806
}
810807
}
811808
finally {
812-
reportFailure(exceptionReporters, exception);
809+
reportFailure(context, exception);
813810
if (context != null) {
814811
context.close();
815812
}
@@ -821,9 +818,9 @@ private void handleRunFailure(ConfigurableApplicationContext context, Throwable
821818
ReflectionUtils.rethrowRuntimeException(exception);
822819
}
823820

824-
private void reportFailure(Collection<SpringBootExceptionReporter> exceptionReporters, Throwable failure) {
821+
private void reportFailure(ApplicationContext context, Throwable failure) {
825822
try {
826-
for (SpringBootExceptionReporter reporter : exceptionReporters) {
823+
for (SpringBootExceptionReporter reporter : getExceptionReporters(context)) {
827824
if (reporter.reportException(failure)) {
828825
registerLoggedException(failure);
829826
return;
@@ -839,6 +836,19 @@ private void reportFailure(Collection<SpringBootExceptionReporter> exceptionRepo
839836
}
840837
}
841838

839+
private Collection<SpringBootExceptionReporter> getExceptionReporters(ApplicationContext context) {
840+
try {
841+
if (context != null) {
842+
return getSpringFactoriesInstances(SpringBootExceptionReporter.class,
843+
new Class[] { ConfigurableApplicationContext.class }, context);
844+
}
845+
}
846+
catch (Throwable ex) {
847+
// Continue
848+
}
849+
return Collections.emptyList();
850+
}
851+
842852
/**
843853
* Register that the given exception has been logged. By default, if the running in
844854
* the main thread, this method will suppress additional printing of the stacktrace.

0 commit comments

Comments
 (0)