前言

Spring boot的CommandLineRunner接口主要用于实现在应用初始化后,去执行一段代码块逻辑,这段初始化代码在整个应用生命周期内只会执行一次。

如何使用CommandLineRunner接口

我们可以用以下三种方式去使用CommandLineRunner接口:

1)和@Component注解一起使用

这种使用方式相当简便,如下所示:

@Component
public class ApplicationStartupRunner implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass()); @Override
public void run(String... args) throws Exception {
logger.info("ApplicationStartupRunner run method Started !!");
}
}

2)和@SpringBootApplication注解一起使用

这种使用方式也相当的简单,参考代码如下:

@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer implements CommandLineRunner { @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootWebApplication.class);
} public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootWebApplication.class, args);
} @Override
public void run(String... args) throws Exception {
logger.info("Application Started !!");
}
}

3)声明一个实现了CommandLineRunner接口的Bean

这种方式其实也大同小异,就是在SpringBootApplication里定义一个Bean,改Bean实现了CommandLineRunner接口,参考代码如下:

ApplicationStartupRunner.java

public class ApplicationStartupRunner implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass());
@Override
public void run(String... args) throws Exception {
logger.info("Application Started !!");
}
}

注册ApplicationStartupRunner bean

@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer { @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootWebApplication.class);
} public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootWebApplication.class, args);
} @Bean
public ApplicationStartupRunner schedulerRunner() {
return new ApplicationStartupRunner();
}
}

注意:在实现CommandLineRunner接口时,run(String… args)方法内部如果抛异常的话,会直接导致应用启动失败,所以,一定要记得将危险的代码放在try-catch代码块里。

用@Order注解去设置多个CommandLineRunner实现类的执行顺序

一个应用可能存在多个 CommandLineRunner接口实现类,如果我们想设置它们的执行顺序,可以使用 @Order实现

@Order(value=3)
@Component
class ApplicationStartupRunnerOne implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass()); @Override
public void run(String... args) throws Exception {
logger.info("ApplicationStartupRunnerOne run method Started !!");
}
} @Order(value=2)
@Component
class ApplicationStartupRunnerTwo implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass()); @Override
public void run(String... args) throws Exception {
logger.info("ApplicationStartupRunnerTwo run method Started !!");
}
}

输出日志:

2017-03-08 13:55:04 - ApplicationStartupRunnerTwo run method Started !!
2017-03-08 13:55:04 - ApplicationStartupRunnerOne run method Started !!

为什么要使用CommandLineRunner接口

  • 实现在应用启动后,去执行相关代码逻辑,且只会执行一次;
  • spring batch批量处理框架依赖这些执行器去触发执行任务;
  • 我们可以在run()方法里使用任何依赖,因为它们已经初始化好了;

原文文链

Site4J

Spring boot CommandLineRunner接口使用例子的更多相关文章

  1. spring boot rest 接口集成 spring security(2) - JWT配置

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  2. spring boot rest 接口集成 spring security(1) - 最简配置

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  3. Spring Boot SSL [https]配置例子

    前言 本文主要介绍Spring Boot HTTPS相关配置,基于自签证书实现: 通过本例子,同样可以了解创建SSL数字证书的过程: 本文概述 Spring boot HTTPS 配置 server. ...

  4. Spring Boot 2 + jpa + mysql例子

    Spring Data框架为数据访问提供了一个通用的模型,无论访问哪种数据库,都可以使用同样的方式,主要有以下几个功能:(1)提供数据与对象映射的抽象层,同一个对象,可以被映射为不同数据库的数据:(2 ...

  5. Spring Boot CommandLineRunner的使用

    1. 说明 程序在启动完成的时候需要去处理某些业务,因此Spring Boot程序中需要去实现CommandLineRunner接口. 2. CommandLineRunner方法执行顺序 程序启动后 ...

  6. spring boot:给接口增加签名验证(spring boot 2.3.1)

    一,为什么要给接口做签名验证? 1,app客户端在与服务端通信时,通常都是以接口的形式实现, 这种形式的安全方面有可能出现以下问题: 被非法访问(例如:发短信的接口通常会被利用来垃圾短信) 被重复访问 ...

  7. spring boot: 设计接口站api的版本号,支持次版本号(spring boot 2.3.2)

    一,为什么接口站的api要使用版本号? 1,当服务端接口的功能发生改进后, 客户端如果不更新版本,    则服务端返回的功能可能不能使用,    所以在服务端功能升级后,     客户端也要相应的使用 ...

  8. 寻找写代码感觉(三)之使用 Spring Boot 编写接口

    一.前言 项目配置完之后,接着就是写接口了,那咱们就开始吧. 二.项目配置补充知识点 上篇文章写的是关于项目属性配置的一些知识,这里针对上次遗忘内容进行补充如下: 2.1.获取配置文件的值 在appl ...

  9. spring boot:使接口返回统一的RESTful格式数据(spring boot 2.3.1)

    一,为什么要使用REST? 1,什么是REST? REST是软件架构的规范体系,它把资源的状态用URL进行资源定位, 以HTTP动作(GET/POST/DELETE/PUT)描述操作 2,REST的优 ...

随机推荐

  1. 记录一次Centos磁盘空间占满的解决办法(转)

    原文地址:https://blog.csdn.net/everything1209/article/details/70209157 解决前 磁盘使用情况: 第二块磁盘使用率达到97% [root@f ...

  2. vue修改端口号

    vue修改端口号   默认端口号 修改端口号 重新启动项目 npm run dev        

  3. 201621123002《JAVA程序设计》第三章学习总结

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词,如类.对象.封装等 关键词:类 对象 封装 构造函数 this,static,final 1.2 用思维导图或者Onenote或 ...

  4. [SoapUI] 在SoapUI中,设置开关批量保存整个Response,作为期望结果进行校验

    //获取保存response的文件路径和名称 def testSuiteName = context.testCase.testSuite.name def testCaseName = contex ...

  5. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  6. Python获取当前类的所有成员属性

    # -*- coding: utf-8 -*- class Market(object): def __init__(self): self.title = 'apple' self.count = ...

  7. PB窗口根据分辨率的大小调整窗口大小

    //来自:http://topic.csdn.net/u/20070105/09/88f3c417-6882-4e26-b622-0f9a0a9a65e0.html //给你个通用函数,在窗口的OPE ...

  8. 开源播放器 ijkplayer (一) :使用Ijkplayer播放直播视频

    1.ijkplayer 编码 IjkPlayer支持硬解码和软解码. 软解码时不会旋转视频角度这时需要你通过onInfo的what == IMediaPlayer.MEDIA_INFO_VIDEO_R ...

  9. JS禁用键盘浏览器退格键

    我们在真实的项目开发中经常会使用JS 对键盘上的一些按键进行禁用,常见的比如说退格键(backspace/ 后退键),我在一个项目中就遇到过在页面编辑的时候禁用掉退格键,因为退格键会发生页面后退,这样 ...

  10. Android开发工程师文集-Android知识点讲解

    前言 大家好,给大家带来Android开发工程师文集-Android知识点讲解的概述,希望你们喜欢 WebView讲解 一般通过Intent调用系统的浏览器: Uri uri = Uri.parse( ...