CommandLineRunner和ApplicationRunner
使用场景
我们在开发过程中会有这样的场景:需要在容器启动的时候执行一些内容,比如:读取配置文件信息,数据库连接,删除临时文件,清除缓存信息,在Spring框架下是通过ApplicationListener监听器来实现的。在Spring Boot中给我们提供了两个接口来帮助我们实现这样的需求。这两个接口就是我们今天要讲的CommandLineRunner和ApplicationRunner,他们的执行时机为容器启动完成的时候。
不同点
共同点:其一执行时机都是在容器启动完成的时候进行执行;其二这两个接口中都有一个run()方法;
不同点:ApplicationRunner中run方法的参数为ApplicationArguments,而CommandLineRunner接口中run方法的参数为String数组。
官方说明:
- Interface used to indicate that a bean should run when it is contained within
- a SpringApplication. Multiple CommandLineRunner beans can be defined
- within the same application context and can be ordered using the Ordered
- interface or @Order annotation.
- If you need access to ApplicationArguments instead of the raw String array consider using ApplicationRunner.
使用Ordered接口或者Order注解修改执行顺序
问题提出: 如果有多个实现类,而我们需要按照一定的顺序执行的话,那么应该怎么办呢?
解决方案:其一可以在实现类上加上@Order注解指定执行的顺序;其二可以在实现类上实现Ordered接口来标识。
需要注意:数字越小,优先级越高,也就是@Order(1)注解的类会在@Order(2)注解的类之前执行。
CommandLineRunner和ApplicationRunner的更多相关文章
- Spring Boot 2 - 使用CommandLineRunner与ApplicationRunner
本篇文章我们将探讨CommandLineRunner和ApplicationRunner的使用. 在阅读本篇文章之前,你可以新建一个工程,写一些关于本篇内容代码,这样会加深你对本文内容的理解,关于如何 ...
- CommandLineRunner和ApplicationRunner的区别
CommandLineRunner和ApplicationRunner的区别 二者的功能和官方文档一模一样,都是在Spring容器初始化完毕之后执行起run方法 不同点在于,前者的run方法参数是St ...
- 使用CommandLineRunner或ApplicationRunner接口创建bean
在spring boot应用中,我们可以在程序启动之前执行任何任务.为了达到这个目的,我们需要使用CommandLineRunner或ApplicationRunner接口创建bean,spring ...
- CommandLineRunner and ApplicationRunner
1. Run spring boot as a standalone application (non-web) <?xml version="1.0" encoding=& ...
- 010-Spring Boot 扩展分析-ApplicationContextInitializer、CommandLineRunner、ApplicationRunner
一.常见的两个扩展点 1.ApplicationContextInitializer 1.1.作用实现 作用:接口实在Spring容器执行refresh之前的一个回调. Callback interf ...
- 项目启动加载配置,以及IP黑名单,使用CommandLineRunner和ApplicationRunner来实现(一般用在网关进行拦截黑名单)
//使用2个类的run方法都可以在项目启动时加载配置,唯一不同的是他们的参数不一样,CommandLineRunner的run方法参数是基本类型,ApplicationRunner的run方法参数是一 ...
- SpringBoot ApplicationRunner/CommandLineRunner
CommandLineRunner.ApplicationRunner 接口是在容器启动成功后的最后一步回调(类似开机自动启动). CommandLineRunner.ApplicationRunne ...
- SpringBoot之CommandLineRunner接口和ApplicationRunner接口
我们在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的.SpringBoot给我们提供了两个接口来帮助我们实现这种需求.这两个接口分别为CommandLi ...
- Spring Boot 启动加载数据 CommandLineRunner
实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求. 为了解决这样的问题,Spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来 ...
随机推荐
- js声明 对象,数组 的方法
i={} 对象字面量 等同 i = new Object();i=[] 数组字面量 等同 i = new Array();
- fist-第六天冲刺随笔
这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE1 这个作业要求在哪里 https://edu.cnblogs.com/campus/fz ...
- 【NOIP2017提高A组模拟9.12】Arrays and Palindrome
[NOIP2017提高A组模拟9.12]Arrays and Palindrome[SPJ] 题目 Description Input Output Sample Input 1 6 Sample O ...
- JZOJ8月15日提高组反思——2020年暑假终结篇
JZOJ8月15日提高组反思--2020年暑假终结篇 T1 仙人掌最短路 抱歉我只会最短路 仙人掌是啥? 听说是缩点+\(LCA\) 最短路30 T2 直接暴力计算 正解\(DP\) \(amazin ...
- maven私服拉取jar失败(内网)
解决方案: 1.私服没jar,找私服负责人添加 2.私服有拉不下来,重启nexus服务端(负责人做) 3.本地有jar但是idea中没找到,先执行一下reimport,不行的话,随意修改下pom文件, ...
- PyQt学习随笔:应用中通过installEventFilter安装重写的eventFilter捕获应用事件的方法
eventFilter函数是直接从QObject继承的定义的事件刷选虚拟函数,如果一个对象A使用installEventFilter函数将另一个对象B安装了B的实例方法eventFilter,则这个对 ...
- 深入浅出让你理解什么是LLVM
什么是LLVM 转载自https://www.jianshu.com/p/1367dad95445 LLVM项目是模块化.可重用的编译器以及工具链技术的集合. 美国计算机协会 (ACM) 将其2012 ...
- sql black list 绕过
Black list is so weak for you,isn't it 姿势: return preg_match("/set|prepare|alter|rename|select| ...
- HBase的基本使用(安装配置、启动关闭、hbash shell的基本操作、phoenix、实战)
HBase的前提条件: JDK SSH Hadoop JDK:Hadoop和JDK运行的环境,他们的守护进程运行在JVM下.HBase支持JDK 1.6以上的版本.比如: jdk-8u161-linu ...
- 混合云存储打开的正确姿势——腾讯云存储网关 CSG
近年来,随着云计算的发展,越来越多的企业选择将IT系统基础设施转移到云上,上云有助于推动企业加快信息化.数字化.智能化的转型,但是很多企业对传统的业务系统依赖程度较高,短时间内将业务迁移上云将会面临很 ...