文章目录

  • CommandLineRunner接口介绍
    • 1. CommandLineRunner接口使用
    • 2. ApplicationRunner与CommandLineRunner
    • 3. ApplicationArguments 源码分析

CommandLineRunner接口介绍

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

1. CommandLineRunner接口使用

  • 写一个类,实现CommandLineRunner接口,将该类注入到Spring容器中;
  • 可以通过@Order注解(属性指定数字越小表示优先级越高)或者Ordered接口来控制执行顺序。

例如,我们自定义两个类,实现CommandLineRunner接口,实现run方法,在run方法中添加处理逻辑。

@Order(5)
@Component
public class AppStartReport implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("AppStartReport : 项目成功启动------------------");}
}
@Order(2)
@Component
public class AppStartReport2 implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("AppStartReport2 : 项目成功启动------------------");}
}

项目启动效果如下,AppStartReport2 较AppStartReport 先执行:

2. ApplicationRunner与CommandLineRunner

  ApplicationRunnerCommandLineRunner功能类似,区别在于方法参数不同:

  • CommandLineRunner的方法参数是原始的参数,未做任何处理;
  • ApplicationRunner的参数为ApplicationArguments对象,是对原始参数的进一步封装。ApplicationArguments是对参数(主要针对main方法)做了进一步的处理,可以解析--name=value的参数,我们就可根据name获取value。
@Component
public class AppStartReport3 implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) throws Exception {System.out.println("AppStartReport3 : 项目成功启动,参数为: " + Arrays.asList(args.getSourceArgs()));}
}

由于我们使用的是main方法的args参数,而这个参数未指定过,因此这里获取值为空;

我们接下来进行参数值的手动指定,再查看效果:

@SpringBootApplication
public class App {public static void main(String[] args) {SpringApplication application = new SpringApplication(App.class);ConfigurableApplicationContext context = application.run("aaa", "bbb");context.close();}
}

以上是自行指定的参数值,未使用main方法的系统参数值,效果如下:

那我们如何指定系统参数值呢?可以在Program arguments选项框设置初始值,“–key=value“形式,如“–argname=ccc”,如下图所示:

@SpringBootApplication
public class App {public static void main(String[] args) {SpringApplication application = new SpringApplication(App.class);ConfigurableApplicationContext context = application.run(args);context.close();}
}

启动器传入参数为main方法的参数args,效果如下:

我们发现获取到的是我们设置的整个参数信息,那如何根据key获取具体的value呢,接下来就需要看一下ApplicationArguments的源码了。

3. ApplicationArguments 源码分析

public interface ApplicationArguments {String[] getSourceArgs();Set<String> getOptionNames();boolean containsOption(String name);List<String> getOptionValues(String name);List<String> getNonOptionArgs();}

我们可以看到5个方法。
从前面的例子中我们可以看出,资源参数分为两种,一种是在调用run方法时传入的(”aaa”,”bbb”),一种是配置的系统参数即main方法的args参数(”ccc”)。

  • 首先getSourceArgs方法,可以获取到所有参数,可以是自己传入的参数,也可以是配置的系统参数;

  • getNonOptionArgs方法即可获得我们传入的参数(”aaa”,”bbb”)。

  • 其余3个方法,可获得系统参数。实际用法如下:

@SpringBootApplication
public class App {public static void main(String[] args) {SpringApplication application = new SpringApplication(App.class);ConfigurableApplicationContext context = application.run(args);ApplicationArguments arguments = context.getBean(ApplicationArguments.class);System.out.println("参数个数:" + arguments.getSourceArgs().length);System.out.println("Non OptionArgs:" + arguments.getNonOptionArgs());System.out.println("Option Names:" + arguments.getOptionNames());System.out.println("获取key为argname的value值:" + arguments.getOptionValues("argname"));System.out.println("是否包含key为argname的参数:" + arguments.containsOption("argname"));context.close();}
}

输出效果如图:

CommandLineRunner接口介绍相关推荐

  1. 实现一个对象验证库系列 -- 1) 接口介绍以及总体思路概述 (请大神批评)

    前情回顾: 上一篇 0) 目录以及库结构介绍 简单描述了下库的代码结构 本文将从接口部分阐述总体的思路 1) 接口介绍以及总体思路概述 如下图,我总共定义了10个Interface 这些实际可分为两类 ...

  2. Spring Boot实践--CommandLineRunner接口

    2019独角兽企业重金招聘Python工程师标准>>> 使用场景的提出: 我们在开发过程中会有这样的场景:需要在容器启动的时候执行一些内容,比如:读取配置文件信息,数据库连接,删除临 ...

  3. SpringBoot中实现CommandLineRunner接口在项目启动后立即执行某方法

    场景 在启动SpringBoot项目的启动类之后需要其立即执行某方法. 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取 ...

  4. 计算机 编程 教程 pdf,计算机专业教程-第3章编程接口介绍.pdf

    下载 第3章 编程接口介绍 • DB2 UDB应用程序概述 • 嵌入S Q L编程 • CLI/ODBC应用程序 • JAVA应用程序 • DAO .R D O .A D O应用程序 本章将介绍对DB ...

  5. Python requests介绍之接口介绍

    Python requests介绍 引用官网介绍 Requests 唯一的一个非转基因的 Python HTTP 库,人类可以安全享用. Requests 允许你发送纯天然,植物饲养的 HTTP/1. ...

  6. Java—Iterator接口介绍及使用

    Iterator接口介绍   Iterator称之为迭代器,是去遍历Collection.Map集合中的元素对象. Iterator常用方法 boolean hasNext():判断是否还有下一个遍历 ...

  7. SPI、I2C、UART三种串行总线协议的区别和SPI接口介绍(转)

    SPI.I2C.UART三种串行总线协议的区别 第一个区别当然是名字: SPI(Serial Peripheral Interface:串行外设接口); I2C(INTER IC BUS) UART( ...

  8. SpringBoot系列: CommandLineRunner接口的用处

    ======================================== 使用 CommandLineRunner 对Spring Bean进行额外初始化 ================== ...

  9. (8)Zynq AXI_ACP接口介绍

    1.1 Zynq AXI_ACP接口介绍 1.1.1 本节目录 1)本节目录: 2)本节引言: 3)FPGA简介: 4)Zynq AXI_ACP接口介绍: 5)结束语. 1.1.2 本节引言 &quo ...

最新文章

  1. WPF 与Surface 2.0 SDK 亲密接触 - ScatterView 数据绑定篇
  2. nmap脚本(nse)原理和编写
  3. 新泰成为全国智慧城市时空信息云平台建设试点
  4. 实时获取ccd图像_四元数数控:CCD视觉检测定位系统在玻璃瓶缺陷的检测
  5. 求最大素数的c语言,for语句计算输出10000以内最大素数怎么搞最简单??各位大神们...
  6. Web游戏开发编程:最神奇的“触觉振动”
  7. java map null吗_Java: Map里面的键和值可以为空吗?
  8. 1)C++对象大小计算
  9. python-matplotlib学习(1)
  10. 报错,> 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL
  11. 楼板计算塑形弹性_土木吧丨弹性与弹塑性计算差异性分析
  12. 幼儿园案例经验迁移_幼儿园故事教学的实施策略
  13. 一张纸的厚度为0.08mm,对折多少次能达到或超过珠穆朗玛峰的高度(8848.13米)
  14. Ubuntu 22.04 ‘Jammy Jellyfish‘ 的新功能
  15. RAC的并发操作与分布式锁DLM
  16. Office系列---将Office文件(Word、PPT、Excel)转换为PDF文件,提取Office文件(Word、PPT)中的所有图片
  17. 基于PC与单片机串口通信的温度监控系统程序设计
  18. 信号与系统1-关于卷积的那些事
  19. mysql集群 教程_mysql集群搭建教程-基础篇
  20. 十代思域遥控升降窗 - 远程开窗

热门文章

  1. C语言输出领结婚证纪念日,领证比较有纪念意义的日子
  2. L1-058 6翻了 (15 分)循环的妙用
  3. 帝国cms怎么发布php,帝国cms如何投稿
  4. 计算机录屏幕和声音的软件是什么,怎么样录制电脑的屏幕和声音?可以进行电脑录像的软件|录制电脑屏幕的方法...
  5. 视频转GIF+GIF录制
  6. 下载支付宝秘钥生成工具
  7. Spark删除redis千万级别set集合数据
  8. Photoshop学习(十三):利用图层制作凹陷效果
  9. 第四届蓝桥杯JavaC组国(决)赛真题
  10. 【Py】隐藏warnings