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 { /** ...
随机推荐
- Navicat Premium 12.0.22 安装与破解
一.安装 Navicat Premium 12.0.22的下载链接:https://pan.baidu.com/s/1swRY_fwIZfufdxDZj3hDyw 密码:09k8 安装步骤就是一路向下 ...
- 解决Git 克隆代码 The remote end hung up unexpectedly错误
从GitHub上克隆一个项目一直不成功!猜想可能是文件太大超时或者网络太慢超时! 解决方案: 配置 git config -- git config -- 增加最低速时间,but,还是不行! 公司网络 ...
- duba网址对firefox快捷方式的劫持
直接删除 “驱动精灵” 即可. 等我 二进制安全 学好了,一定开发一种病毒专干这种劫持的,煞笔软件.
- C# 协变、逆变
微软官方概述: 在C#中,协变和逆变能够实现数组类型.委托类型和泛型类型参数的隐式引用转换.协变保留分配兼容性,逆变则与之相反. 协变:能够使用与原始指定的派生类型相比,派生程度更大的类型. 逆变:能 ...
- Dev控件使用CheckedListBoxControl获取items.count为0 的解决方法
CheckedListBoxControl,我使用DataSource属性,给其绑定了一个List对象.界面显示都挺正常的,当若干个项的复选框被选中的后,它的checkedListBoxControl ...
- ef linq多表查询(三表)
var ph = (from c in reef.CQICC_CLIENT join v in reef.CQICC_VISIT_CLIENT on c.CLIENT_T_ID equals v.CL ...
- Eureka源码解析系列文章汇总
先看一张图 0 这个图是Eureka官方提供的架构图,整张图基本上把整个Eureka的核心功能给列出来了,当你要阅读Eureka的源码时可以参考着这个图和下方这些文章 EurekaServer Eur ...
- android studio学习----gradle命令详解
首先来给大家介绍一种简便并且个人最喜欢的一种办法.很多时候我们在GitHub上看到一个不错的开源项目,一般有两种需求,阅读源码和查看运行效果,如果是单纯的查看源码我更喜欢用一些轻量级编辑器,如vim, ...
- ANDROID培训准备资料之Service
在讨论Service 之前,我们需要了解两点,非常重要的两点 (1) Service 不会专门启动一条单独的进程,Service与它所在应用位于同一个进程中 (2) Service也不 ...
- Phoenix 无法启动报错: java.net.BindException: Address already in use
一.问题描述 i. 登录Ambari发现有一个节点的 Phoenix 无法启动 ii. 在Ambari上点击“Start”,监控 Phoenix 日志文件 iii. Phoenix 日志如下: [ro ...