Spring boot CommandLineRunner接口使用例子
前言
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接口使用例子的更多相关文章
- spring boot rest 接口集成 spring security(2) - JWT配置
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- spring boot rest 接口集成 spring security(1) - 最简配置
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- Spring Boot SSL [https]配置例子
前言 本文主要介绍Spring Boot HTTPS相关配置,基于自签证书实现: 通过本例子,同样可以了解创建SSL数字证书的过程: 本文概述 Spring boot HTTPS 配置 server. ...
- Spring Boot 2 + jpa + mysql例子
Spring Data框架为数据访问提供了一个通用的模型,无论访问哪种数据库,都可以使用同样的方式,主要有以下几个功能:(1)提供数据与对象映射的抽象层,同一个对象,可以被映射为不同数据库的数据:(2 ...
- Spring Boot CommandLineRunner的使用
1. 说明 程序在启动完成的时候需要去处理某些业务,因此Spring Boot程序中需要去实现CommandLineRunner接口. 2. CommandLineRunner方法执行顺序 程序启动后 ...
- spring boot:给接口增加签名验证(spring boot 2.3.1)
一,为什么要给接口做签名验证? 1,app客户端在与服务端通信时,通常都是以接口的形式实现, 这种形式的安全方面有可能出现以下问题: 被非法访问(例如:发短信的接口通常会被利用来垃圾短信) 被重复访问 ...
- spring boot: 设计接口站api的版本号,支持次版本号(spring boot 2.3.2)
一,为什么接口站的api要使用版本号? 1,当服务端接口的功能发生改进后, 客户端如果不更新版本, 则服务端返回的功能可能不能使用, 所以在服务端功能升级后, 客户端也要相应的使用 ...
- 寻找写代码感觉(三)之使用 Spring Boot 编写接口
一.前言 项目配置完之后,接着就是写接口了,那咱们就开始吧. 二.项目配置补充知识点 上篇文章写的是关于项目属性配置的一些知识,这里针对上次遗忘内容进行补充如下: 2.1.获取配置文件的值 在appl ...
- spring boot:使接口返回统一的RESTful格式数据(spring boot 2.3.1)
一,为什么要使用REST? 1,什么是REST? REST是软件架构的规范体系,它把资源的状态用URL进行资源定位, 以HTTP动作(GET/POST/DELETE/PUT)描述操作 2,REST的优 ...
随机推荐
- stark组件开发之分页
""" 分页组件 """ class Pagination(object): def __init__(self, current_page ...
- centos 7安装java开发环境
https://jingyan.baidu.com/article/29697b91660672ab20de3c15.html 自带版本是有问题的~
- python基础之Day10
一.函数的返回值 1.什么是返回值返回值是一个函数的处理结果, 2.为什么要有返回值如果我们需要在程序中拿到函数的处理结果做进一步的处理,则需要函数必须有返回值 3.函数的返回值的应用函数的返回值用r ...
- Eclipse Golang 开发环境搭建 GoClipse 插件
Windows平台 下载完成后,直接双击安装即可 默认情况下,.msi文件会安装在 C:\Go 目录下.可以将 C:\Go\bin 目录添加到环境变量 PATH 中,方便调用命令. Go 里面有两个非 ...
- c++11 线程池学习笔记 (一) 任务队列
学习内容来自一下地址 http://www.cnblogs.com/qicosmos/p/4772486.html github https://github.com/qicosmos/cosmos ...
- c语言模拟c++的继承和多态
//C++中的继承与多态 struct A { virtual void fun() //C++中的多态:通过虚函数实现 { cout << "A:fun()" < ...
- springmvc接收数组方式总结
1.接受正常的数组 如param1=aaa¶m1=bbb¶m1=3 对于这种,在实体参数中,使用String param1[] 这种参数既可以获取数组的值 2.接受数组 ...
- 基于UML的毕业选题系统建模研究
一.基本信息 标题:基于UML的毕业选题系统建模研究 时间:2018 出版源:电脑迷 领域分类:UML建模技术 二.研究背景 问题定义:为了加强学生设计分析开发软件的相关能力,有效避免结构化模型存在的 ...
- 冲刺博客NO.9
今天做了什么: 看书,看视频学UI设计,尝试设计并美化,然并没有美感,感觉自己设计的界面太丑. 主体进度差不多完成了,美化.
- undo空间满的处理方法(含undo的学习与相关解释)
1.查看数据库当前实例使用的是哪个UNDO表空间: show parameter undo_tablespace 2.查看UNDO表空间对应的数据文件和大小 pages col file_name f ...