如果我们想在项目启动后做一些事情(如加载定时任务,初始化工作),可以使用spring提供的CommandLineRunner、ApplicationRunner 接口,在容器启动成功后的最后一步回调(类似开机自启动)。

1. CommandLineRunner接口

/*** Interface used to indicate that a bean should <em>run</em> when it is contained within* a {@link SpringApplication}. Multiple {@link CommandLineRunner} beans can be defined* within the same application context and can be ordered using the {@link Ordered}* interface or {@link Order @Order} annotation.* <p>* If you need access to {@link ApplicationArguments} instead of the raw String array* consider using {@link ApplicationRunner}.** @author Dave Syer* @see ApplicationRunner*/
public interface CommandLineRunner {/*** Callback used to run the bean.* @param args incoming main method arguments* @throws Exception on error*/void run(String... args) throws Exception;}

多个CommandLineRunner可以被同时执行在同一个spring上下文中并且执行顺序是以order注解的参数顺序一致。

下面看一个demo:

@Order(2)
@Component
public class ServerStartedReport implements CommandLineRunner{@Overridepublic void run(String... args) throws Exception {System.out.println("===========ServerStartedReport启动====="+ LocalDateTime.now());}
}

配置参数启动项目:

控制台打印:

[ddd,tyy]
===========ServerStartedReport启动=====2019-02-14T21:31:30.466

2. ApplicationRunner接口

二者基本一样,区别在于接收的参数不一样。CommandLineRunner的参数是最原始的参数,没有做任何处理,而ApplicationRunner的参数是ApplicationArguments,对原始参数做了进一步的封装。
如我们在这里配置了一些启动参数--foo=hu --log=debug

CommandLineRunner只是获取--name=value。而ApplicationRunner可以解析--name=value,使得我们可以直接通过name来获取value。

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;import java.util.Arrays;@Component
public class MyApplicationRunner implements ApplicationRunner{@Overridepublic void run(ApplicationArguments args) throws Exception {System.out.println("ApplicationRunner:"+ Arrays.asList(args.getSourceArgs()));System.out.println("getOptionNames:"+args.getOptionNames());System.out.println("getOptionValues:"+args.getOptionValues("foo"));System.out.println("getOptionValues:"+args.getOptionValues("log"));}
}

打印结果为:

===========ServerStartedReport启动=====2019-02-14T21:36:23.668
ApplicationRunner:[--foo=hu, --log=debug]
getOptionNames:[log, foo]
getOptionValues:[hu]
getOptionValues:[debug]

注意启动后执行的方法一定要加try catch,因为此处抛出异常会影响项目启动。

CommandLineRunner、ApplicationRunner 接口相关推荐

  1. CommandLineRunner与ApplicationRunner接口的使用及源码解析

    引言 我们在使用SpringBoot搭建项目的时候,如果希望在项目启动完成之前,能够初始化一些操作,针对这种需求,可以考虑实现如下两个接口(任一个都可以) org.springframework.bo ...

  2. 使用CommandLineRunner或ApplicationRunner接口创建bean

    在spring boot应用中,我们可以在程序启动之前执行任何任务.为了达到这个目的,我们需要使用CommandLineRunner或ApplicationRunner接口创建bean,spring ...

  3. implements ApplicationRunner 接口的作用

    在开发过程中,需要在启动时执行一些功能,例如读取配置文件,加载缓存,数据库连接等. SpringBoot提供了两个接口去实现--CommandLineRunner.ApplicationRunner. ...

  4. ApplicationRunner 接口的作用

    SpringBoot 的 ApplicationRunner 接口可以让项目在启动时候初始化一些信息 , 比如 数据库连接 , 或者自定义的一些配置等; 步骤 : 重写ApplicationRunne ...

  5. springboot中的ApplicationRunner 接口

    springboot项目在启动的时候,有时候需要在启动之后直接执行某一些代码 package cn.wideth.util;import org.springframework.boot.Applic ...

  6. springboot教程(一)

    撸了今年阿里.头条和美团的面试,我有一个重要发现.......>>> 使用jdk:1.8.maven:3.3.3 spring获取Bean的方式 pom.xml文件内容: <? ...

  7. 如何在项目启动时就执行某些操作

    参考资料:如何在项目启动时就执行某些操作 在实际的项目开发中经常会遇到一些需要在项目启动的时候进行初始化操作的需求,比如初始化线程池,配置某些对象的序列化和反序列化方式,加载黑名单白名单,加载权限应用 ...

  8. Spring Boot实践--CommandLineRunner接口

    2019独角兽企业重金招聘Python工程师标准>>> 使用场景的提出: 我们在开发过程中会有这样的场景:需要在容器启动的时候执行一些内容,比如:读取配置文件信息,数据库连接,删除临 ...

  9. Spring Boot CommandLineRunner和ApplicationRunner

    在本快速教程中,我们将探索Spring Boot中两个非常流行的界面: CommandLineRunner和ApplicationRunner . 这些接口的一种常见用例是在应用程序启动时加载一些静态 ...

最新文章

  1. 【Flutter】Flutter 混合开发 ( Flutter 与 Native 通信 | 通信场景 | Channel 通信机制 | Channel 支持的通信数据类型 | Channel 类型 )
  2. python 计算 IOU
  3. 进程控制块PCB(进程描述符)
  4. 域用户频繁被锁定怎么解决_Oracle11g用户频繁锁定并且解锁后不允许登录
  5. c++ cdi+示例_C ++“或”关键字示例
  6. 阿里要把雄安打造成AI第一城:未来30年城市长啥样?
  7. IIS7的应用程序池详细解析
  8. 斐讯路由器k3c虚拟服务器,斐讯K3C路由器32.1.26.175如何打开telnet升级到官改固件教程...
  9. 2022年兽药行业发展前景
  10. 计算机垃圾桶桌面,电脑桌面比垃圾桶还乱?一分钟轻松快速整理你的电脑桌面...
  11. 算法——中国剩余定理
  12. 程序员,停止你的焦虑
  13. windows如何取消电脑自动关机命令
  14. (172)SystemVerilog[打两拍]
  15. 新三板上市和主板上市的区别主要是什么?
  16. Redhat6.5离线配置Zabbix,含自定义Zabbix监控项
  17. 分享一款好用的PDF转换器的免费注册验证码-All PDF Converter
  18. java list 和数组区别_java list和数组的区别
  19. 大中型公司运维基础运维
  20. dnf全部使用_dnf命令_Linux dnf 命令用法详解:新一代的RPM软件包管理器

热门文章

  1. NLP word2vec 计算优化
  2. flink 6-检查点和水位线
  3. 并发测试工具_软件测试工程师都在用哪些测试工具?
  4. 计算机导航 骨科 ppt模板,(医学PPT课件)术中即时三维导航在脊柱侧弯矫形的应用...
  5. linux常见的几种运行级,linux有几种运行级别
  6. scws sphinx mysql_Sphinx+Scws 搭建千万级准实时搜索应用场景详解
  7. Map和hashmap
  8. 物理化学 焓变的计算和相变焓
  9. 计算机设计学校,计算机设计制作大赛
  10. 北京内推 | 微软亚洲互联网工程院(STCA)招聘NLP算法实习生