SpringBoot使用CommandLineRunner和ApplicationRunner项目初始化事件
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项目初始化事件相关推荐
- SpringBoot使用CommandLineRunner和ApplicationRunner执行初始化业务
业务场景 在业务场景中, 有些情况下需要我们一启动项目就执行一些操作. 例如数据配置的相关初始化, 通用缓存的数据构造等. SpringBoot为我们提供了CommandLineRunner和Appl ...
- Swoole 自定义项目初始化事件处理的实现
为什么80%的码农都做不了架构师?>>> 最近使用基于 Swoole 开发的 imi 框架 开发项目,碰到一个需求,就是想要做项目初始化处理.当初始化处理完成前,不想让 Swo ...
- CommandLineRunner与ApplicationRunner接口的使用及源码解析
引言 我们在使用SpringBoot搭建项目的时候,如果希望在项目启动完成之前,能够初始化一些操作,针对这种需求,可以考虑实现如下两个接口(任一个都可以) org.springframework.bo ...
- CommandLineRunner、ApplicationRunner 接口
如果我们想在项目启动后做一些事情(如加载定时任务,初始化工作),可以使用spring提供的CommandLineRunner.ApplicationRunner 接口,在容器启动成功后的最后一步回调( ...
- CommandLineRunner和ApplicationRunner
本文主要来自:https://blog.csdn.net/zongzhankui/article/details/78681942 前言: Spring Boot如何解决项目启动时初始化资源,在我们实 ...
- 使用SpringBoot的CommandLineRunner遇到的坑
来源:blog.csdn.net/zwq_zwq_zwq/article/details/81059017 使用场景 再应用程序开发过程中,往往我们需要在容器启动的时候执行一些操作.Spring Bo ...
- 异常将上下文初始化事件发送到类的侦听器实例_Spring的Bean实例化原理,这一次彻底搞懂了!...
前言 之前分析了Spring XML和注解的解析原理,并将其封装为BeanDefinition对象存放到IOC容器中,而这些只是refresh方法中的其中一个步骤--obtainFreshBeanFa ...
- SpringBoot使用Slf4j+Log4j2完成项目的日志记录
SpringBoot使用Slf4j+Log4j完成项目的日志记录 前言 本示例采用SpringBoot项目使用SpringAOP记录日志,Slf4j作为日志门面,Log4j2作为日志实现实,实现开发中 ...
- CommandLineRunner 和 ApplicationRunner 的区别
CommandLineRunner 和 ApplicationRunner 概述 CommandLineRunner 和 ApplicationRunner 的作用类似, 都可以在 Spring 容器 ...
最新文章
- kafka+zookeeper搭建步骤kafka问题
- 51单片机怎么学啊?有推荐的线上网课和书籍么?
- PageRank算法并行实现
- NFS Volume Provider(Part II) - 每天5分钟玩转 OpenStack(63)
- [网络安全自学篇] 二十四.基于机器学习的入侵检测和攻击识别——以KDD CUP99数据集为例
- 记录一次nginx配置vhost的小bug
- Linux下如何安装软件
- Flask 蓝图机制及应用
- Linux下使用shell脚本远程登录主机(Ubuntu CentOS)
- 挂载报错:“/dev/vda1 is apparently in use by the system;”
- maven pom聚合与继承
- 金南瓜 secs/gem协议 符合SEMI secs/gem协议 国际通讯协
- feed机制、fetch机制
- mysql error 1114_ERROR 1114 (HY000): The table is full
- 【论文泛读】Leveraging Distribution Alignment via Stein Path for Cross-Domain Cold-Start Recommendation
- 支持扫描的单usb口打印服务器,LP-1000 TT168L1 打印机共享器 USB网络 远程 扫描 云打印服务器...
- 自用力扣笔记备忘录——数组tag简单题
- 2016年7月17日学习 scratch 小兔子偷胡萝卜的故事
- 6.824系统-分布式计算MapReduce模式
- Android三级图片缓存框架思路【附练习Demo】