业务场景

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

接口说明

CommandLineRunner和ApplicationRunner两个接口除了参数不同, 其他基本相同, 可以根据实际需求选择使用. CommandLineRunner中的run方法参数为String..., ApplicationRunner中的run方法参数为ApplicationArguments.在同等顺序中, ApplicationRunner会比CommandLineRunner优先执行

使用方法

定义一个类实现该接口, 重写其中的run方法即可. 如果有多个实现类, 我们可以通过@Order注解来定义优先级(数字越低越先执行)

@Order(1)
@Component
public class MyCommandLineRunner1 implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("========== 初始任务MyCommandLineRunner1 ==========");}
}@Order(2)
@Component
public class MyCommandLineRunner2 implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("========== 初始任务MyCommandLineRunner2 ==========");
//        throw new RuntimeException("模拟异常");}
}@Order(2)
@Component
public class MyApplicationRunner1 implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) throws Exception {System.out.println("========== 初始任务MyApplicationRunner1 ==========");}
}

启动项目, 输出如下:

注意事项:

1. CommandLineRunner和ApplicationRunner的执行其实是整个项目启动周期中的一部分, Runner执行完成后, 才最终启动项目.

2. 如果Runner中出现异常, 就会影响项目的启动, 所以要在Runner中处理异常

3. 如果Runner中需要指定定时周期任务(如一直循环打印某些信息等), 需要在异步线程中执行, 否则项目的主线程会一直阻塞 , 无法启动成功

SpringBoot使用CommandLineRunner和ApplicationRunner执行初始化业务相关推荐

  1. SpringBoot使用CommandLineRunner和ApplicationRunner项目初始化事件

    1. 概述 在实际开发工作中,有时需要在项目启动的时候初始化资源,例如:缓存.定时任务等等. Spring Boot 提供了这样的方案,只要创建 Bean 实现CommandLineRunner或者A ...

  2. 如何在SpringBoot启动时执行初始化操作,两个简单接口就可以实现

    (一)概述 最近遇到一个功能点,数据库中一张很简单的表有一千多条数据,这里的数据主要做到了值域映射的作用,简单来讲就是我可以通过中文名拿到数据库中对应的code值.原本的实现方式是每次用到之后去查一次 ...

  3. java 项目启动初始化_Spring项目启动时执行初始化方法

    一.applicationContext.xml配置bean init-method="initKeyWord"> classpath:sensitive-word.xml ...

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

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

  5. SpringBoot—项目启动时几种初始化操作及SpringApplication类详解

    关注微信公众号:CodingTechWork,一起学习进步. 引言   在使用Spring Boot搭建项目时,启动项目工程,经常遇到一些需要启动初始化数据或者资源的需求,比如提前加载某个配置文件内容 ...

  6. SpringBoot启动时实现自动执行代码的几种方式讲解

    点击关注公众号,实用技术文章及时了解 来源:blog.csdn.net/u011291072/article/ details/81813662 前言 目前开发的SpringBoot项目在启动的时候需 ...

  7. 使用SpringBoot的CommandLineRunner遇到的坑

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

  8. CommandLineRunner、ApplicationRunner 接口

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

  9. CommandLineRunner 和 ApplicationRunner 的区别

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

最新文章

  1. CocoaPods原理(一)
  2. Socket编程中的强制关闭与优雅关闭及相关socket选项
  3. Spring Cloud Alibaba - 04 Nacos 领域模型划分
  4. Scala变量的声明和赋值
  5. excel进销存管理系统_【实例分享】勤哲Excel服务器做企业进销存财务管理系统...
  6. 在 Apache Spark 中利用 HyperLogLog 函数实现高级分析
  7. php swoole 心跳,聊聊swoole的心跳
  8. 是未来的风口还是无声的战争,中国的saas平台究竟能不能做起来?
  9. 信息学奥赛一本通 1358:中缀表达式值(expr)
  10. springboot2.1.1连接数据库失败的原因查找
  11. 《软件需求最佳实践》阅读笔记02
  12. c# winform中datagridview空间添加序号和表头“序号”
  13. 在mybatis里面设置不同数据库运行环境和适应性问题
  14. arcgis怎么压缩tif文件_PDF文件怎么压缩?这个方法一看就会!
  15. cad批量选择相同块_在CAD中如何快速选择相同或类似的图形、图块?
  16. 合肥工业大学暑期“三下乡”——探访悠悠古村 发扬传统文化
  17. Ubuntu16.04常用工具
  18. access是用来干什么的_access是干什么用的
  19. 赠书:支付平台架构业务、规划、设计与实现
  20. 展望计算机未来发展趋势,计算机的未来展望

热门文章

  1. adb查看安卓设备系统Android版本
  2. TortoiseGit 分支管理策略
  3. Codeforces 776D The Door Problem
  4. 原创:Spring整合junit测试框架(简易教程 基于myeclipse,不需要麻烦的导包)
  5. 十二生肖swift1.2
  6. ie6识别important问题
  7. 机器学习算法(1)——贝叶斯估计与极大似然估计与EM算法之间的联系
  8. php 上传 blob,Laravel框架+Blob实现的多图上传功能示例
  9. “send“ and “transfer“ are only available for objects of type “address payable“, not “address
  10. php中configuration,php configuration