从 SpringApplication 认识 Spring 应用启动过程
一、SpringApplication 是什么?
Spring 应用的启动类。
二、SpringApplication 执行了什么?
创建 ApplicationContext 实例
ApplicationContext 就是我们所说的容器实例。
注册 CommandLinePropertySource
CommandLinePropertySource 的作用是将命令行参数输出为 Spring 属性。
刷新 ApplicationContext
这一步骤包括诸多操作,并且会加载所有的单例 bean。
触发 CommandLineRunner bean
CommandLineRunner 是一个接口,它只有一个 run() 方法。
凡是实现了此接口的类,如果被加载进容器,就会执行其 run() 方法。
容器中可以包含多个实现 CommandLineRunner 的 bean,执行顺序可以遵从 Ordered 接口或者 @Order 注解设置。
三、bean 加载源
SpringApplication 有诸多 bean 加载源:
AnnotatedBeanDefinitionReader
顾名思义,注解 bean 定义读取。
XmlBeanDefinitionReader
xml 配置资源读取。
ClassPathBeanDefinitionScanner
classpath 路径扫描。
GroovyBeanDefinitionReader
... ...
四、SpringApplication 创建
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
this.webApplicationType = WebApplicationType.deduceFromClasspath();
this.bootstrapRegistryInitializers = new ArrayList<>(
getSpringFactoriesInstances(BootstrapRegistryInitializer.class));
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass();
}
1、resourceLoader
参数可以为 null,为 null 时,使用默认:
(this.resourceLoader != null) ? this.resourceLoader: new DefaultResourceLoader(null);
2、primarySources
主要的 bean 定义来源。
3、webApplicationType
web 应用类型判断:
NONE:应用不会以 web 应用运行,且不会启动内嵌 web 服务器。
SERVLET:基于 servlet web 应用,运行于内嵌 web 服务器。
REACTIVE:响应式 web 应用,运行于内嵌 web 服务器。
4、bootstrapRegistryInitializers
BootstrapRegistryInitializer:回调接口,用于 BootstrapRegistry 初始化。
BootstrapRegistry:对象注册器,作用期间为从应用启动,Environment 处理直到 ApplicationContext 完备。
5、setInitializers
ApplicationContextInitializer 列表设置。
ApplicationContextInitializer:回调接口,用于 Spring ConfigurableApplicationContext 初始化。
通常用于 web 应用 ApplicationContext 自定义初始化。如注册 property source、激活 profile 等。
6、setListeners
ApplicationListener 列表设置。
ApplicationListener:应用事件监听接口,基于标准的 EventListener 接口,观察者模式实现。
7、mainApplicationClass
main class
五、SpringApplication.run() 逻辑
public ConfigurableApplicationContext run(String... args) {
long startTime = System.nanoTime();
DefaultBootstrapContext bootstrapContext = createBootstrapContext();
ConfigurableApplicationContext context = null;
configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting(bootstrapContext, this.mainApplicationClass);
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
context.setApplicationStartup(this.applicationStartup);
prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), timeTakenToStartup);
}
listeners.started(context, timeTakenToStartup);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, listeners);
throw new IllegalStateException(ex);
}
try {
Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);
listeners.ready(context, timeTakenToReady);
}
catch (Throwable ex) {
handleRunFailure(context, ex, null);
throw new IllegalStateException(ex);
}
return context;
}
创建,刷新 ApplicationContext 并运行 Spring 应用。
1、startTime
使用 System.nanoTime(),计算耗时间隔更精确。不可用于获取具体时刻。
2、创建启动上下文
DefaultBootstrapContext = createBootstrapContext();
BootstrapContext:启动上下文,生命周期同 BootstrapRegistry。
DefaultBootstrapContext 继承了 BootstrapContext、BootstrapRegistry。
用于 BootstrapRegistry 初始化。
3、ConfigurableApplicationContext
可配置的 ApplicationContext。
4、获取事件监听器
SpringApplicationRunListeners = getRunListeners()。
Spring 应用运行期间事件监听。
5、starting 事件
listeners.starting():starting step。
6、启动参数处理
ApplicationArguments:提供 SpringApplication 启动参数访问。
7、应用环境配置
ConfigurableEnvironment = prepareEnvironment()
private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
DefaultBootstrapContext bootstrapContext, ApplicationArguments applicationArguments) {
// Create and configure the environment
ConfigurableEnvironment environment = getOrCreateEnvironment();
configureEnvironment(environment, applicationArguments.getSourceArgs());
ConfigurationPropertySources.attach(environment);
listeners.environmentPrepared(bootstrapContext, environment);
DefaultPropertiesPropertySource.moveToEnd(environment);
Assert.state(!environment.containsProperty("spring.main.environment-prefix"),
"Environment prefix cannot be set via properties.");
bindToSpringApplication(environment);
if (!this.isCustomEnvironment) {
EnvironmentConverter environmentConverter = new EnvironmentConverter(getClassLoader());
environment = environmentConverter.convertEnvironmentIfNecessary(environment, deduceEnvironmentClass());
}
ConfigurationPropertySources.attach(environment);
return environment;
}
configureEnvironment() 模板方法,代理执行 configurePropertySources() 及 configureProfiles() 方法。
configurePropertySources():PropertySource 配置,用于添加、移除或者调序 PropertySource 资源。CommandLinePropertySource 在这一步处理。
configureProfiles():应用 profile 设置。
ConfigurationPropertySources.attach(environment)
ConfigurationPropertySources:提供对 ConfigurationPropertySource 的访问。
attach(environment):就是将这个功能提供给 environment。
listeners.environmentPrepared(bootstrapContext, environment)
environment-prepared step。
DefaultPropertiesPropertySource.moveToEnd(environment)
DefaultPropertiesPropertySource:是一个 MapPropertySource,包含 SpringApplication 可以使用的一些默认属性。为了使用方便,默认会置于尾序。
bindToSpringApplication(environment)
将 environment 绑定到 SpringApplication。
Binder:用于对象绑定的容器。
8、configureIgnoreBeanInfo()
9、打印 Banner
printBanner()。
10、创建 ApplicationContext
createApplicationContext()。
内部通过 ApplicationContextFactory 创建。
ApplicationContextFactory:策略接口,默认实现为 DefaultApplicationContextFactory。
11、ApplicationStartup 设置
为容器设置 ApplicationStartup,用于记录启动过程性能指标。
12、ApplicationContext 准备
prepareContext()
private void prepareContext(DefaultBootstrapContext bootstrapContext, ConfigurableApplicationContext context,
ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments, Banner printedBanner) {
context.setEnvironment(environment);
postProcessApplicationContext(context);
applyInitializers(context);
listeners.contextPrepared(context);
bootstrapContext.close(context);
if (this.logStartupInfo) {
logStartupInfo(context.getParent() == null);
logStartupProfileInfo(context);
}
// Add boot specific singleton beans
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
if (printedBanner != null) {
beanFactory.registerSingleton("springBootBanner", printedBanner);
}
if (beanFactory instanceof AbstractAutowireCapableBeanFactory) {
((AbstractAutowireCapableBeanFactory) beanFactory).setAllowCircularReferences(this.allowCircularReferences);
if (beanFactory instanceof DefaultListableBeanFactory) {
((DefaultListableBeanFactory) beanFactory)
.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
}
}
if (this.lazyInitialization) {
context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor());
}
context.addBeanFactoryPostProcessor(new PropertySourceOrderingBeanFactoryPostProcessor(context));
// Load the sources
Set<Object> sources = getAllSources();
Assert.notEmpty(sources, "Sources must not be empty");
load(context, sources.toArray(new Object[0]));
listeners.contextLoaded(context);
}
设置环境
postProcessApplicationContext() 前置处理
beanNameGenerator 设置,用于 bean 名称生成。
resourceLoader 设置,用于资源加载。
addConversionService:ConversionService 类型转换 Service。
applyInitializers()
ApplicationContextInitializer 应用
contextPrepared 事件
【spring.boot.application.context-prepared】step
BootstrapContext 关闭
注册 springApplicationArguments bean
注册 springBootBanner bean
AbstractAutowireCapableBeanFactory
设置是否允许 bean 之间的循环依赖,并自动处理,默认为 true。
设置是否允许 bean 定义覆盖,默认为 true。
lazyInitialization 懒加载
设置 LazyInitializationBeanFactoryPostProcessor post-processor。
PropertySource 重排序
设置 PropertySourceOrderingBeanFactoryPostProcessor post-processor。
getAllSources() bean 定义源加载
load() bean 定义加载,BeanDefinitionLoader
用于从底层资源加载 bean 定义信息,包括 xml、JavaConfig。
是基于 AnnotatedBeanDefinitionReader、XmlBeanDefinitionReader、ClassPathBeanDefinitionScanner 的门面模式。
beanNameGenerator、resourceLoader、environment 设置。
资源加载:
private void load(Object source) {
Assert.notNull(source, "Source must not be null");
if (source instanceof Class<?>) {
load((Class<?>) source);
return;
}
if (source instanceof Resource) {
load((Resource) source);
return;
}
if (source instanceof Package) {
load((Package) source);
return;
}
if (source instanceof CharSequence) {
load((CharSequence) source);
return;
}
throw new IllegalArgumentException("Invalid source type " + source.getClass());
}
contextLoaded() contextLoaded 事件
【spring.boot.application.context-loaded】step。
13、ApplicationContext 刷新
refreshContext()
注册 shutdownHook。
Runtime.getRuntime().addShutdownHook(new Thread(this, "SpringApplicationShutdownHook"));
刷新操作:加载或刷新
AbstractApplicationContext::refresh()
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
beanPostProcess.end();
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
contextRefresh.end();
}
}
}
作为启动方法,如果失败,则必须销毁所有已创建的单例bean。
StartupStep【spring.context.refresh】
准备刷新 prepareRefresh()
设置启动日期。
设置 active 标志。
initPropertySources():子类实现 PropertySource 初始化。
validateRequiredProperties():校验 ConfigurablePropertyResolver#setRequiredProperties 设置的必需属性。
obtainFreshBeanFactory():通过子类获取最新的内部 bean factory。如果存在旧的则先销毁,然后再创建新的返回。
prepareBeanFactory() 准备 bean factory
setBeanClassLoader():默认为线程上下文类加载器,用于 bean 定义加载。
setBeanExpressionResolver() spel 表达式解析设置:StandardBeanExpressionResolver。
addPropertyEditorRegistrar():ResourceEditorRegistrar 用于 bean 创建过程。
添加 ApplicationContextAwareProcessor post-processor。
注册依赖:BeanFactory、ResourceLoader、ApplicationEventPublisher、ApplicationContext。
添加 ApplicationListenerDetector post-processor:用于检测发现实现了 ApplicationListener 的 bean。
LoadTimeWeaver 处理。
environment、systemProperties、systemEnvironment、applicationStartup 注册。
postProcessBeanFactory():用于子类实现,修改内部 bean factory。
这一时期,所有的 bean 定义都已被加载,但还未实例化。
StartupStep【spring.context.beans.post-process】
invokeBeanFactoryPostProcessors() 触发所有已注册的 BeanFactoryPostProcessor
registerBeanPostProcessors() 注册 bean post-processor
StartupStep【spring.context.beans.post-process】 结束
initMessageSource() MessageSource 初始化
容器内 bean 名称:messageSource。
存在则检查并设置 ParentMessageSource。
不存在则创建默认 DelegatingMessageSource,设置 ParentMessageSource 并注册。
initApplicationEventMulticaster() 事件分发初始化
容器 bean:applicationEventMulticaster。ApplicationEventMulticaster 接口,用于管理 ApplicationListener,并执行事件分发。
不存在则创建并注册 SimpleApplicationEventMulticaster 对象。
onRefresh()
用于子类初始化一些特有的 bean。
模板方法,用于重写实现刷新逻辑。
registerListeners() 监听器注册
将实现了 ApplicationListener 接口的 bean 注册到容器。
finishBeanFactoryInitialization() 实例化所有余下的单例 bean。
conversionService。
注册内嵌值(${...})解析器。
初始化 LoadTimeWeaverAware。
停用类型匹配 ClassLoader。
freezeConfiguration() 冻结所有的 bean 定义。所有注册的 bean 定义都不允许再有变更。
preInstantiateSingletons() 实例化所有余下的单例 bean。
14、afterRefresh()
ApplicationContext 刷新完毕后调用。
15、StartupInfoLogger
记录应用启动信息。
16、started 事件
listeners.started()
17、Runner 调用
包括 ApplicationRunner 和 CommandLineRunner。
18 ready 事件
listeners.ready()
从 SpringApplication 认识 Spring 应用启动过程的更多相关文章
- Spring Boot启动过程及回调接口汇总
Spring Boot启动过程及回调接口汇总 链接: https://www.itcodemonkey.com/article/1431.html 来自:chanjarster (Daniel Qia ...
- Spring Boot启动过程(四):Spring Boot内嵌Tomcat启动
之前在Spring Boot启动过程(二)提到过createEmbeddedServletContainer创建了内嵌的Servlet容器,我用的是默认的Tomcat. private void cr ...
- Spring Boot启动过程(七):Connector初始化
Connector实例的创建已经在Spring Boot启动过程(四):Spring Boot内嵌Tomcat启动中提到了: Connector是LifecycleMBeanBase的子类,先是设置L ...
- Spring MVC启动过程(1):ContextLoaderListener初始化
此文来自https://my.oschina.net/pkpk1234/blog/61971 (写的特别好)故引来借鉴 Spring MVC启动过程 以Tomcat为例,想在Web容器中使用Spirn ...
- 转:spring的启动过程-spring和springMVC父子容器的原理
要想很好理解这三个上下文的关系,需要先熟悉spring是怎样在web容器中启动起来的.spring的启动过程其实就是其IoC容器的启动过程,对于web程序,IoC容器启动过程即是建立上下文的过程. s ...
- Spring Boot启动过程(三)
我已经很精简了,两篇(Spring Boot启动过程(一).pring Boot启动过程(二))依然没写完,接着来. refreshContext之后的方法是afterRefresh,这名字起的真.. ...
- spring的启动过程就是创建ioc容器的过程
1. spring简介 spring的最基本的功能就是创建对象及管理这些对象之间的依赖关系,实现低耦合.高内聚.还提供像通用日志记录.性能统计.安全控制.异常处理等面向切面的能力,还能帮我们管理最头疼 ...
- Spring Boot启动过程(一)
之前在排查一个线上问题时,不得不仔细跑了很多遍Spring Boot的代码,于是整理一下,我用的是1.4.3.RELEASE. 首先,普通的入口,这没什么好说的,我就随便贴贴代码了: SpringAp ...
- Spring Boot启动过程(二)
书接上篇 该说refreshContext(context)了,首先是判断context是否是AbstractApplicationContext派生类的实例,之后调用了强转为AbstractAppl ...
- 转:Spring Boot启动过程
之前在排查一个线上问题时,不得不仔细跑了很多遍Spring Boot的代码,于是整理一下,我用的是1.4.3.RELEASE. 首先,普通的入口,这没什么好说的,我就随便贴贴代码了: SpringAp ...
随机推荐
- SpringBoot打包成exe(别再用exe4j了,使用JDK自带工具)
SpringBoot打包成exe(别再用exe4j了,使用JDK自带工具) 搜到大部分打包exe的文章都是使用exe4j打包 步骤贼多,安装麻烦,打包麻烦 收费软件,公司使用会吃律师函 JDK14以上 ...
- AWS IAM介绍
前言 AWS是世界上最大的云服务提供商,它提供了很多组件供消费者使用,其中进行访问控制的组件叫做IAM(Identity and Access Management), 用来进行身份验证和对AWS资源 ...
- Django之form表单相关操作
目录 摘要 form表单 form表单的action参数 form表单的method参数 request.method方法 简介 get请求传递数据 post请求传递数据 GET/POST实际应用,简 ...
- [数据库/Java]数据库开发过程中产生的MySQL错误代码及其解决方案
前言 吐槽一下,均是这两天遇到的破烂事儿,搞定了也好,以后出现此类问题也就放心些了. 下列遇到的问题大都是因为MySQL从5.x版本升级到8.0.11(MySQL8.0涉及重大改版)后,跟着连带着出现 ...
- AspectCore和MSDI 实现Name注册以及解析对象
AspectCore 在注册服务这块比较简单,默认是无法根据Name去注册和解析对象,这边做一下这块的扩展 大致原理是根据自定义Name去生成对应的动态类型,然后使用委托或者对象的方式,进行注册 ti ...
- javasec(四)序列化与反序列化基本原理
title: javasec(四)序列化与反序列化基本原理 tags: - javasec - 反序列化 categories: - javasec cover: 'https://blog-1313 ...
- javasec(三)类加载机制
这篇文章介绍java的类加载机制. Java是一个依赖于JVM(Java虚拟机)实现的跨平台的开发语言.Java程序在运行前需要先编译成class文件,Java类初始化的时候会调用java.lang. ...
- Vue的项目打包为移动端(安卓手机应用)app
现在基于vue全家桶技术体系,基本上可以开发各端的各种应用,pc端的应用,开发完成以后,直接运行打包命令 yarn build 即可打包,部署到服务器端上线即可.那么,今天我们来聊一聊,开发好的vue ...
- 获取电脑的网络连接状态(五)WebClient
网络连接判断,使用WebClient测试获取: 1 public static bool IsWebClientConnected() 2 { 3 try 4 { 5 using (var clien ...
- Spring Boot入门项目之外卖
文章目录 呱呱外卖 前言 项目介绍 项目技术栈介绍(主讲后端) 项目功能介绍 项目的代码结构 实体类 控制类(Controller) Service类和Mapper类 缓存 接口文档管理 项目添加的部 ...