Article1

在开发中可能会有这样的情景。需要在容器启动的时候执行一些内容。比如读取配置文件,数据库连接之类的。SpringBoot给我们提供了两个接口来帮助我们实现这种需求。这两个接口分别为CommandLineRunnerApplicationRunner。他们的执行时刻为容器启动完成的时候。

这两个接口中有一个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的更多相关文章

  1. SpringBoot之ApplicationRunner接口和@Order注解

    我们在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的.SpringBoot给我们提供了ApplicationRunner接口来帮助我们实现这种需求.该接 ...

  2. SpringBoot的ApplicationRunner和CommandLineRunner

    如果你需要在你的SpringBoot启动完成之后实现一些功能,那么可以通过创建class实现ApplicationRunner和CommandLineRunner来完成: @Component pub ...

  3. springboot备忘

    1.springboot中有ApplicationRunner类,如果项目中的启动类名称也是ApplicationRunner,单元测试时需要注意:import不要import到springboot的 ...

  4. 六种方式,教你在SpringBoot初始化时搞点事情!

    前言 在实际工作中总是需要在项目启动时做一些初始化的操作,比如初始化线程池.提前加载好加密证书....... 那么经典问题来了,这也是面试官经常会问到的一个问题:有哪些手段在Spring Boot 项 ...

  5. shiro中部分SpringCache失效问题

    原文:https://www.cnblogs.com/liruiloveparents/p/9392159.html shiro中部分SpringCache失效问题   1.问题抛出 今天在做Spri ...

  6. SpringBoot-容器启动的时候执行一些内容

    SpringBoot的ApplicationRunner.CommandLineRunner 场景: 在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的. ...

  7. SpringBoot启动加载类ApplicationRunner

    SpringBoot启动加载类ApplicationRunner 有时希望项目在启动的时候加载一些系统参数,就要用到ApplicationRunner ApplicationRunner是一个接口,我 ...

  8. SpringBoot之CommandLineRunner接口和ApplicationRunner接口

    我们在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的.SpringBoot给我们提供了两个接口来帮助我们实现这种需求.这两个接口分别为CommandLi ...

  9. SpringBoot扩展点之二:ApplicationRunner和CommandLineRunner的扩展

    CommandLineRunner并不是Spring框架原有的概念,它属于SpringBoot应用特定的回调扩展接口: public interface CommandLineRunner { /** ...

随机推荐

  1. 阿里OSS 渗透案例

    采用JavaScript客户端直接签名时,AccessKeyID和AcessKeySecret会暴露在前端页面,因此存在严重的安全隐患. 渗透案例 阿里云Access Token问题 - 项目收获记录 ...

  2. SQL学习笔记之 数据库基础(一)

    数据库基础 数据库系统的组成:由数据库,数据库管理软件,数据库管理员DBA,支持数据库系统的硬件和软件组成,其中数据库管理员是对数据库进行规划.设计.维护.和监视的专业管理人员,在数据库系统中起着非常 ...

  3. 关于PHP在企业级开发领域的访谈

    企业软件的一个关键元素就是互操作性,它可以让软件与其他平台交换信息.大家都认为PHP在这方面表现欠佳,因为它的WS-*支持相对来说比较新且功能较少,成熟度不高.关于这点我们从未手动开启过PHP的相关进 ...

  4. VUE-父组件和子组件

    1.父组件 const cnp2 = Vue.extend({ template: ` <div> <h2>我是构造器2</h2> <cpn1>< ...

  5. Web Service 接口测试

    Web Service 接口测试 Web service 概念 Web service使用与平台和编程语言无关的方式进行通讯的一项技术, web service 是一个接口, 他描述了一组可以在网络上 ...

  6. Selenium通过监听事件实现自动截图

    需要继承extends TestListenerAdapter这个类 代码如下package com.mushishi.selenium.util; import java.util.ArrayLis ...

  7. python之便携式mysql类和tornado mysql长链接

    mymysql.py class MyMysql2(object): def __init__(self, host = '', user = '', passwd = '', db = '', po ...

  8. odoo10学习笔记九:Odoo10 API

    转载请转载原文地址:https://www.cnblogs.com/ygj0930/p/11189315.html 一:纪录集API model中的数据是以集合的形式使用的,因此可以使用集合运算来操作 ...

  9. linux 重启 网络服务

    设置网路ip等后 需要重启网路服务 debian: /etc/iinit.d/networking  restart centos7: service network restart or syste ...

  10. 004-OpenStack-计算服务

    OpenStack-计算服务 [基于此文章的环境]点我快速打开文章 1.控制节点(controller) 1.1 创库授权  nova_api, nova, 和 nova_cell0 mysql CR ...