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方法如下。