前言

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()方法里使用任何依赖,因为它们已经初始化好了;

转载自:

Spring boot CommandLineRunner接口使用例子

Springboot学习—CommandLineRunner接口(转载)的更多相关文章

  1. SpringBoot系列: CommandLineRunner接口的用处

    ========================================使用 CommandLineRunner 对Spring Bean进行额外初始化==================== ...

  2. SpringBoot之CommandLineRunner接口和ApplicationRunner接口

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

  3. Springboot学习06-Spring AOP封装接口自定义校验

    Springboot学习06-Spring AOP封装接口自定义校验 关键字 BindingResult.Spring AOP.自定义注解.自定义异常处理.ConstraintValidator 前言 ...

  4. springboot 学习之路 9 (项目启动后就执行特定方法)

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  5. SpringBoot学习笔记(10):使用MongoDB来访问数据

    SpringBoot学习笔记(10):使用MongoDB来访问数据 快速开始 本指南将引导您完成使用Spring Data MongoDB构建应用程序的过程,该应用程序将数据存储在MongoDB(基于 ...

  6. SpringBoot学习笔记

    SpringBoot个人感觉比SpringMVC还要好用的一个框架,很多注解配置可以非常灵活的在代码中运用起来: springBoot学习笔记: .一.aop: 新建一个类HttpAspect,类上添 ...

  7. Springboot学习03-SpringMVC自动配置

    Springboot学习03-SpringMVC自动配置 前言 在SpringBoot官网对于SpringMVCde 自动配置介绍 1-原文介绍如下: Spring MVC Auto-configur ...

  8. 使用JWT设计SpringBoot项目api接口安全服务

    转载直: 使用JWT设计SpringBoot项目api接口安全服务

  9. java springboot调用第三方接口 借助hutoool工具类 爬坑

    楼主是个后端小白一枚,之前没接触过后端,只学了java基本语法,还是在学校老师教的,学的很浅,什么ssh.ssm框架都没有学,最近在自学spring boot,看书学也看不是很懂,就在b站上看教学视频 ...

随机推荐

  1. 【转】golang 结构体和方法

    原文:https://www.jianshu.com/p/b6ae3f85c683 ---------------------------------------------------------- ...

  2. Java笔记(第五篇)

    抛出异常 使用throws声明抛出异常 Throws 通常用于方法声明,当方法中可能存在异常,却不想在方法中对异常进行处理时,就可以在声明方法时使用throws声明抛出的异常,然后再调用该方法的其他方 ...

  3. 串口+RS485驱动

    其实RS485不算什么协议,只是物理层做了差分传输,AB两线的电压差来表示0,1,0,1,可靠性和距离更加好,因此,一个串口外设只能作为半双工使用,而RS232是可以全双工的. max485模块可以直 ...

  4. qs.stringify()与JSON.stringify()区别

    qs.stringify.JSON.stringify虽然都是序列化,但他俩却不是一个东西. qs是nodejs的一个模块 JSON.stringify是js自带的方法,是将json对象转换为json ...

  5. 2、docker安装:内核要求、docker三要素、安装、helloworld、底层原理

    1.前提说明 1.CentOS Docker 安装 Docker支持以下的CentOS版本: CentOS 7 (64-bit) CentOS 6.5 (64-bit) 或更高的版本 2.前提条件:内 ...

  6. vue 解析时表达式闪烁的问题

    现象: 在使用 vuejs.angularjs 开发时,经常会遇见浏览器页面闪现表达式 ({{ express }} ), 或者是模块(div)的闪烁. 原因: 由于 JavaScript 去操作DO ...

  7. Java 显示调用隐式调用

    当你没有使用父类默认的构造方法时,此时在子类的构造方法中就需要显示的调用父类定义的构造方法.比如:父类:class Animal{ private String name; //如果你定义一个新的构造 ...

  8. Codeforces Round #346 (Div. 2) B题

    B. Qualifying Contest Very soon Berland will hold a School Team Programming Olympiad. From each of t ...

  9. js 获取滚动条的高度

    function getScrollTop() { var scroll_top = 0; if (document.documentElement && document.docum ...

  10. Codevs 1404 字符串匹配(Kmp)

    1404 字符串匹配 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给你两个串A,B,可以得到从A的任意位开始的子串和B匹配的 ...