执行SpringApplication.run方法:
A.initialize--deduceWebEnvironment--"javax.servlet.Servlet","org.springframework.web.context.ConfigurableWebApplicationContext"
--setInitializers--getSpringFactoriesInstances-获取ApplicationContextInitializer类型对象的列表,
- SpringFactoriesLoader.loadFactoryNames(type, classLoader))--来获取所有Spring Factories的名字,然后调用createSpringFactoriesInstances方法根据读取到的名字创建对象,并排序
--deduceMainApplicationClass--判断是否是main方法
B.StopWatch stopWatch.start(); --主要是记录时间和避免重复调用run()方法
C.configureHeadlessProperty--java.awt.headless-- Headless模式是在缺少显示屏、键盘或者鼠标时的系统配置
D.SpringApplicationRunListeners--getRunListeners--getSpringFactoriesInstances--createSpringFactoriesInstances 监听器创建
E.SpringApplicationRunListener.started();---启动监听----在run方法首次启动时立即调用。可以用于非常早期的初始化
--environmentPrepared(ConfigurableEnvironment environment);--在环境准备好后调用
--contextPrepared(ConfigurableApplicationContext context);--在ApplicationContext创建并准备好之后调用,但在加载源之前调用
--contextLoaded(ConfigurableApplicationContext context);--在应用程序上下文加载后但在刷新之前调用。
--finished(ConfigurableApplicationContext context, Throwable exception);--在run方法完成之前立即调用
这5个接口方法在SpringApplicationRunListeners类中实现,每个方法都循环通知所有监听器,也即所说的广播
F.createAndRefreshContext(listeners, applicationArguments))--创建并根据启动时传入的参数更新context
--1.getOrCreateEnvironment--this.webEnvironment ? new StandardServletEnvironment() : new StandardEnvironment()--创建并配置环境变量,如果可以get则get,get为null就创建一个
--2.configureEnvironment--configurePropertySources,configureProfiles--将args(启动时的参数)设置到environment中
--3.environmentPrepared--通知监听器,ApplicationEnvironmentPreparedEvent事件
--5. isWebEnvironment-- convertToStandardEnvironment--环境判断
--6.bannerMode--printBanner--输出Spring图形启动画面
--6.createApplicationContext--this.webEnvironment ? "org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext" : "org.springframework.context.annotation.AnnotationConfigApplicationContext")-- 创建完上下文并实例化
--7.postProcessApplicationContext--向context注册beanNameGenerator、resourceLoader、ClassLoader
--8.applyInitializers(ConfigurableApplicationContext context)--应用Initializers初始化器
--9.SpringApplicationRunListeners.contextPrepared --EventPublishingRunListener发布contextPrepare事件,因为此事件为空,所以此次未做任何操作
--10.logStartupInfo
--11.ConfigurableApplicationContext.getBeanFactory().registerSingleton --“springApplicationArguments”--把applicationArguments注册到context中,且为单例模式
--13.getSources--加载sources
--14.load--createBeanDefinitionLoader--getBeanDefinitionRegistry(context)----加载sources
--15.contextLoaded--发布ApplicationPreparedEvent事件给各个监听器
--16.refresh--这里synchronized(this.startupShutdownMonitor)加锁处理了
-----16.1.prepareRefresh --日志开关,环境变量校验,时间戳记录--保存 java 环境键值,保存系统环境键值
-----16.2.obtainFreshBeanFactory--获取(或创建) BeanFactory--BeanFactory负责 bean 的创建、依赖注入和初始化,bean 的各项特征由 BeanDefinition 定义--BeanDefinition 会存入 BeanFactory 中的 beanDefinitionMap
-----16.3.prepareBeanFactory--完善 BeanFactory为它的各项成员变量赋值
---beanExpressionResolver--用来解析 SpEL,常见实现为 StandardBeanExpressionResolver
---beanPostProcessors 是 bean 后处理器集合,会工作在 bean 的生命周期各个阶段,此处会添加两个
---ApplicationContextAwareProcessor 用来解析 Aware 接口
---ApplicationListenerDetector 用来识别容器中 ApplicationListener 类型的 bean
-----16.4.postProcessBeanFactory(beanFactory);--空实现,留给子类扩展--模板方法设计模式
-----16.5.invokeBeanFactoryPostProcessors(beanFactory);--调用 beanFactory 后处理器--充当 beanFactory 的扩展点,可以用来补充或修改 BeanDefinition
--常见的 beanFactory **后处理器有**
-- ConfigurationClassPostProcessor – 解析 @Configuration、@Bean、@Import、@PropertySource 等
-- PropertySourcesPlaceHolderConfigurer – 替换 BeanDefinition 中的 ${ }
--MapperScannerConfigurer – 补充 Mapper 接口对应的 BeanDefinition
-----16.6.registerBeanPostProcessors(beanFactory);--继续从 beanFactory 中找出 bean 后处理器,添加至 beanPostProcessors 集合中
--bean 后处理器,充当 bean 的扩展点,可以工作在 bean 的实例化、依赖注入、初始化阶段,常见的有
--AutowiredAnnotationBeanPostProcessor 功能有:解析 @Autowired,@Value 注解
--CommonAnnotationBeanPostProcessor 功能有:解析 @Resource,@PostConstruct,@PreDestroy
--AnnotationAwareAspectJAutoProxyCreator 功能有:为符合切点的目标 bean 自动创建代理
-----16.7.initMessageSource();--为 ApplicationContext 添加 messageSource 成员,实现国际化功能
-----16.8.initApplicationEventMulticaster();--添加事件广播器成员,即 applicationContextEventMulticaster--ApplicationContext.publishEvent(事件对象) 来发布事件
-----16.9.onRefresh();--空实现,留给子类扩展--子类在这里准备了 WebServer,即内嵌 web 容器--模板方法设计模式
-----16.10.registerListeners();--从多种途径找到事件监听器,并添加至 applicationEventMulticaster--要实现事件监听器,只需要实现 ApplicationListener 接口,重写其中 onApplicationEvent(E e) 方法即可
-----16.11.finishBeanFactoryInitialization(beanFactory);--将 beanFactory 的成员补充完毕,并初始化所有非延迟单例 bean
--conversionService 也是一套转换机制,作为对 PropertyEditor 的补充
--embeddedValueResolvers 即内嵌值解析器,用来解析 @Value 中的 ${ },借用的是 Environment 的功能
--singletonObjects 即单例池,缓存所有单例对象
-----16.12.finishRefresh();--会为 ApplicationContext 添加 lifecycleProcessor 成员,用来控制容器内需要生命周期管理的 bean
--调用 context 的 start,即可触发所有实现 LifeCycle 接口 bean 的 start,stop,即可触发所有实现 LifeCycle 接口 bean 的 stop
--发布 ContextRefreshed 事件,整个 refresh 执行完成
-----16.13.resetCommonCaches--缓存清空
--17.判断registerShutdownHook--registerShutdownHook--注册钩子,避免程序异常关闭资源不释放
G.afterRefresh(context, (ApplicationArguments)applicationArguments)---调用所有ApplicationRunner和CommandLineRunner的Runner,Order接口实现的排序规则排序,不通的runner类型调用不通的callRunner方法
H.listeners.finished(context, (Throwable)null);--广播ApplicationReadyEvent事件,exception参数为null,所以是ApplicationReadyEvent事件
I.stopWatch.stop();--停止stopWatch,会计算出启动耗时