public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
/**
* 获取factory.properties下的所有配置SpringApplicationRunListener对象并封装为SpringApplicationRunListeners对象
*/
SpringApplicationRunListeners listeners = getRunListeners(args);
//执行所有的SpringApplicationRunListener的starting方法
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
/**
* 加载所有的配置属性, application.properties,application.yml,和其他外部配置属性
* 执行spring.factories中的指定EnvironmentPostProcessor配置的环境初始化类如:EnvironmentPostProcessor
*/
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
configureIgnoreBeanInfo(environment);
//打印banner
Banner printedBanner = printBanner(environment);
//创建容器
context = createApplicationContext();
//创建异常报告器
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
/**
* 准备容器,组件对象之间进行关联,通知监听器准备执行容器初始化
* spring-cloud 实现自定义环境配置,
* 自定义的PropertySourceLocator实现环境变量设置。
*/
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
//初始化容器-执行spring.factories中的指定EnableAutoConfiguration配置的@Configure的@Bean方法
refreshContext(context);
//初始化后之后执行,默认实现为空
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
//通知监听器容器初始化结束
listeners.started(context);
//调用执行ApplicationRunner 和 CommandLineRunner 的运行方法
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;
}
spring-boot初始化过程
最新推荐文章于 2024-05-30 10:31:44 发布