码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • springboot源码理解五、SpringApplication初始化


    SpringApplication初始化

    • main
      • new SpringApplication(primarySources)
        • setInitializers
        • setListeners

    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。
    监听器
    在这里插入图片描述

  • 相关阅读:
    电脑软件:推荐一款电脑多屏幕管理工具DisplayFusion
    re 正则从文本中提取全球电话和邮箱
    公司为什么要使用OKR,目的是什么?
    越早越好,突破职业瓶颈,2023年考PMP项目管理有何好处?
    tp6获取请求参数
    HBase的flush(刷写)及compact(合并)机制
    粒子群算法
    Jupyter的安装
    容器环境下php进程与bash进程树关系
    【Python】使用Shell通配符来匹配字符串
  • 原文地址:https://blog.csdn.net/qq_35549286/article/details/126930845
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号