如何在Spring Boot应用启动之后立刻执行一段逻辑

1. 前言
不知道你有没有接到这种需求,项目启动后立马执行一些逻辑。比如简单的缓存预热,或者上线后的广播之类等等。如果你使用 Spring Boot 框架的话就可以借助其提供的接口CommandLineRunner和 ApplicationRunner来实现。
2. CommandLineRunner
org.springframework.boot.CommandLineRunner 是Spring Boot提供的一个接口,当你实现该接口并将之注入Spring IoC容器后,Spring Boot应用启动后就会执行其run方法。一个Spring Boot可以存在多个CommandLineRunner的实现,当存在多个时,你可以实现Ordered接口控制这些实现的执行顺序(Order 数值越大优先级越低)。接下来我们来声明两个实现并指定顺序:
优先执行:
package cn.felord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
/**
* 优先级最高
* 该类期望在springboot 启动后第一顺位执行
* @author felord.cn
* @since 12:57
**/
@Slf4j
@Component
public class HighOrderCommandLineRunner implements CommandLineRunner, Ordered {
@Override
public void run(String... args) throws Exception {
for (String arg : args) {
log.info("arg = " + arg);
}
log.info("i am highOrderRunner");
}
@Override
public int getOrder() {
return Integer.MIN_VALUE+1;
}
}
第二顺序执行:
package cn.felord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
/**
* 优先级低于{@code HighOrderCommandLineRunner}
* @author felord.cn
* @since 12:59
**/
@Slf4j
@Component
public class LowOrderCommandLineRunner implements CommandLineRunner, Ordered {
@Override
public void run(String... args) throws Exception {
log.info("i am lowOrderRunner");
}
@Override
public int getOrder() {
return Integer.MIN_VALUE+1;
}
}
然后启动Spring Boot应用后,控制台按照预定的顺序打印出了结果:
2020-05-30 23:11:03.685 INFO 11976 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-05-30 23:11:03.701 INFO 11976 --- [ main] c.f.Application : Started SpringBootApplication in 4.272 seconds (JVM running for 6.316)
2020-05-30 23:11:03.706 INFO 11976 --- [ main] c.f.HighOrderCommandLineRunner : i am highOrderRunner
2020-05-30 23:11:03.706 INFO 11976 --- [ main] c.f.LowOrderCommandLineRunner : i am lowOrderRunner
3. ApplicationRunner
在Spring Boot 1.3.0又引入了一个和CommandLineRunner功能一样的接口ApplicationRunner。CommandLineRunner接收可变参数String... args,而ApplicationRunner 接收一个封装好的对象参数ApplicationArguments。除此之外它们功能完全一样,甚至连方法名都一样。 声明一个ApplicationRunner并让它优先级最低:
package cn.felord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
/**
* 优先级最低
* @author felord.cn
* @since 13:00
**/
@Slf4j
@Component
public class DefaultApplicationRunner implements ApplicationRunner, Ordered {
@Override
public void run(ApplicationArguments args) throws Exception {
log.info("i am applicationRunner");
Set<String> optionNames = args.getOptionNames();
log.info("optionNames = " + optionNames);
String[] sourceArgs = args.getSourceArgs();
log.info("sourceArgs = " + Arrays.toString(sourceArgs));
List<String> nonOptionArgs = args.getNonOptionArgs();
log.info("nonOptionArgs = " + nonOptionArgs);
List<String> optionValues = args.getOptionValues("foo");
log.info("optionValues = " + optionValues);
}
@Override
public int getOrder() {
return Integer.MIN_VALUE+2;
}
}
按照顺序打印了三个类的执行结果:
2020-06-01 13:02:39.420 INFO 19032 --- [ main] c.f.MybatisResultmapApplication : Started MybatisResultmapApplication in 1.801 seconds (JVM running for 2.266)
2020-06-01 13:02:39.423 INFO 19032 --- [ main] c.f.HighOrderCommandLineRunner : i am highOrderRunner
2020-06-01 13:02:39.423 INFO 19032 --- [ main] c.f.LowOrderCommandLineRunner : i am lowOrderRunner
2020-06-01 13:02:39.423 INFO 19032 --- [ main] c.f.DefaultApplicationRunner : i am applicationRunner
2020-06-01 13:02:39.423 INFO 19032 --- [ main] c.f.DefaultApplicationRunner : optionNames = []
2020-06-01 13:02:39.423 INFO 19032 --- [ main] c.f.DefaultApplicationRunner : sourceArgs = []
2020-06-01 13:02:39.423 INFO 19032 --- [ main] c.f.DefaultApplicationRunner : nonOptionArgs = []
2020-06-01 13:02:39.423 INFO 19032 --- [ main] c.f.DefaultApplicationRunner : optionValues = null
Ordered接口并不能被@Order注解所代替。
4. 传递参数
相信很多同学看到这里都开始对这两个run方法的入参感兴趣了。Spring Boot应用启动时是可以接受参数的,换句话说也就是Spring Boot的main方法是可以接受参数的。这些参数通过命令行 java -jar yourapp.jar 来传递。CommandLineRunner会原封不动照单全收这些接口,这些参数也可以封装到ApplicationArguments对象中供ApplicationRunner调用。 我们来认识一下ApplicationArguments的相关方法:
getSourceArgs() 被传递给应用程序的原始参数,返回这些参数的字符串数组。
getOptionNames() 获取选项名称的
Set字符串集合。如--spring.profiles.active=dev --debug将返回["spring.profiles.active","debug"]。getOptionValues(String name) 通过名称来获取该名称对应的选项值。如
--foo=bar --foo=baz将返回["bar","baz"]。containsOption(String name) 用来判断是否包含某个选项的名称。
getNonOptionArgs() 用来获取所有的无选项参数。
接下来我们试验一波,你可以通过下面的命令运行一个 Spring Boot应用 Jar
java -jar yourapp.jar --foo=bar --foo=baz --dev.name=码农小胖哥 java felordcn
或者在IDEA开发工具中打开Spring Boot应用main方法的配置项,进行如下配置,其他IDE工具同理。

运行Spring Boot应用,将会打印出:
2020-06-01 15:04:31.490 INFO 13208 --- [ main] c.f.HighOrderCommandLineRunner : arg = --foo=bar
2020-06-01 15:04:31.490 INFO 13208 --- [ main] c.f.HighOrderCommandLineRunner : arg = --foo=baz
2020-06-01 15:04:31.490 INFO 13208 --- [ main] c.f.HighOrderCommandLineRunner : arg = --dev.name=码农小胖哥
2020-06-01 15:04:31.490 INFO 13208 --- [ main] c.f.HighOrderCommandLineRunner : arg = java
2020-06-01 15:04:31.490 INFO 13208 --- [ main] c.f.HighOrderCommandLineRunner : arg = felordcn
2020-06-01 15:04:31.491 INFO 13208 --- [ main] c.f.HighOrderCommandLineRunner : i am highOrderRunner
2020-06-01 15:04:31.491 INFO 13208 --- [ main] c.f.LowOrderCommandLineRunner : i am lowOrderRunner
2020-06-01 15:04:31.491 INFO 13208 --- [ main] c.f.DefaultApplicationRunner : i am applicationRunner
2020-06-01 15:04:31.491 INFO 13208 --- [ main] c.f.DefaultApplicationRunner : optionNames = [dev.name, foo]
2020-06-01 15:04:31.491 INFO 13208 --- [ main] c.f.DefaultApplicationRunner : sourceArgs = [--foo=bar, --foo=baz, --dev.name=码农小胖哥, java, felordcn]
2020-06-01 15:04:31.491 INFO 13208 --- [ main] c.f.DefaultApplicationRunner : nonOptionArgs = [java, felordcn]
2020-06-01 15:04:31.491 INFO 13208 --- [ main] c.f.DefaultApplicationRunner : optionValues = [bar, baz]
然后你就可以根据实际需要动态地执行一些逻辑。
5. 总结
今天我们对CommandLineRunner和ApplicationRunner进行了讲解,从用法到顺序执行,又对Spring Boot传递参数进行了介绍和演示,希望对你有所帮助。多多关注:码农小胖哥,更多编程干货分享给你。
关注公众号:Felordcn 获取更多资讯
如何在Spring Boot应用启动之后立刻执行一段逻辑的更多相关文章
- (一)在Spring Boot应用启动之后立刻执行一段逻辑
在Spring Boot应用启动之后立刻执行一段逻辑 1.CommandLineRunner 2.ApplicationRunner 3.传递参数 码农小胖哥:如何在Spring Boot应用启动之后 ...
- 如何在Spring Boot 中动态设定与执行定时任务
本篇文章的目的是记录并实现在Spring Boot中,动态设定与执行定时任务. 我的开发项目是 Maven 项目,所以首先需要在 pom.xml 文件中加入相关的依赖.依赖代码如下所示: <de ...
- 如何在Spring boot中修改默认端口
文章目录 介绍 使用Property文件 在程序中指定 使用命令行参数 值生效的顺序 如何在Spring boot中修改默认端口 介绍 Spring boot为应用程序提供了很多属性的默认值.但是有时 ...
- 如何在Spring Boot开启事务
说到事务,那什么是事务呢? 事务(Transaction),一般是指要做的或所做的事情. 原子性(Atomicity):事务作为一个整体被执行,包含在其中的对数据库的操作要么全部被执行,要么都不执行. ...
- spring boot无法启动,或者正常启动之后无法访问报404的解决办法
以前用spring boot都是用idea的自动创建,或者是用的Jhipster创建的,就没有深究怎么去搭建.但是今天晚上心血来潮,想自己搭一个demo来整合一些技术,于是就花一点时间来手动搭.因为今 ...
- spring boot容器启动详解
目录 一.前言 二.容器启动 三.总结 =======正文分割线====== 一.前言 spring cloud大行其道的当下,如果不了解基本原理那么是很纠结的(看见的都是约定大于配置,但是原理呢?为 ...
- 让Spring Boot项目启动时可以根据自定义配置决定初始化哪些Bean
让Spring Boot项目启动时可以根据自定义配置决定初始化哪些Bean 问题描述 实现思路 思路一 [不符合要求] 思路二[满足要求] 思路三[未试验] 问题描述 目前我工作环境下,后端主要的框架 ...
- Spring boot自定义启动字符画(banner)
spring boot项目启动时会打印spring boot的ANSI字符画,可以进行自定义. 如何自定义 实现方式非常简单,我们只需要在Spring Boot工程的/src/main/resourc ...
- spring boot 项目启动无任何反应
遇到的问题 spring boot项目启动后无任何报错,ps有进程,nohub无日志 定位 更换jar包,问题依然存在,将jar包放到其他服务器,运行正常,排除打包问题 同服务器其他系统运行正常,但停 ...
随机推荐
- mongodb windows 集群搭建
准备三台机器,系统:windows 8 192.168.1.1 192.168.1.2 192.168.1.3 每台机器上安装mongodb 服务,步骤: 下载以下文件并依次执行安装 clearcom ...
- Flash 被禁止运行的方法
下面以谷歌浏览器 和 火狐浏览器 来说明. █ 自2020 年以来, 谷歌浏览器(Chome)已全面禁止Flash的运行,我们可以通过如下方法开启: █ 火狐(FireFox) 也禁止,但需要点“运行 ...
- WordPress安装:零基础入门教程
WordPress安装:零基础入门教程 1主机空间要求要运行 WordPress,主机空间需满足以下条件.不过现在网络上的空间基本都可以,而且还让你随意定制Php和Mysql版本,至于空间和数据库大小 ...
- 一个学习 Koa 源码的例子
作者: MarkLin 学习目标: 原生 node 封装 中间件 路由 Koa 原理 一个 nodejs 的入门级 http 服务代码如下, // index.js const http = requ ...
- virtualbox复制了以后网卡启动不了。
还是有些坑..必须踩 virtualbox复制了以后网卡启动不了.原因是如果没选重新生成mac ,那么mac地址会和第一台 机器一模一样,出现ip 征用的情况. 第二是系统方面的原因,你要删一个文件, ...
- vue组件试错
[Vue warn]: Property or method "child1" is not defined on the instance but referenced duri ...
- promise对象里resolve和reject状态讲解及Promise.all()的使用
首先来说下同步异步与阻塞非阻塞的概念,同步异步与阻塞非阻塞并没有关系.同步异步主要是事情做完以后,如何进行处理.或者说关注的是一种消息通信机制. 同步的情况下,是由处理消息者自己去等待消息是否被触发: ...
- docker基本维护命令
docker search centos ##查服务器上面的镜像:docker images ##查本地的镜像.docker pull centos ##拉镜像. docker run centos ...
- 关于php中数据提交到当前页面action的问题
关于php中数据提交到当前页面action的问题 2011-06-21 17:45杨超★杰伦 | 分类:PHP | 浏览695次 php中数据提交到当前页面,有人action=“<?php ec ...
- 苏浪浪 201771010120 面向对象程序设计(Java)第13周
/实验十三 图形界面事件处理技术 1.实验目的与要求 (1) 掌握事件处理的基本原理,理解其用途: (2) 掌握AWT事件模型的工作机制: (3) 掌握事件处理的基本编程模型: (4) 了解GUI界 ...