SpringBoot 源码解析笔记

欢迎各位关注我的笔记仓库,clone 仓库到本地后使用 Typora 阅读效果更好。

如果大家只关注 SpringBoot 如何自动装配,可以只看“注解分析”和“装配流程”两个小节


启动流程

应用启动:

@SpringBootApplication
public class BootApplication {
    public static void main(String[] args) {
        // 启动代码
        SpringApplication.run(BootApplication.class, args);
    }
}

SpringApplication 构造方法:

  • this.resourceLoader = resourceLoader:资源加载器,初始为 null

  • this.webApplicationType = WebApplicationType.deduceFromClasspath():判断当前应用的类型,是响应式还是 Web 类

  • this.bootstrapRegistryInitializers = getBootstrapRegistryInitializersFromSpringFactories()获取引导器

    • 去 META-INF/spring.factories 文件中找 org.springframework.boot.Bootstrapper
    • 寻找的顺序:classpath → spring-beans → boot-devtools → springboot → boot-autoconfigure
  • setInitializers(getSpringFactoriesInstances(ApplicationContextInitializer.class))获取初始化器

    • 去 META-INF/spring.factories 文件中找 org.springframework.context.ApplicationContextInitializer
  • setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class))获取监听器

    • 去 META-INF/spring.factories 文件中找 org.springframework.context.ApplicationListener
  • this.mainApplicationClass = deduceMainApplicationClass():获取出 main 程序类

SpringApplication#run(String... args):

  • StopWatch stopWatch = new StopWatch():停止监听器,监控整个应用的启停

  • stopWatch.start():记录应用的启动时间

  • bootstrapContext = createBootstrapContext()创建引导上下文环境

    • bootstrapContext = new DefaultBootstrapContext():创建默认的引导类环境
    • this.bootstrapRegistryInitializers.forEach():遍历所有的引导器调用 initialize 方法完成初始化设置
  • configureHeadlessProperty():让当前应用进入 headless 模式

  • listeners = getRunListeners(args):获取所有 RunListener(运行监听器)

    • 去 META-INF/spring.factories 文件中找 org.springframework.boot.SpringApplicationRunListener
  • listeners.starting(bootstrapContext, this.mainApplicationClass)遍历所有的运行监听器调用 starting 方法

  • applicationArguments = new DefaultApplicationArguments(args):获取所有的命令行参数

  • environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments)准备环境

    • environment = getOrCreateEnvironment():返回或创建基础环境信息对象

      • switch (this.webApplicationType):根据当前应用的类型创建环境
        • case SERVLET:Web 应用环境对应 ApplicationServletEnvironment
        • case REACTIVE:响应式编程对应 ApplicationReactiveWebEnvironment
        • default:默认为 Spring 环境 ApplicationEnvironment
    • configureEnvironment(environment, applicationArguments.getSourceArgs()):读取所有配置源的属性值配置环境

    • ConfigurationPropertySources.attach(environment):属性值绑定环境信息

      • sources.addFirst(ATTACHED_PROPERTY_SOURCE_NAME,..):把 configurationProperties 放入环境的属性信息头部
    • listeners.environmentPrepared(bootstrapContext, environment)运行监听器调用 environmentPrepared(),EventPublishingRunListener 发布事件通知所有的监听器当前环境准备完成

    • DefaultPropertiesPropertySource.moveToEnd(environment):移动 defaultProperties 属性源到环境中的最后一个源

    • bindToSpringApplication(environment):与容器绑定当前环境

    • ConfigurationPropertySource

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot项目实战笔记可以按照以下步骤进行: 1. 首先,你可以通过观看B站上的教程视频来学习SpringBoot项目实战。在视频中,你可以学习到如何使用SpringBoot、MyBatis和MySQL创建一个电脑商城项目。 2. 确保你的SpringBoot项目能够成功启动。找到被@SpringBootApplication注解修饰的入口启动类,并运行该类。如果你能够观察到图形化的界面,那么说明你的项目成功启动了。 3. 如果你还没有创建SpringBoot项目,你可以使用Spring Initializr来初始化一个新的项目。Spring Initializr是一个Web应用程序,可以为你生成Spring Boot项目的基本结构。你可以选择使用Maven或Gradle作为构建工具,并添加适合你的项目的依赖。然后,你只需要编写应用程序的代码即可。 希望以上信息对你有帮助!如果还有其他问题,请随时提问。123 #### 引用[.reference_title] - *1* *2* [SpringBoot项目实战笔记:电脑商城项目实战(SpringBoot+MyBatis+MySQL)](https://blog.csdn.net/weixin_44260350/article/details/127746667)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] - *3* [《SpringBoot实战》读书笔记](https://blog.csdn.net/sanhewuyang/article/details/104494202)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值