diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/report/AutoConfigurationReportApplicationContextInitializer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/report/AutoConfigurationReportApplicationContextInitializer.java index d8a4f8b714..2c524e97c3 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/report/AutoConfigurationReportApplicationContextInitializer.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/report/AutoConfigurationReportApplicationContextInitializer.java @@ -39,7 +39,9 @@ public class AutoConfigurationReportApplicationContextInitializer implements } @Override - public void handle(SpringApplication springApplication, String[] args, Throwable e) { + public void handleError(SpringApplication application, + ConfigurableApplicationContext applicationContext, String[] args, + Throwable exception) { if (this.report != null) { this.report.initialize(); // salvage a report if possible } diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index 17a7acbdc9..26ab455528 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -168,7 +168,7 @@ public class SpringApplication { private boolean webEnvironment; - private Collection> initializers; + private Set> initializers; private Map defaultProperties; @@ -258,9 +258,9 @@ public class SpringApplication { StopWatch stopWatch = new StopWatch(); stopWatch.start(); + ConfigurableApplicationContext context = null; try { - // Call all non environment aware initializers very early callNonEnvironmentAwareSpringApplicationInitializers(args); @@ -280,7 +280,7 @@ public class SpringApplication { } // Create, load, refresh and run the ApplicationContext - ConfigurableApplicationContext context = createApplicationContext(); + context = createApplicationContext(); context.registerShutdownHook(); context.setEnvironment(environment); postProcessApplicationContext(context); @@ -301,24 +301,26 @@ public class SpringApplication { runCommandLineRunners(context, args); return context; } - catch (RuntimeException e) { - handle(e, args); - throw e; + catch (RuntimeException ex) { + handleError(context, args, ex); + throw ex; } - catch (Error e) { - handle(e, args); - throw e; + catch (Error ex) { + handleError(context, args, ex); + throw ex; } } - private void handle(Throwable e, String... args) { + private void handleError(ConfigurableApplicationContext context, String[] args, + Throwable exception) { List> initializers = new ArrayList>( getInitializers()); Collections.reverse(initializers); for (ApplicationContextInitializer initializer : initializers) { if (initializer instanceof SpringApplicationErrorHandler) { - ((SpringApplicationErrorHandler) initializer).handle(this, args, e); + ((SpringApplicationErrorHandler) initializer).handleError(this, context, + args, exception); } } } @@ -703,7 +705,8 @@ public class SpringApplication { */ public void setInitializers( Collection> initializers) { - this.initializers = new ArrayList>(initializers); + this.initializers = new LinkedHashSet>( + initializers); } /** @@ -716,12 +719,12 @@ public class SpringApplication { } /** - * Returns readonly list of the {@link ApplicationContextInitializer}s that will be + * Returns readonly set of the {@link ApplicationContextInitializer}s that will be * applied to the Spring {@link ApplicationContext}. * @return the initializers */ - public List> getInitializers() { - return new ArrayList>(this.initializers); + public Set> getInitializers() { + return Collections.unmodifiableSet(this.initializers); } /** diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplicationErrorHandler.java b/spring-boot/src/main/java/org/springframework/boot/SpringApplicationErrorHandler.java index 99b937689e..7df8f6bd30 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplicationErrorHandler.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplicationErrorHandler.java @@ -16,21 +16,27 @@ package org.springframework.boot; +import org.springframework.context.ConfigurableApplicationContext; + /** - * Strategy interface that can be used to capture errors in a {@link SpringApplication} - * after it fails to start up. + * Strategy interface that can be used on a {@link SpringApplicationInitializer} to + * capture errors in a {@link SpringApplication} after it fails to start up. * * @author Dave Syer + * @see SpringApplicationInitializer */ public interface SpringApplicationErrorHandler { /** - * Finalize the application. - * - * @param springApplication the spring application. + * Handle an application startup error. + * @param application the spring application. + * @param applicationContext the spring context (if one was created, may be + * {@code null}) * @param args the run arguments - * @param e an exception thrown during startup (or null if none) + * @param exception an exception thrown during startup (or null if none) */ - void handle(SpringApplication springApplication, String[] args, Throwable e); + void handleError(SpringApplication application, + ConfigurableApplicationContext applicationContext, String[] args, + Throwable exception); }