剖析ApplicationRunner、CommandLineRunner
需求:SpringBoot项目启动成功后执行某方法
方案:在spring-boot中提供了两种Runner接口:ApplicationRunner和CommandLineRunner,编写自己的类实现这两种接口的run方法,均可实现需求
不同的是实现的两个run方法接收的参数不同,这也是他俩唯一的不同,
ApplicationRunner 接收的是 ApplicationArguments 类型的参数,即应用程序启动参数
CommandLineRunner 接收的是String类型的可变参数,它的值就是我们main函数接收到的命令行参数(此参数可通过Run Configurations中的Arguments添加)
Runner源码解析:SpringApplication.run 方法源代码执行成功的最后一步调用了 callRunners(context, applicationArguments),
此方法内部搜集了当前容器中所有的Runner类型的实体,并遍历调用其run方法,实现了后置的方法调用功能,具体源码如下
① SpringBoot主启动类:
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(CcwebApplication.class);
springApplication.run(args);
}
②run方法最后一步
public ConfigurableApplicationContext run(String... args) {
...
listeners.started(context);
// 寻找并调用当前容器中所有Runner
this.callRunners(context, applicationArguments);
...
}
③ 遍历执行所有Runner
private void callRunners(ApplicationContext context, ApplicationArguments args) {
List<Object> runners = new ArrayList();
// 找到上下文中所有实现了ApplicationRunner的类
runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
// 找到上下文中所有实现了CommandLineRunner的类
runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
// 根据@Order注解的Value值进行排序
AnnotationAwareOrderComparator.sort(runners);
Iterator var4 = (new LinkedHashSet(runners)).iterator();
// 依次调用run 方法
while(var4.hasNext()) {
Object runner = var4.next();
if (runner instanceof ApplicationRunner) {
this.callRunner((ApplicationRunner)runner, args);
}
if (runner instanceof CommandLineRunner) {
this.callRunner((CommandLineRunner)runner, args);
}
}
}
剖析ApplicationRunner、CommandLineRunner的更多相关文章
- SpringBoot ApplicationRunner/CommandLineRunner
CommandLineRunner.ApplicationRunner 接口是在容器启动成功后的最后一步回调(类似开机自动启动). CommandLineRunner.ApplicationRunne ...
- 不懂SpringApplication生命周期事件?那就等于不会Spring Boot嘛
学习方法之少废话:吹牛.装逼.叫大哥. 作者:A哥(YourBatman) 公众号:BAT的乌托邦(ID:BAT-utopia) 文末是否有彩蛋:有 目录 前言 正文 生命周期事件流程图 版本说明: ...
- springboot 学习进度
1 hello world --------------ok 主启动程序必须在层次结构的最上面. 2 配置 3.日志 4.Web开发 1)SpringBoot集成JSP的方法 配置applicatio ...
- springboot情操陶冶-SpringApplication(一)
SpringApplication是所有springboot的入口类,分析此类有助于我们了解springboot的工作机制.本文以2.0.3.REALEASE版本作分析 SpringApplicati ...
- springboot启动配置原理之一(创建SpringApplication对象)
几个重要的事件回调机制 配置在META-INF/spring.factories ApplicationContextInitializer SpringApplicationRunListener ...
- 七、Spring Boot 启动配置原理
几个重要的事件回调机制 配置在META-INF/spring.factories ApplicationContextInitializer SpringApplicationRunListener ...
- Spring boot 启动配置原理
配置在META-INF/spring.factories 有几个主要的类 ApplicationContextInitializer 创建SpringAplication SpringAppli ...
- Spring Boot总结
一.Spring Boot 入门 1.Spring Boot 简介 简化Spring应用开发的一个框架: 整个Spring技术栈的一个大整合: J2EE开发的一站式解决方案: 2.微服务 2014,m ...
- springboot超详细笔记
一.Spring Boot 入门 1.Spring Boot 简介 简化Spring应用开发的一个框架: 整个Spring技术栈的一个大整合: J2EE开发的一站式解决方案: 2.微服务 2014,m ...
随机推荐
- Oracle 11g 新建用户
create user XXXuser identified by XXXpassword;--创建用户XXXuser,设置初始密码XXXpassword alter user XXXuser ide ...
- 期望 概率DP
期望 \(x\) 的期望 \(E(x)\) 表示平均情况下 \(x\) 的值. 令 \(C\) 表示常数, \(X\) 和 \(Y\) 表示两个随机变量. \(E(C)=C\) \(E(C \time ...
- Python gpu 显卡小工具 gpu
安装 pip install gpustat 或者 换源 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade gpust ...
- centos 7 安装nfs 实现主机目录共享
多台服务器之间共享目录,实现每个服务器进入目录看到的内容都一样 服务器A 服务器B 1.服务器A和服务器B,安装 nfs-utils和rpcbind #yum install -y nfs-utils ...
- 记一次排查CPU高的问题
背景 将log4j.xml的日志级别从error调整为info后,进行压测发现CPU占用很高达到了90%多(之前也就是50%,60%的样子). 问题排查 排查思路: 看进程中的线程到底执行的是什么, ...
- 详细剖析Spring Cloud 和Spring Cloud Alibaba的前世今生
我们知道spring cloud可以用来开发微服务,但是应该很少有人真正知道Spring Cloud是什么. 官方的解释是:spring cloud提供了一些可以让开发者快速构建分布式应用的工具,这些 ...
- 2021.11.4测试T1-妹子
题目 今天测试,直接挂完了 写了四个小时,最后发现自己题目理解错误了 有两个区间,在输入了 \(l\) 和 \(r\) 以后,进行查询 \[ min(max(a_1,a_2,...a_p,b_{p+1 ...
- DockerFile-构建容器的基石
DockerFile 非常的关键,它不同于 docker commit 的手动命令方式来进行镜像的构建和修改,类似 docker commit 的交互被称为命令式交互.命令式交互是运维一直绕不开的一种 ...
- [gym102538H]Horrible Cycles
将右侧$n$个点逆序排列,并将左侧的第$i$个点插入到右侧的$a_{i}$之前(左侧的点顺序任意) 换言之,一个左侧的点恰与(排列中)其之后所有右侧的点有边 对于一个简单环,仅保留(排列中)前$i$个 ...
- [hdu6973]Bookshop
将询问拆成$x$到$lca$和$lca$($lca$靠近$y$的儿子)到$y$两部分,分别处理(后者以前者的答案为基础) 两者是类似地,不妨仅考虑前者:用树剖将该询问拆成dfs序上若干个 ...