mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Polish SpringApplicationErrorHandler
This commit is contained in:
parent
68e07eba5c
commit
a9a6077fdb
@ -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
|
||||
}
|
||||
|
@ -168,7 +168,7 @@ public class SpringApplication {
|
||||
|
||||
private boolean webEnvironment;
|
||||
|
||||
private Collection<ApplicationContextInitializer<?>> initializers;
|
||||
private Set<ApplicationContextInitializer<?>> initializers;
|
||||
|
||||
private Map<String, Object> 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<ApplicationContextInitializer<?>> initializers = new ArrayList<ApplicationContextInitializer<?>>(
|
||||
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<? extends ApplicationContextInitializer<?>> initializers) {
|
||||
this.initializers = new ArrayList<ApplicationContextInitializer<?>>(initializers);
|
||||
this.initializers = new LinkedHashSet<ApplicationContextInitializer<?>>(
|
||||
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<ApplicationContextInitializer<?>> getInitializers() {
|
||||
return new ArrayList<ApplicationContextInitializer<?>>(this.initializers);
|
||||
public Set<ApplicationContextInitializer<?>> getInitializers() {
|
||||
return Collections.unmodifiableSet(this.initializers);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user