剖析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 ...
随机推荐
- numpy读取本地数据和索引
1.numpy读取数据 np.loadtxt(fname,dtype=np.float,delimiter=None,skiprows=0,usecols=None,unpack=False) 做一个 ...
- 树莓派-openeuler安装
一.安装准备 1.硬件安装 2.下载openeuler镜像 3.sd卡格式化 sd格式化工具 4.镜像校验 二.镜像烧写 选择树莓派官方烧写工具,耐心等待... 三.网络配置 1.寻找树莓派的ip地址 ...
- max-points-on-a-line leetcode C++
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- 小白都能看懂的Spring源码揭秘之IOC容器源码分析
目录 前言 IOC 只是一个 Map 集合 IOC 三大核心接口 IOC 初始化三大步骤 定位 加载 注册 总结 前言 在 Spring 框架中,大家耳熟能详的无非就是 IOC,DI,Spring M ...
- MySQL到底能否解决幻读问题
先说结论,MySQL 存储引擎 InnoDB 在可重复读(RR)隔离级别下是解决了幻读问题的. 方法:是通过next-key lock在当前读事务开启时,1.给涉及到的行加写锁(行锁)防止写操作:2. ...
- 【java + selenium3】窗口基本操作及8大定位元素方法总结(一)
一.窗口基本操作 1. 关于窗口的设置都是由window对象提供的: 获取window的对象方法: driver.manage().window(); //1.获取 window 对象 Window ...
- java 三大特性_继承、封装、多态_day005
一.继承: java的三大特性之一.两个类之间通过extends关键字来描述父子关系,子类便可拥有父类的公共方法和公共属性.子类可以继承父类的方法和属性,子类也可以自己定义没有的方法或者通过覆盖父类的 ...
- 【Python+postman接口自动化测试】(6)Chrome开发者工具
Chrome开发者工具 Elements: HTML元素面板,用于定位查看元素源代码 Console: js控制台面板,js命令行,查看前端日志 Sources: 资源面板,用于断点调试js Netw ...
- 自定义 axios
自定义 axios function axios({ url, method = 'GET', params = {}, data = {} }) { // 返回一个 promise 对象 retur ...
- Linux ns 5. IPC Namespace 详解
文章目录 1. 简介 2. 源码分析 2.1 copy_ipcs() 2.2 ipcget() 2.3 ipc_check_perms() 2.4 相关系统调用 参考文档: 1. 简介 进程间通讯的机 ...