From 226ee61dea7fa5a7156b57a339c6a5db1d20fc8a Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 16 Mar 2021 16:13:40 +0000 Subject: [PATCH] Create exception reporters when needed so they pick up current state Fixes gh-25691 --- .../boot/SpringApplication.java | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index d557d8dd89..4c7fd8ecf3 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -299,7 +299,6 @@ public class SpringApplication { StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; - Collection 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 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 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 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.