Spring BOOT源碼日誌

spring context啓動入口方法:

org.springframework.boot.SpringApplication#run(java.lang.String...)

//加載環境以及propertie文件等常量

org.springframework.boot.SpringApplication#prepareEnvironment

//創建BeanFactory

org.springframework.boot.SpringApplication#createApplicationContext

//爲後續的context中bean的加載初始化一些加載器Bean定義到BeanFactory.

org.springframework.boot.SpringApplication#prepareContext

//裝載Bean

org.springframework.boot.SpringApplication#refreshContext

其中分爲幾個環節:

1)對BeanFactory進行配置

 org.springframework.context.support.AbstractApplicationContext#prepareBeanFactory

2)執行BeanFactory創建後的額外的上下文相關的配置(視具體上下文的定製化邏輯決定,如spring-web,會將BeanFactory引用加入到servletContext中 ),下面是三種context下的例子:

org.springframework.context.support.AbstractApplicationContext#postProcessBeanFactory

org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext#postProcessBeanFactory

org.springframework.web.context.support.GenericWebApplicationContext#postProcessBeanFactory

3)執行前面步驟加入到的BeanFactoryPostProcessor。

org.springframework.context.support.AbstractApplicationContext#invokeBeanFactoryPostProcessors

1.優先執行org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor的processor,可以

加載beandefinition到beanFactory.

優先執行的是ConfigurationClassPostProcessor

org.springframework.context.annotation.ConfigurationClassPostProcessor#processConfigBeanDefinitions

可以加載不同定義路徑的Bean。

org.springframework.context.annotation.ConfigurationClassParser#parse(java.util.Set<org.springframework.beans.factory.config.BeanDefinitionHolder>)

第一種是根據@SpringBootApplication註解獲取相關的bean定義。

@ComponentScan是@SpringBootApplication的別名,所以有SpringBootApplication的註解會自動掃描其項目下的包,根據註解識別是否該創建爲bean。

並且還會掃描註解類的所有註解(含註解類的註解),找到該類下所有註解類爲org.springframework.context.annotation.Import的value值。

org.springframework.context.annotation.ConfigurationClassParser#collectImports

找到所有import的value中使用的類後進行bean的引入,根據value對應的引入類的不同,進行不同的引入策略

org.springframework.context.annotation.ConfigurationClassParser#processImports

分爲三種:

1 org.springframework.context.annotation.ImportSelector

 

使用下方法根據選擇器獲得使用org.springframework.boot.autoconfigure.EnableAutoConfiguration的配置,再加載bean

org.springframework.boot.autoconfigure.AutoConfigurationImportSelector#getAutoConfigurationEntry

2 org.springframework.context.annotation.ImportBeanDefinitionRegistrar

條件引入bean

3 其他

@Configuration的註解引入


 

 

 

org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanDefinitionRegistryPostProcessors

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章