您好,登錄后才能下訂單哦!
這篇文章主要介紹了Springboot底層啟動過程是怎樣的的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Springboot底層啟動過程是怎樣的文章都會有所收獲,下面我們一起來看看吧。
1、記錄 BeanDefinition 源
spring容器剛開始是空的,要去各個源找到beanDefinition,這些源可能是配置類,可能是xml文件。在構造方法里會獲取一個主源,也就是引導類,根據引導類去獲取beanDefinition。
2、推斷應用類型
根據jar包去判斷是什么引用類型
3、記錄 ApplicationContext 初始化器
對ApplicationContext做擴展
4、記錄監聽器
監聽重要事件
5、推斷主啟動類
記錄運行的主類。
1、得到 SpringApplicationRunListeners,名字取得不好,實際是事件發布器
發布 application starting 事件,在程序啟動的重要節點發布事件
public static void main(String[] args) throws Exception{ // 添加 app 監聽器 SpringApplication app = new SpringApplication(); app.addListeners(e -> System.out.println(e.getClass())); // 獲取事件發送器實現類名 List<String> names = SpringFactoriesLoader.loadFactoryNames(SpringApplicationRunListener.class, A39_2.class.getClassLoader()); for (String name : names) { System.out.println(name); Class<?> clazz = Class.forName(name); Constructor<?> constructor = clazz.getConstructor(SpringApplication.class, String[].class); SpringApplicationRunListener publisher = (SpringApplicationRunListener) constructor.newInstance(app, args); // 發布事件 DefaultBootstrapContext bootstrapContext = new DefaultBootstrapContext(); publisher.starting(bootstrapContext); // spring boot 開始啟動 publisher.environmentPrepared(bootstrapContext, new StandardEnvironment()); // 環境信息準備完畢 GenericApplicationContext context = new GenericApplicationContext(); publisher.contextPrepared(context); // 在 spring 容器創建,并調用初始化器之后,發送此事件 publisher.contextLoaded(context); // 所有 bean definition 加載完畢 context.refresh(); publisher.started(context); // spring 容器初始化完成(refresh 方法調用完畢) publisher.running(context); // spring boot 啟動完畢 publisher.failed(context, new Exception("出錯了")); // spring boot 啟動出錯 }
2、封裝啟動 args
3、準備 Environment 添加命令行參數(*)
public static void main(String[] args) throws IOException { ApplicationEnvironment env = new ApplicationEnvironment(); // 系統環境變量, properties, yaml env.getPropertySources().addLast(new ResourcePropertySource(new ClassPathResource("step3.properties"))); env.getPropertySources().addFirst(new SimpleCommandLinePropertySource(args)); for (PropertySource<?> ps : env.getPropertySources()) { System.out.println(ps); } // System.out.println(env.getProperty("JAVA_HOME")); System.out.println(env.getProperty("server.port")); }
4、ConfigurationPropertySources 處理(*)
發布 application environment 已準備事件
public static void main(String[] args) throws IOException, NoSuchFieldException { ApplicationEnvironment env = new ApplicationEnvironment(); env.getPropertySources().addLast( new ResourcePropertySource("step4", new ClassPathResource("step4.properties")) ); ConfigurationPropertySources.attach(env); for (PropertySource<?> ps : env.getPropertySources()) { System.out.println(ps); } System.out.println(env.getProperty("user.first-name")); System.out.println(env.getProperty("user.middle-name")); System.out.println(env.getProperty("user.last-name")); } }
5、通過 EnvironmentPostProcessorApplicationListener 進行 env 后處理(*)
application.properties,由 StandardConfigDataLocationResolver 解析
spring.application.json
public class Step5 { public static void main(String[] args) { SpringApplication app = new SpringApplication(); app.addListeners(new EnvironmentPostProcessorApplicationListener()); /*List<String> names = SpringFactoriesLoader.loadFactoryNames(EnvironmentPostProcessor.class, Step5.class.getClassLoader()); for (String name : names) { System.out.println(name); }*/ EventPublishingRunListener publisher = new EventPublishingRunListener(app, args); ApplicationEnvironment env = new ApplicationEnvironment(); System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>> 增強前"); for (PropertySource<?> ps : env.getPropertySources()) { System.out.println(ps); } publisher.environmentPrepared(new DefaultBootstrapContext(), env); System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>> 增強后"); for (PropertySource<?> ps : env.getPropertySources()) { System.out.println(ps); } } private static void test1() { SpringApplication app = new SpringApplication(); ApplicationEnvironment env = new ApplicationEnvironment(); System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>> 增強前"); for (PropertySource<?> ps : env.getPropertySources()) { System.out.println(ps); } ConfigDataEnvironmentPostProcessor postProcessor1 = new ConfigDataEnvironmentPostProcessor(new DeferredLogs(), new DefaultBootstrapContext()); postProcessor1.postProcessEnvironment(env, app); System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>> 增強后"); for (PropertySource<?> ps : env.getPropertySources()) { System.out.println(ps); } RandomValuePropertySourceEnvironmentPostProcessor postProcessor2 = new RandomValuePropertySourceEnvironmentPostProcessor(new DeferredLog()); postProcessor2.postProcessEnvironment(env, app); System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>> 增強后"); for (PropertySource<?> ps : env.getPropertySources()) { System.out.println(ps); } System.out.println(env.getProperty("server.port")); System.out.println(env.getProperty("random.int")); System.out.println(env.getProperty("random.int")); System.out.println(env.getProperty("random.int")); System.out.println(env.getProperty("random.uuid")); System.out.println(env.getProperty("random.uuid")); System.out.println(env.getProperty("random.uuid")); } }
6、綁定 spring.main 到 SpringApplication 對象(*)
把配置文件中的值賦給SpringApplication的默認屬性值
public class Step6 { // 綁定 spring.main 前綴的 key value 至 SpringApplication, 請通過 debug 查看 public static void main(String[] args) throws IOException { SpringApplication application = new SpringApplication(); ApplicationEnvironment env = new ApplicationEnvironment(); env.getPropertySources().addLast(new ResourcePropertySource("step6", new ClassPathResource("step6.properties"))); System.out.println(application); Binder.get(env).bind("spring.main", Bindable.ofInstance(application)); System.out.println(application); }
7、打印 banner(*)
public class Step7 { public static void main(String[] args) { ApplicationEnvironment env = new ApplicationEnvironment(); SpringApplicationBannerPrinter printer = new SpringApplicationBannerPrinter( new DefaultResourceLoader(), new SpringBootBanner() ); // 測試文字 banner // env.getPropertySources().addLast(new MapPropertySource("custom", Map.of("spring.banner.location","banner1.txt"))); // 測試圖片 banner // env.getPropertySources().addLast(new MapPropertySource("custom", Map.of("spring.banner.image.location","banner2.png"))); // 版本號的獲取 System.out.println(SpringBootVersion.getVersion()); printer.print(env, Step7.class, System.out); } }
8、創建容器
private static GenericApplicationContext createApplicationContext(WebApplicationType type) { GenericApplicationContext context = null; switch (type) { case SERVLET -> context = new AnnotationConfigServletWebServerApplicationContext(); case REACTIVE -> context = new AnnotationConfigReactiveWebServerApplicationContext(); case NONE -> context = new AnnotationConfigApplicationContext(); } return context; }
9、準備容器發布
application context 已初始化事件
10、加載 bean 定義
發布 application prepared 事件
DefaultListableBeanFactory beanFactory = context.getDefaultListableBeanFactory(); AnnotatedBeanDefinitionReader reader1 = new AnnotatedBeanDefinitionReader(beanFactory); XmlBeanDefinitionReader reader2 = new XmlBeanDefinitionReader(beanFactory); ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(beanFactory); reader1.register(Config.class); reader2.loadBeanDefinitions(new ClassPathResource("b03.xml")); scanner.scan("com.itheima.a39.sub");
11、refresh 容器
發布 application started 事件
12、執行 runner
發布 application ready 事件
這其中有異常,發布 application failed 事件
關于“Springboot底層啟動過程是怎樣的”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“Springboot底層啟動過程是怎樣的”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。