public ConfigurableApplicationContext run(String... args) {
任务执行时长
StopWatch stopWatch = new StopWatch();
stopWatch.start();
Spring 上下文:ConfigurableApplicationContext
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
Headless模式是在缺少显示屏、键盘或者鼠标是的系统配置。
Headless模式是系统的一种配置模式。在系统可能缺少显示设备、键盘或鼠标这些外设的情况下可以使用该模式。
configureHeadlessProperty();
实例化监听spring的监听器,EventPublishingRunListener,根据配置文件,
以及过滤器SpringApplicationRunListener,实例化监听对象,通过class.forName.
SpringApplicationRunListeners listeners = getRunListeners(args);
启动事件监听
listeners.starting();
try {
构造容器参数信息
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
准备环境变量,初始为StandardServletEnvironment
标准的servlet环境。给上边的监听对象,绑定环境变量信息 具体参考里边的方法。
ConfigurableEnvironment environment = prepareEnvironment(listeners,applicationArguments);
通过配置忽略Bean的信息
configureIgnoreBeanInfo(environment);
打印 banner日志。
Banner printedBanner = printBanner(environment);
创建springApplication上下文。根据servlet类型:实例的对象是:
AnnotationConfigServletWebServerApplicationContext
context = createApplicationContext();
从配置文件中加载,加载SpringBootExceptionReporter的子类。
exceptionReporters = getSpringFactoriesInstances(
SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context);
准备上下文信息。
prepareContext(context, environment, listeners, applicationArguments,printedBanner);
刷新容器信息,采用的是spring的加载过程。
refreshContext(context);
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
// 监听执行started方法
listeners.started(context);、
// 执行CommandLineRunner 和ApplicationRunner的方法
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;
}
SpringApplicationRunListener :接口说明
主要完成boot启动的时,执行的自己得一套逻辑代码,可以根据资源,容器的初始话过程,执行不同的逻辑代码信息
public interface SpringApplicationRunListener {
void starting();
/**
* Called once the environment has been prepared, but before the
* {@link ApplicationContext} has been created.
* @param environment the environment
*/
void environmentPrepared(ConfigurableEnvironment environment);
/**
* Called once the {@link ApplicationContext} has been created and prepared, but
* before sources have been loaded.
* @param context the application context
*/
void contextPrepared(ConfigurableApplicationContext context);
/**
* Called once the application context has been loaded but before it has been
* refreshed.
* @param context the application context
*/
void contextLoaded(ConfigurableApplicationContext context);
/**
* The context has been refreshed and the application has started but
* {@link CommandLineRunner CommandLineRunners} and {@link ApplicationRunner
* ApplicationRunners} have not been called.
* @param context the application context.
* @since 2.0.0
*/
void started(ConfigurableApplicationContext context);
/**
* Called immediately before the run method finishes, when the application context has
* been refreshed and all {@link CommandLineRunner CommandLineRunners} and
* {@link ApplicationRunner ApplicationRunners} have been called.
* @param context the application context.
* @since 2.0.0
*/
void running(ConfigurableApplicationContext context);
/**
* Called when a failure occurs when running the application.
* @param context the application context or {@code null} if a failure occurred before
* the context was created
* @param exception the failure
* @since 2.0.0
*/
void failed(ConfigurableApplicationContext context, Throwable exception);
}