前言
上一章讲到了项目启动之后,整个环境的创建以及事件监听的启动-SpringBoot启动流程(1) | 七日打卡。接下来就到了
Context
部分了,
流程讲解
今天的打卡讲解其中一部分(也算是整个环境搭建的重点)-开搞
代码部分:
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,new Class[] { ConfigurableApplicationContext.class }, context);
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
流程一
首先根据上文的webApplicationType
类型,利用反射创建出对应的上下文(这里即是AnnotationConfigServletWebServerApplicationContext
)。
流程二
从当前类加载器中获取SpringBootExceptionReporter
类型的Bean
。该Bean
是用户自定义异常处理回调接口。
流程三
- 设置环境(
environment
)至(context
)中。 - 将相关的后处理设置入
context
中,例如转化器实例,Bean
名称生成器等等。 - 通过
Initializers
集合操作(context
)添加后处理器以及Initializer
操作等。 - 注册
DefaultApplicationArguments
对象至BeanFactory
中。 - 注册
SpringBootBanner
对象至BeanFactory
中。 - 设置
allowBeanDefinitionOverriding
值。 - 构建
BeanDefinitionLoader
。
- 构建
AnnotatedBeanDefinitionReader
实例。 - 构建
XmlBeanDefinitionReader
实例。 - 构建
ClassPathBeanDefinitionScanner
实例。 registerDefaultFilters
。这里包括 includeFilters添加成员:
1.Component注解
2.javax.annotation.ManagedBean注解-JSR-250
3.javax.inject.Named注解-JSR-330setEnvironment
以及conditionEvaluator
=null
。- 构建
ConditionEvaluator
实例(Conditional annotation
)。 - 判断所需要的bean后处理器是否存在
registry
中。
- 构建
- 调用
BeanDefinitionLoader
的load()
。- 构建·StandardAnnotationMetadata`(含有启动类上所有注解信息)。
- 判断是否含有
Conditional
。 - 设置单例还是多例(默认单例)。
- 判断
Lazy
属性,Primary
属性,DependsOn
属性,Role
属性,Description
属性。 - 创建
BeanDefinitionHolder
。 - 判断是否要进行
bean
的ScopedProxyMode
。 - 注册
DefinitionHolder
。
context.addApplicationListener(listener)
(PropertySourceOrderingPostProcessor
注册springBootLoggingSystem
)。
流程完结
上述的代码主要是将整个
Bean
的环境准备好,为下面解析Bean
以及创建出来做出准备。下面一节将会讲解到。