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 来 ...
随机推荐
- PowerManagerService流程分析
一.PowerManagerService简介 PowerManagerService主要服务Android系统电源管理工作,这样讲比较笼统,就具体细节上大致可以认为PowerManagerServi ...
- Spring Boot 2.4.0 发布,配置文件重大调整,不要乱升级!!
前段时间 Spring Boot 2.4.0 发布了,栈长作了一个新特性全盘解读,其中介绍了一个很重要的变革,那就是配置文件. 配置文件可是每个框架的核心,不得不搞清楚,所以,这篇栈长就带大家深入实战 ...
- Python模块是否支持自定义属性使用双下划线开头和结尾?
我们知道在Python中,变量名类似__xxx__的,也就是以双下划线开头并且以双下划线结尾的变量和方法,是特殊变量,特殊变量是可以直接访问的,不是私有变量,所以,一般实例变量和类变量以及方法不能用_ ...
- Gradle上传依赖到私服(nexus)
子模块配置 buildscript { repositories { mavenLocal() maven { url "http://maven.aliyun.com/nexus/cont ...
- AcWing 404. 婚礼
大型补档计划 题目链接 根据题意,显然只有新郎这边可能存在矛盾,考虑这边怎么放即可,新娘那边的放法与这边正好相反且一一对应. 显然对于两个约束条件是一对矛盾,开始我以为可以用并查集,后来发现输出方案的 ...
- JAVA死锁的检测流程
步骤一. 查询检测的进程 1.首先查看系统资源占用信息,TOP看一下 发现正在运行的JAVA项目CPU占用率很高,百分之360左右了,那么问题一定出在这个程序中 2 .也可以通过名称查询进程pid 步 ...
- Android全面解析之Window机制
前言 你好! 我是一只修仙的猿,欢迎阅读我的文章. Window,读者可能更多的认识是windows系统的窗口.在windows系统上,我们可以多个窗口同时运行,每个窗口代表着一个应用程序.但在安卓上 ...
- vscode 编辑python文件
1 安装python 自动姿势 Chinese # 换成中文 path Autocomplete 路径自动补全 Vetur vue文件补全 配置文件 首选项-设置 应用程序 在 seyying.jso ...
- redis 存储验证码 基本使用
1 redis 存储验证码 基本使用 1.1 setting 配置 CACHES = { "default": { "BACKEND": "djang ...
- IOS中使用.xib文件封装一个自定义View
1.新建一个继承UIView的自定义view,假设类名叫做 MyAppVew #import <UIKit/UIKit.h> @class MyApp; @interface MyAppV ...