需求: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的更多相关文章

  1. SpringBoot ApplicationRunner/CommandLineRunner

    CommandLineRunner.ApplicationRunner 接口是在容器启动成功后的最后一步回调(类似开机自动启动). CommandLineRunner.ApplicationRunne ...

  2. 不懂SpringApplication生命周期事件?那就等于不会Spring Boot嘛

    学习方法之少废话:吹牛.装逼.叫大哥. 作者:A哥(YourBatman) 公众号:BAT的乌托邦(ID:BAT-utopia) 文末是否有彩蛋:有 目录 前言 正文 生命周期事件流程图 版本说明: ...

  3. springboot 学习进度

    1 hello world --------------ok 主启动程序必须在层次结构的最上面. 2 配置 3.日志 4.Web开发 1)SpringBoot集成JSP的方法 配置applicatio ...

  4. springboot情操陶冶-SpringApplication(一)

    SpringApplication是所有springboot的入口类,分析此类有助于我们了解springboot的工作机制.本文以2.0.3.REALEASE版本作分析 SpringApplicati ...

  5. springboot启动配置原理之一(创建SpringApplication对象)

    几个重要的事件回调机制 配置在META-INF/spring.factories ApplicationContextInitializer SpringApplicationRunListener ...

  6. 七、Spring Boot 启动配置原理

    几个重要的事件回调机制 配置在META-INF/spring.factories ApplicationContextInitializer SpringApplicationRunListener ...

  7. Spring boot 启动配置原理

    配置在META-INF/spring.factories 有几个主要的类 ApplicationContextInitializer    创建SpringAplication SpringAppli ...

  8. Spring Boot总结

    一.Spring Boot 入门 1.Spring Boot 简介 简化Spring应用开发的一个框架: 整个Spring技术栈的一个大整合: J2EE开发的一站式解决方案: 2.微服务 2014,m ...

  9. springboot超详细笔记

    一.Spring Boot 入门 1.Spring Boot 简介 简化Spring应用开发的一个框架: 整个Spring技术栈的一个大整合: J2EE开发的一站式解决方案: 2.微服务 2014,m ...

随机推荐

  1. sql server 如何跟更新拼接的数据(cast用法)

    我们在实际中会做如下图的连接 执行以后这个连接就会报错了,如下图所示   然后我们用cast将数字转换为字符串在连接,如下图所示     这次连接的结果就没问题了,如下图所示     最后如果两个数字 ...

  2. TypeError: Error when calling the metaclass bases Cannot create a consistent method resolution

    Python Error when calling the metaclass bases Cannot create a consistent method resolution order (MR ...

  3. palindrome-partitioning leetcode C++

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  4. ELK 脚本自动化删除索引

    kibana有自带接口,可通过自带的API接口 通过传参来达到删除索引的目的. # 删除15天前的索引 curl -XDELETE "http://10.228.81.161:9201/pa ...

  5. GO 字符串反转

    字符串反转 即 abc 反转后成 cba 思路:两边都设置一个游标,然后互换位置,游标同步向中间移动,再互换. for i, j := 0, len(s)-1; i < j; i, j = i+ ...

  6. 简单理解函数声明(以signal函数为例)

    这两天遇到一些声明比较复杂的函数,比如signal函数,那我们先简单说说signal函数的用法:(参考<c陷阱与缺陷>) [signal:几乎所有c语言程序的实现过程中都要用到signal ...

  7. FZU_DS_2019_SequenceList

    单选题 2-1   数组A[1..5,1..6]每个元素占5个单元,将其按行优先次序存储在起始地址为1000的连续的内存单元中,则元素A[5,5]的地址为:  A.1120      B.1125   ...

  8. 如何用webgl(three.js)搭建一个3D库房,3D密集架,3D档案室(升级版)

    很长一段时间没有写3D库房,3D密集架相关的效果文章了,刚好最近有相关项目落地,索性总结一下 与之前我写的3D库房密集架文章<如何用webgl(three.js)搭建一个3D库房,3D密集架,3 ...

  9. Redis篇:事务和lua脚本的使用

    现在多数秒杀,抽奖,抢红包等大并发高流量的功能一般都是基于 redis 实现,然而在选择 redis 的时候,我们也要了解 redis 如何保证服务正确运行的原理 前言 redis 如何实现高性能和高 ...

  10. [Codeforces Global Round 14]

    打挺差的. 不过\(C,D\)一眼秒了,大概是对这几个月努力的一个结果? \(B\)玄学错误挂了两发. 脑子痛然后打到一半就去睡觉了. -------------------------------- ...