1. 概述

在实际开发工作中,有时需要在项目启动的时候初始化资源,例如:缓存、定时任务等等。

Spring Boot 提供了这样的方案,只要创建 Bean 实现CommandLineRunner或者ApplicationRunner即可。这两个的作用是相同的,只是提供的参数略有不同。

如果存在多个类实现CommandLineRunner或者ApplicationRunner,可以添加 @Order 注解或者实现 Ordered 接口来控制执行顺序。

2. Demo

先创建一个空的 Spring Boot 项目,创建过程就不演示了。

2.1 CommandLineRunner

MyCommandLineRunner实现类,输出args参数

@Component
public class MyCommandLineRunner implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {String argsStr = String.join(",", args);System.out.println("===================================================================");System.out.println();System.out.println("CommandLineRunner:" + argsStr);}
}

应用程序启动类:

@SpringBootApplication
public class ApplicationRunnerApplication {public static void main(String[] args) {SpringApplication.run(ApplicationRunnerApplication.class, args);System.out.println("SpringApplication.run 执行完毕");}}

配置Idea的启动参数


在这里输入“a1 a2 a3”,然后OK保存


最后启动运行程序,输出结果如下:

2.2 ApplicationRunner

创建MyApplicationRunner 类,代码:

@Component
public class MyApplicationRunner implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) throws Exception {String argsStr = String.join(",", args.getSourceArgs());System.out.println("ApplicationRunner:" + argsStr);}
}

直接运行

SpringBoot使用CommandLineRunner和ApplicationRunner项目初始化事件相关推荐

  1. SpringBoot使用CommandLineRunner和ApplicationRunner执行初始化业务

    业务场景 在业务场景中, 有些情况下需要我们一启动项目就执行一些操作. 例如数据配置的相关初始化, 通用缓存的数据构造等. SpringBoot为我们提供了CommandLineRunner和Appl ...

  2. Swoole 自定义项目初始化事件处理的实现

    为什么80%的码农都做不了架构师?>>>    最近使用基于 Swoole 开发的 imi 框架 开发项目,碰到一个需求,就是想要做项目初始化处理.当初始化处理完成前,不想让 Swo ...

  3. CommandLineRunner与ApplicationRunner接口的使用及源码解析

    引言 我们在使用SpringBoot搭建项目的时候,如果希望在项目启动完成之前,能够初始化一些操作,针对这种需求,可以考虑实现如下两个接口(任一个都可以) org.springframework.bo ...

  4. CommandLineRunner、ApplicationRunner 接口

    如果我们想在项目启动后做一些事情(如加载定时任务,初始化工作),可以使用spring提供的CommandLineRunner.ApplicationRunner 接口,在容器启动成功后的最后一步回调( ...

  5. CommandLineRunner和ApplicationRunner

    本文主要来自:https://blog.csdn.net/zongzhankui/article/details/78681942 前言: Spring Boot如何解决项目启动时初始化资源,在我们实 ...

  6. 使用SpringBoot的CommandLineRunner遇到的坑

    来源:blog.csdn.net/zwq_zwq_zwq/article/details/81059017 使用场景 再应用程序开发过程中,往往我们需要在容器启动的时候执行一些操作.Spring Bo ...

  7. 异常将上下文初始化事件发送到类的侦听器实例_Spring的Bean实例化原理,这一次彻底搞懂了!...

    前言 之前分析了Spring XML和注解的解析原理,并将其封装为BeanDefinition对象存放到IOC容器中,而这些只是refresh方法中的其中一个步骤--obtainFreshBeanFa ...

  8. SpringBoot使用Slf4j+Log4j2完成项目的日志记录

    SpringBoot使用Slf4j+Log4j完成项目的日志记录 前言 本示例采用SpringBoot项目使用SpringAOP记录日志,Slf4j作为日志门面,Log4j2作为日志实现实,实现开发中 ...

  9. CommandLineRunner 和 ApplicationRunner 的区别

    CommandLineRunner 和 ApplicationRunner 概述 CommandLineRunner 和 ApplicationRunner 的作用类似, 都可以在 Spring 容器 ...

最新文章

  1. kafka+zookeeper搭建步骤kafka问题
  2. 51单片机怎么学啊?有推荐的线上网课和书籍么?
  3. PageRank算法并行实现
  4. NFS Volume Provider(Part II) - 每天5分钟玩转 OpenStack(63)
  5. [网络安全自学篇] 二十四.基于机器学习的入侵检测和攻击识别——以KDD CUP99数据集为例
  6. 记录一次nginx配置vhost的小bug
  7. Linux下如何安装软件
  8. Flask 蓝图机制及应用
  9. Linux下使用shell脚本远程登录主机(Ubuntu CentOS)
  10. 挂载报错:“/dev/vda1 is apparently in use by the system;”
  11. maven pom聚合与继承
  12. 金南瓜 secs/gem协议 符合SEMI secs/gem协议 国际通讯协
  13. feed机制、fetch机制
  14. mysql error 1114_ERROR 1114 (HY000): The table is full
  15. 【论文泛读】Leveraging Distribution Alignment via Stein Path for Cross-Domain Cold-Start Recommendation
  16. 支持扫描的单usb口打印服务器,LP-1000 TT168L1 打印机共享器 USB网络 远程 扫描 云打印服务器...
  17. 自用力扣笔记备忘录——数组tag简单题
  18. 2016年7月17日学习 scratch 小兔子偷胡萝卜的故事
  19. 6.824系统-分布式计算MapReduce模式
  20. Android三级图片缓存框架思路【附练习Demo】

热门文章

  1. ALV 动态显示列Demo
  2. 80%的人都混淆的BI和报表之不同
  3. 49、Power Query-合并查询温故知新
  4. SAP收货时自动创建采购订单
  5. 上市后首份年报亮眼,快手天花板在哪?
  6. 寺库等奢侈品电商补贴下的奢侈品市场,会是怎样的未来
  7. linux中jpg文件,Linux下压缩和优化jpg与png图片的方法
  8. php 循环链表,PHP实现循环链表功能
  9. js根据月数计算有效日期_你真的看懂产品生产日期了么
  10. php谈谈你对分布式的理解,简谈关于对分布式处理的理解