SpringBoot源码分析之CommandLineRunner、ApplicationRunner
来源:互联网 发布:u盘如何恢复数据 编辑:程序博客网 时间:2024/06/05 20:10
我们在之前的文章中简单的说过SpringBoot的CommandLineRunner和ApplicationRunner这两个接口
SpringBoot之CommandLineRunner接口和ApplicationRunner接口,这篇文章中我们从源码上简单的分析一下这两个
接口。在org.springframework.boot.SpringApplication#run()这个方法中有这样一段代码:
afterRefresh(context, applicationArguments);
方法内容如下:
protected void afterRefresh(ConfigurableApplicationContext context, ApplicationArguments args) { callRunners(context, args); }
SpringBoot的注释中说,在上下文刷新完之后调用这个方法。在调用这个方法的时候Spring容器已经启动完
成了。这里的context的真正对象是:AnnotationConfigEmbeddedWebApplicationContext,这个类贯
穿着SpringBoot的整个启动过程。我们看一下callRunners这个方法的内容:
private void callRunners(ApplicationContext context, ApplicationArguments args) { List<Object> runners = new ArrayList<Object>(); //从Spring容器中查找类型为ApplicationRunner的Bean runners.addAll(context.getBeansOfType(ApplicationRunner.class).values()); //从Spring容器中查找类型为CommandLineRunner的Bean runners.addAll(context.getBeansOfType(CommandLineRunner.class).values()); //将上一步得到的Bean进行排序 AnnotationAwareOrderComparator.sort(runners); for (Object runner : new LinkedHashSet<Object>(runners)) { //如果是ApplicationRunner的实例 if (runner instanceof ApplicationRunner) { callRunner((ApplicationRunner) runner, args); } //如果是CommandLineRunner的实例 if (runner instanceof CommandLineRunner) { callRunner((CommandLineRunner) runner, args); } } }
callRunner方法的内容就很简单了直接调用run方法。
private void callRunner(ApplicationRunner runner, ApplicationArguments args) { try { (runner).run(args); } catch (Exception ex) { throw new IllegalStateException("Failed to execute ApplicationRunner", ex); } }
ApplicationRunner和CommandLineRunner的区别就是run方法参数不同,ApplicationRunner中run方法
的参数是ApplicationArguments,CommandLineRunner中run方法的参数是String类型的可变参数。。
阅读全文
0 0
- SpringBoot源码分析之CommandLineRunner、ApplicationRunner
- SpringBoot之CommandLineRunner接口和ApplicationRunner接口
- springboot之启动加载数据 CommandLineRunner 和ApplicationRunner
- 23.8 Using the ApplicationRunner or CommandLineRunner 实现ApplicationRunner 和CommandLineRunner
- Spring Boot CommandLineRunner和ApplicationRunner
- springboot实战之整合CommandLineRunner
- SpringBoot之启动加载数据 CommandLineRunner(G)
- Spring-boot中ApplicationRunner源码
- springboot源码分析2-springboot 之banner定制以及原理
- springboot源码分析4-springboot之SpringFactoriesLoader使用
- springboot源码分析5-springboot之命令行参数以及原理
- springboot源码分析6-springboot之PropertySource类初探
- SpringBoot:spring boot CommandLineRunner启动
- Spring Boot 启动时执行特殊的代码使用CommandLineRunner、ApplicationRunner
- SpringBoot RestTemplate源码分析
- springboot源码分析之环境属性构造过程1
- 第三十一章 SpringBoot使用CommandLineRunner
- SpringBoot通过CommandLineRunner实现参数的初始化
- 安装php框架---laravel教程
- 基于display:inline-block 的列表布局
- CSS清除浮动
- OpenGL多重纹理使用与理解
- 微机原理给出字符串比较
- SpringBoot源码分析之CommandLineRunner、ApplicationRunner
- Information Disturbing (树形dp+二分)
- Skynet基础入门例子详解(4)
- cookie、session
- 表的基本操作
- linux-lvm基本应用
- 3.2 计算浮点数相除的余数
- Leetcode 322 & 518
- 微机原理输入字符串并比较