当前位置:首页 > 知识库 > 正文

springboot源码怎么看(springboot源码深度解析)

  前面给大家介绍了SpringBoot启动的核心流程,本文开始给大家详细的来介绍SpringBoot启动中的具体实现的相关细节。

springboot源码怎么看(springboot源码深度解析)  第1张

SpringBoot2.png"https://www.changchenghao.cn/wp-content/uploads/2022/01/a5696440b4fa472091df1992b1c1591c.png" alt="SpringBoot源码分析之SpringApplication构造方法核心源码分析">

image.png"unchecked", "rawtypes" }) 2 public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) { 3 // 传递的resourceLoader为null 4 this.resourceLoader = resourceLoader; 5 Assert.notNull(primarySources, "PrimarySources must not be null"); 6 // 记录主方法的配置类名称 7 this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources)); 8 // 记录当前项目的类型 9 this.webApplicationType = WebApplicationType.deduceFromClasspath();10 // 加载配置在spring.factories文件中的ApplicationContextInitializer对应的类型并实例化11 // 并将加载的数据存储在了 initializers 成员变量中。12 setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));13 // 初始化监听器 并将加载的监听器实例对象存储在了listeners成员变量中14 setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));15 // 反推main方法所在的Class对象 并记录在了mainApplicationClass对象中16 this.mainApplicationClass = deduceMainApplicationClass();17 }

1.webApplicationType

  首先来看下webApplicationType是如何来推导出当前启动的项目的类型。通过代码可以看到是通过deduceFromClassPath()方法根据ClassPath来推导出来的。

1this.webApplicationType = WebApplicationType.deduceFromClasspath();

  跟踪进去看代码

springboot源码怎么看(springboot源码深度解析)  第2张

在看整体的实现逻辑之前,我们先分别看两个内容,第一就是在上面的代码中使用到了相关的静态变量。

springboot源码怎么看(springboot源码深度解析)  第3张

image.png"https://www.changchenghao.cn/wp-content/uploads/2022/01/f77c005afcd642febda7341ec455a050.png" alt="SpringBoot源码分析之SpringApplication构造方法核心源码分析">

image.png"https://www.changchenghao.cn/wp-content/uploads/2022/01/fb307a27d24a405b9051880c320af2e9.png" alt="SpringBoot源码分析之SpringApplication构造方法核心源码分析">

image.png"https://www.changchenghao.cn/wp-content/uploads/2022/01/511d171fb54040f7b5248e26d895b0c8.png" alt="SpringBoot源码分析之SpringApplication构造方法核心源码分析">

 然后加载的关键方法是getSpringFactoriesInstances()方法。该方法会加载 spring.factories文件中的key为org.springframework.context.ApplicationContextInitializer 的值。

spring-boot项目下

1# Application Context Initializers2org.springframework.context.ApplicationContextInitializer=3org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,4org.springframework.boot.context.ContextIdApplicationContextInitializer,5org.springframework.boot.context.config.DelegatingApplicationContextInitializer,6org.springframework.boot.rsocket.context.RSocketPortInfoApplicationContextInitializer,7org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer

spring-boot-autoconfigure项目下

1# Initializers2org.springframework.context.ApplicationContextInitializer=3org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,4org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListenerspringboot源码怎么看(springboot源码深度解析)  第4张

image.png"https://www.changchenghao.cn/wp-content/uploads/2022/01/3ee3bcebeb044347a2b768d40e88ff6f.png" alt="SpringBoot源码分析之SpringApplication构造方法核心源码分析">

image.png"https://www.changchenghao.cn/wp-content/uploads/2022/01/dba1e0c58f724901a124c185949ffaae.png" alt="SpringBoot源码分析之SpringApplication构造方法核心源码分析">

image.png"https://www.changchenghao.cn/wp-content/uploads/2022/01/bc85fc2a2e644023890690cf77736e75.png" alt="SpringBoot源码分析之SpringApplication构造方法核心源码分析">

image.png"https://www.changchenghao.cn/wp-content/uploads/2022/01/2456070e6c3940399086c7de8c191341.png" alt="SpringBoot源码分析之SpringApplication构造方法核心源码分析">

image.png"https://www.changchenghao.cn/wp-content/uploads/2022/01/a361b392ba90475b9af0b3ccd4597283.png" alt="SpringBoot源码分析之SpringApplication构造方法核心源码分析">

image.png"https://www.changchenghao.cn/wp-content/uploads/2022/01/11a05a39cf7f4e93a51ba9cb9cb0c1d2.png" alt="SpringBoot源码分析之SpringApplication构造方法核心源码分析">

image.png"https://www.changchenghao.cn/wp-content/uploads/2022/01/ca21c70289ba4cbd8caa9a226be69f5f.png" alt="SpringBoot源码分析之SpringApplication构造方法核心源码分析">

image.png"https://www.changchenghao.cn/wp-content/uploads/2022/01/d1f7dd59e25a4e05b159e89d0ed7e6f7.png" alt="SpringBoot源码分析之SpringApplication构造方法核心源码分析">

image.png"https://www.changchenghao.cn/wp-content/uploads/2022/01/7d67bbef947b404697fd833f726e29d9.png" alt="SpringBoot源码分析之SpringApplication构造方法核心源码分析">

image.png"https://www.changchenghao.cn/wp-content/uploads/2022/01/614a438875ac4f67aad34b4e2b61231d.png" alt="SpringBoot源码分析之SpringApplication构造方法核心源码分析">

image.png"https://www.changchenghao.cn/wp-content/uploads/2022/01/44bdeabb231642f7beb5e06ad9d196eb.png" alt="SpringBoot源码分析之SpringApplication构造方法核心源码分析">

image.png"https://www.changchenghao.cn/wp-content/uploads/2022/01/065a14cd226b4691abf84eaa17132647.png" alt="SpringBoot源码分析之SpringApplication构造方法核心源码分析">

image.png"https://www.changchenghao.cn/wp-content/uploads/2022/01/d060651460cb4930bcc1094571bedbe4.png" alt="SpringBoot源码分析之SpringApplication构造方法核心源码分析">

image.png" width="258" height="268 alt=二维码 title=微信二维码 src=https://www.changchenghao.cn/wp-content/uploads/2022/01/1.jpg">

微信号:sansui663(长安复制)

发表评论

最新文章

推荐文章