SpringBoot的ApplicationRunner
Article1
在开发中可能会有这样的情景。需要在容器启动的时候执行一些内容。比如读取配置文件,数据库连接之类的。SpringBoot给我们提供了两个接口来帮助我们实现这种需求。这两个接口分别为CommandLineRunner和ApplicationRunner。他们的执行时刻为容器启动完成的时候。
这两个接口中有一个run方法,我们只需要实现这个方法即可。这两个接口的不同之处在于:ApplicationRunner中run方法的参数为ApplicationArguments,而CommandLineRunner接口中run方法的参数为String数组。目前我在项目中用的是ApplicationRunner。是这么实现的:
package com.jdddemo.demo.controller; import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component; @Component
public class JDDRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(args);
System.out.println("这个是测试ApplicationRunner接口");
}
}
执行结果如下:
可用于添加缓存、定时任务,也可实现某些业务逻辑(@Autowired可以注入JavaBean)
---------------------
作者:姜小栀在北
来源:CSDN
原文:https://blog.csdn.net/jdd92/article/details/81053404
Article2
业务场景:
应用服务启动时,加载一些数据和执行一些应用的初始化动作。如:删除临时文件,清除缓存信息,读取配置文件信息,数据库连接等。
1、SpringBoot提供了CommandLineRunner和ApplicationRunner接口。当接口有多个实现类时,提供了@order注解实现自定义执行顺序,也可以实现Ordered接口来自定义顺序。
注意:数字越小,优先级越高,也就是@Order(1)注解的类会在@Order(2)注解的类之前执行。
两者的区别在于:
ApplicationRunner中run方法的参数为ApplicationArguments,而CommandLineRunner接口中run方法的参数为String数组。想要更详细地获取命令行参数,那就使用ApplicationRunner接口
ApplicationRunner
@Component
@Order(value = 10)
public class AgentApplicationRun2 implements ApplicationRunner {
@Override
public void run(ApplicationArguments applicationArguments) throws Exception { }
}
CommandLineRunner
@Component
@Order(value = 11)
public class AgentApplicationRun implements CommandLineRunner { @Override
public void run(String... strings) throws Exception { }
}
---------------------
作者:小贼驴
来源:CSDN
原文:https://blog.csdn.net/weixin_38362455/article/details/83023025
SpringBoot的ApplicationRunner的更多相关文章
- SpringBoot之ApplicationRunner接口和@Order注解
我们在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的.SpringBoot给我们提供了ApplicationRunner接口来帮助我们实现这种需求.该接 ...
- SpringBoot的ApplicationRunner和CommandLineRunner
如果你需要在你的SpringBoot启动完成之后实现一些功能,那么可以通过创建class实现ApplicationRunner和CommandLineRunner来完成: @Component pub ...
- springboot备忘
1.springboot中有ApplicationRunner类,如果项目中的启动类名称也是ApplicationRunner,单元测试时需要注意:import不要import到springboot的 ...
- 六种方式,教你在SpringBoot初始化时搞点事情!
前言 在实际工作中总是需要在项目启动时做一些初始化的操作,比如初始化线程池.提前加载好加密证书....... 那么经典问题来了,这也是面试官经常会问到的一个问题:有哪些手段在Spring Boot 项 ...
- shiro中部分SpringCache失效问题
原文:https://www.cnblogs.com/liruiloveparents/p/9392159.html shiro中部分SpringCache失效问题 1.问题抛出 今天在做Spri ...
- SpringBoot-容器启动的时候执行一些内容
SpringBoot的ApplicationRunner.CommandLineRunner 场景: 在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的. ...
- SpringBoot启动加载类ApplicationRunner
SpringBoot启动加载类ApplicationRunner 有时希望项目在启动的时候加载一些系统参数,就要用到ApplicationRunner ApplicationRunner是一个接口,我 ...
- SpringBoot之CommandLineRunner接口和ApplicationRunner接口
我们在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的.SpringBoot给我们提供了两个接口来帮助我们实现这种需求.这两个接口分别为CommandLi ...
- SpringBoot扩展点之二:ApplicationRunner和CommandLineRunner的扩展
CommandLineRunner并不是Spring框架原有的概念,它属于SpringBoot应用特定的回调扩展接口: public interface CommandLineRunner { /** ...
随机推荐
- conda opencv cv2.imshow无法使用
error: -------src-dir-------/opencv-2.4.10/modules/highgui/src/window.cpp:501: error: (-2) The funct ...
- 查询SAP系统支持的ABAP版本
7.52可以使用select 内表,但是怎么看版本呢? 如果有在开发中用到ABAP 7.4&7.5个版本的新语法时,需要考虑到系统支持的ABAP版本,那么要怎么查看呢? 其实这个和SAP的内核 ...
- visa
- AppSetting配置工具类
<?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访 ...
- wc项目(node.js实现)
一.github地址:https://github.com/Jasminejiamei/homework-wc 二.PSP PSP Personal Software Process Stages 预 ...
- Python中创建虚拟环境(virtualenv模块)
针对环境:win7,python2.7 需要virtualenv模块1.测试是否已安装virtualenv模块 import virtualenv #没有报错则已安装 否则需要先安装vi ...
- DV型、OV型、EV型证书的主要区别
DV型和OV型证书的区别 DV和OV型证书最大的差别是:DV型证书不包含企业名称信息:而OV型证书包含企业名称信息,以下是两者差别对比表: DV OV 包含企业名称信息 否 是 验证公司名称 ...
- ezdml设计数据库
数据结构和设计的介绍 ezdml软件:https://www.lanzous.com/i6ew2pe 1 用ezdml设计数据表然后导出到mysql数据库中 点击生成模型 2 选择库 3 点击开始生成
- 安装教程-Xshell 5 远程连接工具的安装
Xshell 5 远程连接工具的安装 1.实验描述 物理机中安装 Xshell 5 ,为实现 Linux 等操作系统提供远程连接的环境. 2.实验环境 物理机系统:Windows 10 企业版 3.实 ...
- Mysqldump一次备份多个指定数据库
今天碰到了,就作个记录. mysqldump -u user -p --databases DB1 DB2 --single-transaction --master-data=2 --default ...