springboot项目启动成功后执行一段代码的两种方式
springboot项目启动成功后执行一段代码的两种方式
实现ApplicationRunner接口
package com.lnjecit.lifecycle; import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; /**
* @author lnj
* createTime 2018-11-07 22:37
**/
@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("通过实现ApplicationRunner接口,在spring boot项目启动后打印参数");
String[] sourceArgs = args.getSourceArgs();
for (String arg : sourceArgs) {
System.out.print(arg + " ");
}
System.out.println();
}
}
项目启动后,会打印如下信息:
实现CommandLineRunner接口
package com.lnjecit.lifecycle; import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component; /**
* @author lnj
* createTime 2018-11-07 22:25
**/
@Component
public class CommandLineRunnerImpl implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("通过实现CommandLineRunner接口,在spring boot项目启动后打印参数");
for (String arg : args) {
System.out.print(arg + " ");
}
System.out.println();
}
}
两种实现方式的不同之处在于run方法中接收的参数类型不一样
指定执行顺序
当项目中同时实现了ApplicationRunner和CommondLineRunner接口时,可使用Order注解或实现Ordered接口来指定执行顺序,值越小越先执行
原理
通过调试可以发现都是在 org.springframework.boot.SpringApplication#run(java.lang.String...)方法内的afterRefresh(上下文刷新后处理)方法时,会执行callRunners方法。
afterRefresh方法具体实现如下:
protected void afterRefresh(ConfigurableApplicationContext context,
ApplicationArguments args) {
callRunners(context, args);
}
callRunners方法具体实现如下:
private void callRunners(ApplicationContext context, ApplicationArguments args) {
List<Object> runners = new ArrayList<Object>();
runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
AnnotationAwareOrderComparator.sort(runners);
for (Object runner : new LinkedHashSet<Object>(runners)) {
if (runner instanceof ApplicationRunner) {
callRunner((ApplicationRunner) runner, args);
}
if (runner instanceof CommandLineRunner) {
callRunner((CommandLineRunner) runner, args);
}
}
}
可以看出上下文完成刷新后,依次调用注册的runners, runners的类型为 ApplicationRunner 或 CommandLineRunner
案例地址
https://github.com/linj6/springboot-learn/tree/master/springboot-runner
参考资料
https://blog.csdn.net/zknxx/article/details/52196427
springboot项目启动成功后执行一段代码的两种方式的更多相关文章
- springboot启动后执行一段代码的方式
文章转载自: https://www.cnblogs.com/zuidongfeng/p/9926471.html https://blog.csdn.net/zknxx/article/detail ...
- Springboot - 在启动完成后执行特定方法
1.实现方式 实现ApplicationRunner接口 实现CommandLineRunner接口 @Component @Slf4j public class AfterServiceStarte ...
- Unittest 支持 case 失败后自动截图功能的另外两种方式
原生的unittest框架是不支持case失败后自动截图的功能的,网上看了大家的解决办法,大体上分为两种:1.要么加装饰器2.也有人封装断言这里我们看看还有没有其他的更加方便的方法值得大家一起探讨一下 ...
- android启动第一个界面时即闪屏的核心代码(两种方式)
闪屏,就是SplashScreen,也能够说是启动画面,就是启动的时候,闪(展示)一下,持续数秒后.自己主动关闭. 第一种方式: android的实现很easy,使用Handler对象的postDe ...
- springboot项目启动并立即执行自定义程序内容
第一种:实现ApplicationRunner接口,重写其中的run()方法: 第二种:实现CommandLineRunner接口,重写其中的run()方法: 还有第三种...
- 执行xxx.sh脚本的两种方式
因公司测试环境的登录模式有2种,大佬们直接写了个脚本完成一键切换,看了其中的脚本文件,其中出现了send "sh out.sh\r":一直疑惑这里的sh out.sh的意思...查 ...
- selenium中webdriver跳转新页面后定位置新页面的两种方式
刚刚在写Python爬虫的时候用到了selenium , 在跳转新页面时发现无法定位新页面 , 查找不到新页面的元素 一番查询后得到了解决方法 , 便记录下来备忘 , 也与大家分享 # 页面跳转代码. ...
- springboot2.X 在项目启动后执行一段自定义代码
场景: 项目需要在项目启动后从数据库初始化一些数据进入redis , 但是没有很适合 的监听器去实现 , 监听 老是在dao初始化之前触发. 解决方法:自定义类实现 ApplicationRunner ...
- 在springboot中使用Mybatis Generator的两种方式
介绍 Mybatis Generator(MBG)是Mybatis的一个代码生成工具.MBG解决了对数据库操作有最大影响的一些CRUD操作,很大程度上提升开发效率.如果需要联合查询仍然需要手写sql. ...
随机推荐
- app开发相关
app播放UIWebview 没有声音解决: 设置 allowsInlineMediaPlayback = YES; mediaPlaybackRequiresUserAction = NO
- MathExam第二次作业(升级版)
MathExamLv2——林华伟 211506319 陈珍 211406263 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实 ...
- Python:文件操作总结1——文件基本操作
一.文件的操作流程 1.打开文件,得到文件句柄并赋值给一个变量 2.通过句柄对文件进行操作 3.关闭文件 二.文件的打开与关闭 A.文件的打开——open函数 语法:open(file[,mode[, ...
- OpenCV学习笔记——Mat类型数据存储
CV_[The number of bits per item][Signed or Unsigned][Type Prefix]C[The channel number] 比如 CV_8UC3 表示 ...
- 第八,九周web制作代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or ...
- 未能加载文件或程序集“log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821”或它的某一个依赖项。系统找不到指定的文件。
在网上找了很久,很多个地方让修改配置文件,也有重装log4net的. 如文章:使用Common.Logging与log4net的组件版本兼容问题 我检查下发现项目中的package包中的Log4net ...
- lintcode-208-赋值运算符重载
208-赋值运算符重载 实现赋值运算符重载函数,确保: 新的数据可准确地被复制 旧的数据可准确地删除/释放 可进行 A = B = C 赋值 说明 本题只适用于C++,因为 Java 和 Python ...
- MDL详解
以下的虚拟内存可以理解成逻辑内存,因为我觉得只有这样才能讲通下面所有的东西.以下的“未分页”指没有为页进行编码. 以下为MDL结构体(我很郁闷,我在MSDN上没有找到这个结构体) typedef st ...
- python获取前几天的时间
days的参数就是你想获取前多少天的数据,如果是昨天的话,则days=1 import datetime today=datetime.date.today() oneday=datetime.tim ...
- BETA预发布演示视频
视频连接:优酷http://v.youku.com/v_show/id_XMTgxMjQxMjc0NA==.html?from=y1.7-2