SpringApplication 运行及源码解读
SpringApplication.run
@SpringBootApplication
public class AdvancedApplication {
public static void main(String[] args) {
SpringApplication.run(AdvancedApplication.class, args);
}
}源码分析
public ConfigurableApplicationContext run(String... args) {
//1.
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
//2.
configureHeadlessProperty();
//3.
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
//4.
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
//5.
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
//6.
context = createApplicationContext();
//7.
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
//8.
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
//9.
refreshContext(context);
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}Last updated