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);
相关推荐
米小虾17 分钟前
从 Chatbot 到 Agent:AI 智能体架构设计的 5 个关键模式
后端
星浩AI31 分钟前
清华团队开源!我给孩子制作了 AI 互动课堂,手把手教你给孩子做一个
人工智能·后端·github
编程小风筝37 分钟前
如何用redission实现springboot的分布式锁?
spring boot·分布式·后端
菜鸟小九1 小时前
hot100(71-80)
java·数据结构·算法
大傻^1 小时前
LangChain4j 1.4.0 快速入门:JDK 11+ 基线迁移与首个 AI Service 构建
java·开发语言·人工智能
代码探秘者1 小时前
【大模型应用】4.分块之六大策略
java·数据结构·后端·python·spring
码喽7号1 小时前
Springboot学习六:MybatisPlus的多表查询以及分页查询
java·spring boot·学习
那我掉的头发算什么1 小时前
【博客系统】基于Spring全家桶的博客系统(下)
java·后端·spring·mybatis·开发
不吃香菜学java1 小时前
苍穹外卖-新增菜品需求分析
java·spring boot·spring·tomcat·maven·ssm
Oneslide1 小时前
Pod启动失败: /var/lib/kubelet/xxx/kube-api-access/ :no space left on device
后端