Spring Boot笔记

1,ApplicationContextInitializer,用于在Spring上下文刷新前初始化。此时已经创建了ApplicationContext,但是没有refresh(),ApplicationContextInitializer对ApplicationContext进行初始操作。

2.SpringApplicationRunListener,它广播ApplicationContext运行的每个周期中的事件,以便ApplicationListener可以监控这些事件。

3.Runner,Spring上下文后处理runner可以是两个接口的实现类:org。spring framework . boot . applicationrunnerorg . spring framework . boot . command line runner .其实没什么区别,只是接口中run方法接受的参数类型不同。一种是打包的ApplicationArguments类型,另一种是直接字符串不定数组类型。因此,可以根据需要选择相应的接口。

SpringBoot启动时,无论调用什么方法,都会构造一个SpringApplication的实例,然后调用这个实例的run方法,也就是启动SpringBoot。

在调用run方法之前,也就是构造SpringApplication的时候,会进行初始化,初始化的时候会做以下事情:

SpringApplication构造完成后,调用run方法启动SpringApplication。当执行run方法时,它将执行以下操作:

enable auto configuration import selector是在@ springbootappication标签中引入的,其中调用了selectImports()方法。方法中调用org . spring framework . boot . auto configure . enableautoconfigurationimportselector # getCandidateconfigurations方法。SpringFactoryLoader用于在META-INF文件夹的spring.factories文件中加载以EnableAutoConfiguration为关键字的文件。加载的文件都是java config配置文件(默认配置),以及@ conditional(类

一个相对基础且重要的类,运行并加载MATE-INF中的spring.factories文件。

@Conditional标签是所有条件相关标签的根。源代码中的条件标记由ConditionEvaluator解析,如下:org . spring framework . context . annotation . annotated bean定义阅读器# registerbean (java.lang.class

初始化AnnotationConfigurationApplicationContext时,会注册ConfigurationClassPostProcessor和AutoWired AnnotationBeanpostprocessor等类。如下

ConfigurationClassPostProcessor是一个BeanFactoryPostProcessor,所以它会在创建后对BeanDefinitionRegistry或BeanDefinition进行后处理(BeanFactory已在refresh方法中创建,具体取决于它运行的位置)。

导入解析原理是基于configurationclasspostprocessor,configurationclasspostprocessor的加载过程参考上面,主要分析为什么@import标签引入配置却能调用选择器的方法org . spring framework . context . annotation . configurationclasspostprocess。或者# postprocessbeandeditionregistry org . spring framework . context . annotation . configurationclasspostprocessor # processconfigbeandeditions org . spring framework . context . annotation . configurationclassparser # parse(Java . util . set & lt;org . spring framework . beans . factory . config . bean definition holder & gt;)org . spring framework . context . annotation . configurationclassparser # processDeferredimportselectors然后,调用selectImports方法如下。