• springboot源码理解五、SpringApplication初始化



    springboot版本:2.2.9.RELEASE。

    main

    在这里插入图片描述
    SpringApplication#run(java.lang.Class, java.lang.String…)
    在这里插入图片描述

    SpringApplication#run(java.lang.Class[], java.lang.String[])
    在这里插入图片描述
    我们注意这里的new SpringApplication(primarySources)

    new SpringApplication(primarySources)

    初始化SpringApplication

    SpringApplication#SpringApplication(java.lang.Class…)
    在这里插入图片描述

    SpringApplication#SpringApplication(org.springframework.core.io.ResourceLoader, java.lang.Class…)
    在这里插入图片描述

    public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
    	// 资源加载器设置为null
    	this.resourceLoader = resourceLoader;
    	
    	// 加载类资源不能为null
    	Assert.notNull(primarySources, "PrimarySources must not be null");
    	
    	// 将数组primarySources转换为List,放入LinkedHashSet里。
    	this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
    	
    	// 推断应用类型,当前项目为WebApplicationType.SERVLET。
    	this.webApplicationType = WebApplicationType.deduceFromClasspath();
    	
    	// 加载classpath下(项目中),META-INF\spring.factories中配置的ApplicationContextInitializer。
    	setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
    	
    	// 加载META-INF\spring.factories中配置的ApplicationListener。
    	setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
    	
    	// 根据调用栈推断main方法主类
    	this.mainApplicationClass = deduceMainApplicationClass();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    setInitializers

    加载classpath下(项目中),META-INF\spring.factories中配置的ApplicationContextInitializer。
    初始化器类

    SpringApplication#getSpringFactoriesInstances(java.lang.Class)
    在这里插入图片描述
    SpringApplication#getSpringFactoriesInstances(java.lang.Class, java.lang.Class[], java.lang.Object…)

    SpringFactoriesLoader.loadFactoryNames这个方法我们在:自动配置(第三方依赖中的bean),中讲到过,
    作用就是加载META-INF\spring.factories的信息。
    在这里插入图片描述
    在这里插入图片描述

    setListeners

    加载META-INF\spring.factories中配置的ApplicationListener。
    监听器
    在这里插入图片描述

  • 相关阅读:
    (五)编译中出现的向后兼容问题
    Vue中的MVVM模型
    Rust的注释与文档
    arm 架构下内核打印的 oops 信息中的 Code 字段解析
    跟着实例学Go语言(四)
    何时使用 Apache Kafka 的请求-响应
    MySQL学习笔记(十二)锁
    Git LFS提交大文件到GitHub
    算法公司TikTok的黑科技: Seeing Like an algorithm
    SQL模板-用户留存率计算
  • 原文地址:https://blog.csdn.net/qq_35549286/article/details/126930845