SpringApplication
的職責是建立一個ApplicationContext
SpringFactoriesLoader
加載相似於SPI機制
Spring會去找META-INF/spring.factories
文件(文件存在多個),找到對應的實現類的全類名進行加載java
建立
ApplicationContext`的過程SpringApplication.initialize()
javax.servlet.Servlet
org.springframework.web.context.ConfigurableWebApplicationContext
spring會去判斷這倆個類是否都存在,都存在的話就會設置webEnvironment爲Trueweb
ApplicationContextInitializer
經過SpringFactoriesLoader加載機制,將 org.springframework.context.ApplicationContextInitializer
對應的Context進行加載 > org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer
> org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer
> org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer
> org.springframework.boot.context.ContextIdApplicationContextInitializer
> org.springframework.boot.context.config.DelegatingApplicationContextInitializer
> org.springframework.boot.context.embedded.ServerPortInfoApplicationContextInitializer
spring
Listener
經過SpringFactoriesLoader加載機制,將org.springframework.context.ApplicationListener
對應的Listener進行加載bootstrap
org.springframework.boot.autoconfigure.BackgroundPreinitializer
org.springframework.cloud.bootstrap.BootstrapApplicationListener
org.springframework.cloud.bootstrap.LoggingSystemShutdownListener
org.springframework.cloud.context.restart.RestartListener
org.springframework.boot.ClearCachesApplicationListener
org.springframework.boot.builder.ParentContextCloserApplicationListener
org.springframework.boot.context.FileEncodingApplicationListener
org.springframework.boot.context.config.AnsiOutputApplicationListener
org.springframework.boot.context.config.ConfigFileApplicationListener
org.springframework.boot.context.config.DelegatingApplicationListener
org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener
org.springframework.boot.logging.ClasspathLoggingApplicationListener
org.springframework.boot.logging.LoggingApplicationListener
ui
Spring會拿當前線程的堆棧信息,而後遍歷堆棧中的方法幀,查看是否有方法名等於main的方法幀spa
SpringApplication.run()
SpringApplicationRunListener
SpringApplicationRunListener
會在在SpringApplication
的run()
方法中建立ApplicationContext
的各個步驟作相應的處理SpringApplicationRunListeners
是SpringApplicationRunListener
的集合類SpringApplicationRunListeners
類負責管理在SpringApplication
的run()
方法中建立ApplicationContext
的各個步驟,一共有5個步驟,starting()
->environmentPrepared()
->contextPrepared()
->contextLoaded()
->finished()
,在每一個步驟中會調用具體的SpringApplicationRunListener
實現類去作相應的事情命令行
經過SpringFactoriesLoader加載機制,將org.springframework.boot.SpringApplicationRunListener
對應的SpringApplicationRunListener
進行加載線程
EventPublishingRunListener
rest
SpringApplicationRunListeners.start()
方法 在該方法中利用了SpringApplicationRunListener
實現類EventPublishingRunListener
向ApplicationListener
實現類廣播了ApplicationStartedEvent
事件響應了
ApplicationStartedEvent
事件的ApplicationListener
org.springframework.boot.logging.LoggingApplicationListener
org.springframework.boot.context.config.DelegatingApplicationListener
org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener
code
Environment
以及對Environment
進行相應的配置
Environment
接口表明Spring應用的上下文環境,保存了Spring應用的配置文件和系統屬性
Environment
會根據是否在Web環境上示例化對應Environment
實現類,若是是Web環境,就建立StandardServletEnvironment
,不是則建立StandardEnvironment
,StandardServletEnvironment
對Servlet
、JNDI
的屬性進行了設置Environment
當中,具體方法SpringApplication.configurePropertySources()
能夠利用
SpringApplication.addCommandLineProperties
屬性對命令行參數進行禁用
Environment
的activeProfiles
,從spring.profiles.active
屬性中讀取SpringApplicationRunListeners.environmentPrepared()
方法 在該方法中利用了SpringApplicationRunListener
實現類EventPublishingRunListener
向ApplicationListener
實現類廣播了ApplicationEnvironmentPreparedEvent
事件 >響應了ApplicationEnvironmentPreparedEvent
事件的ApplicationListener
>org.springframework.cloud.bootstrap.BootstrapApplicationListener
>org.springframework.cloud.bootstrap.LoggingSystemShutdownListener
>org.springframework.boot.context.config.ConfigFileApplicationListener
>org.springframework.boot.context.config.AnsiOutputApplicationListener
>org.springframework.boot.logging.LoggingApplicationListener
>org.springframework.boot.autoconfigure.BackgroundPreinitializer
>org.springframework.boot.logging.ClasspathLoggingApplicationListener
>org.springframework.boot.context.config.DelegatingApplicationListener
>org.springframework.boot.context.FileEncodingApplicationListener
ApplicationContext
以及對ApplicationContext
進行相應的配置ApplicationContext
會根據是否在Web環境上示例化對應ApplicationContext
實現類,若是是Web環境,就建立AnnotationConfigEmbeddedWebApplicationContext
,不是則建立AnnotationConfigApplicationContext
。Environment
設置進ApplicationContext
ApplicationContext
進行前置處理 若是指定了BeanNameGenerator
和ResourceLoader
,會設置到ApplicationContext
ApplicationContextInitializer
org.springframework.cloud.bootstrap.BootstrapApplicationListener$AncestorInitializer
org.springframework.cloud.bootstrap.BootstrapApplicationListener$DelegatingEnvironmentDecryptApplicationInitializer
org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration$$EnhancerBySpringCGLIB$$d551191c
org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer
org.springframework.boot.context.config.DelegatingApplicationContextInitializer
org.springframework.boot.context.ContextIdApplicationContextInitializer
org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer
org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer
org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer
org.springframework.boot.context.embedded.ServerPortInfoApplicationContextInitializer
SpringApplicationRunListeners.contextPrepared()
方法bean
,DefaultApplicationArguments
sources
到ApplicationContext
SpringApplicationRunListeners.contextLoaded()
方法 在這一步首先會將如今的ApplicationListener
註冊到ApplicationContext
當中,而後再利用了SpringApplicationRunListener
實現類EventPublishingRunListener
向ApplicationListener
實現類廣播了ApplicationPreparedEvent
事件添加到
ApplicationContext
的ApplicationListener
org.springframework.cloud.bootstrap.BootstrapApplicationListener
org.springframework.cloud.bootstrap.LoggingSystemShutdownListener
org.springframework.boot.context.config.ConfigFileApplicationListener
org.springframework.boot.context.config.AnsiOutputApplicationListener
org.springframework.boot.logging.LoggingApplicationListener
org.springframework.boot.autoconfigure.BackgroundPreinitializer
org.springframework.boot.logging.ClasspathLoggingApplicationListener
org.springframework.cloud.context.restart.RestartListener
org.springframework.boot.context.config.DelegatingApplicationListener
org.springframework.boot.builder.ParentContextCloserApplicationListener
org.springframework.boot.ClearCachesApplicationListener
org.springframework.boot.context.FileEncodingApplicationListener
org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener
響應了ApplicationPreparedEvent
事件的ApplicationListener
org.springframework.boot.context.config.ConfigFileApplicationListener
org.springframework.boot.logging.LoggingApplicationListener
org.springframework.cloud.context.restart.RestartListener
org.springframework.boot.context.config.DelegatingApplicationListener
refreshContext()
afterRefresh()
執行實現了ApplicationRunner
和CommandLineRunner
的beanSpringApplicationRunListeners.finished()
方法