1.啟動類里面調用SpringApplication.run(xxx.class,args)方法 2.在SpringApplicaiton的run方法中有兩個步驟,首先創建SpringApplicaiton對象,然后再調用run方法。 3.在SpringApplicaiton構造器中調用initialize(sources)方法。 4.initialize方法中, a.將sources轉換成list加到this.sources屬性中 b.判斷是否為web環境(在類路徑下是否可以加載到Servlet和ConfigurableWebApplicationContext) c.加載Initializers(通過META-INF/spring.factories中鍵為ApplicationContextInitializer的配置進行加載),dubug發現一共 加載了6個initializer(spring-boot-1.5.10.RELEASE.jar中4個,spring-boot-autoconfigure-1.5.10.RELEASE.jar中2個) d.加載ApplicationListener(也是通過META-INF/spring.factories),debug發現共加載了10個 e.通過尋找main方法找到啟動主類。 5.run方法中 a.StopWatch主要是監控啟動過程,統計啟動時間,檢測應用是否已經啟動或者停止。 b.加載SpringApplicationRunListener(也是通過META-INF/spring.factories),默認加載的是EventPublishingRunListener c.調用RunListener.starting()方法。 d.根據args創建應用參數解析器ApplicationArguments; e.准備環境變量:獲取環境變量environment,將應用參數放入到環境變量持有對象中,監聽器監聽環境變量對象的變化(listener.environmentPrepared) f.打印Banner信息(SpringBootBanner) g.創建SpringBoot的應用上下文(AnnotationConfigEmbeddedWebApplicationContext) h.prepareContext上下文之前的准備 i.refreshContext刷新上下文 j.afterRefresh(ApplicationRunner,CommandLineRunner接口實現類的啟動) k.返回上下文對象
構造器中
//保存主配置類
//判斷當前是否是一個web應用
//從類路徑下找到META-INF/spring.facories配置的所有Initializer,保存
//找到所有的listener保存
run方法中
//獲取srpingApplicationRunListeners 也是META-INF
//回調所有listener的starting()方法
//封裝命令行參數
//創建運行環境,回調所有listener的environmentPrepared()方法
//打印banner
//創建ioc容器
//准備上下文,將運行環境保存其中,根據之前保存的Initializer,調用器initialize(),回調所有listener的contextPrepared()方法
//prepareContext中回調listener的contextLoaded()方法
//初始化ioc容器
//從ioc中獲取ApplicationRunner和CommandLineRunner,進行回調
//回調listener的finish()方法
幾個重要的事件回調機制
配置在META-INFO/spring.factories中的
ApplicationContextInitializer
SpringApplicationRunListener
放在ioc容器中的
ApplicationRunner
CommandLineRunner
