003-Spring boot 启动流程分析

目录

    • 启动流程分析
      • [创建 SpringApplication](#创建 SpringApplication)
      • [启动 run(String... args)](#启动 run(String... args))

启动流程分析

java 复制代码
SpringApplication.run(App.class, args);

return new SpringApplication(primarySources).run(args);

创建 SpringApplication

java 复制代码
SpringApplication(primarySources):

this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
//这个推断流程 001 篇 分析webServer里有详情 web应用返回WebApplicationType.SERVLET
this.webApplicationType = WebApplicationType.deduceFromClasspath();
//获取Factories中配置的 BootstrapRegistryInitializer
this.bootstrapRegistryInitializers = new ArrayList<>(
				getSpringFactoriesInstances(BootstrapRegistryInitializer.class));
this.initializers = getSpringFactoriesInstances(ApplicationContextInitializer.class);
this.listeners = getSpringFactoriesInstances(ApplicationListener.class));
//推断main方法所在的Class
this.mainApplicationClass = deduceMainApplicationClass();

启动 run(String... args)

java 复制代码
//获取Factories中配置的 SpringApplicationRunListener 并执行其 starting()
SpringApplicationRunListeners listeners = getSpringFactoriesInstances(SpringApplicationRunListener.class, types, this, args).forEach(listener.starting(bootstrapContext))
//封装 args:这里会读取执行jar的时候添加的以 '--' 开头的 kay=value
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
//读取配置 包括:系统,JVM,ServletContext,properties,yaml配置
//全部设置到 environment 中
ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
//这一步我查资料应该是忽略Java解析BeanInfo的缓存 因为Spring使用的是ASM技术
configureIgnoreBeanInfo(environment);
//打印banner并返回
Banner printedBanner = printBanner(environment);
//根据类型返回上下文 001 有详细分析 AnnotationConfigServletWebServerApplicationContext
context = createApplicationContext();
context.applicationStartup = applicationStartup;
//利用ApplicationContextInitializer初始化Spring 上下文
//发布ApplicationContextInitializedEvent
//注册primarySources = run() 传入的配置类列表
//发布ApplicationContextPreparedEvent
prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
//调用refresh(context) 这一步就是执行Spring bean创建的逻辑了
refreshContext(context);
//这一步是空的 给子类一个实现的机会
afterRefresh(context, applicationArguments);
//遍历执行之前 listeners started()
listeners.started(context, timeTakenToStartup);
//执行 ApplicationRunner.run(args);
//执行 CommandLineRunner.run(args.getSourceArgs());
//两个执行器只有 入参的区别 一个是解析之后的对象,一个是字符串
callRunners(context, applicationArguments);
//遍历执行之前 listeners ready()
listeners.ready(context, timeTakenToReady);
相关推荐
朝新_16 小时前
【LangChain】少样本提示(few-shorting) 掌握 Few-Shot 提示,让大模型按你的规则输出
java·人工智能·langchain
Le_ee16 小时前
ctfweb:flask+ssti
后端·python·flask
木易 士心16 小时前
一文彻底搞懂 Elasticsearch:原理、场景、避坑与优化
大数据·后端·elasticsearch·搜索引擎
七七powerful16 小时前
mac电脑安装cmca根证书
java·前端·macos
曹牧16 小时前
Java:数据载体
java·开发语言
东北甜妹16 小时前
K8s Ingress
java·运维·前端
杨凯凡16 小时前
【033】Maven 生命周期与坐标:多模块结构
java·maven
lihongli00016 小时前
关于c++中锁的种类与使用
java·开发语言·c++
凤凰院凶涛QAQ16 小时前
《C++转Java快速入手系列》继承与多态篇
java·c++
倒霉蛋小马16 小时前
Idea--如何同一个SpringBoot项目复制多次,模拟集群环境
java·ide·intellij-idea