springboot的ApplicationRunner和CommandLineRunner

简介: springboot的ApplicationRunner和CommandLineRunner

当我们希望在springboot容器后,执行一些操作的时候,我们可以考虑使用ApplicationRunner或者CommandLineRunner

问题:

  1. ApplicationRunner与CommandLineRunner谁先执行
  2. 两者的区别在哪
  3. 多个CommandLineRunner 执行顺序问题


1.ApplicationRunner与CommandLineRunner谁先执行。


我们在一个应用中分别实现两个接口

@Component
    public class CommandLineRunnerTest implements CommandLineRunner {
  @Override
  public void run(String... arg0) throws Exception {
    System.out.println("CommandLineRunner=====执行开始");
        for (int i = 0; i < arg0.length; i++) {
          System.out.println(arg0[i]);
        }
    System.out.println("CommandLineRunner=====执行完毕");
      }
    }
复制代码
@Component
    public class ApplicationRunnerTest implements ApplicationRunner {
      @Override
      public void run(ApplicationArguments arg0) throws Exception {
        System.out.println("ApplicationRunner=====执行开始");
        System.out.println(arg0.getNonOptionArgs());
        System.out.println(arg0.getOptionNames());
        System.out.println("ApplicationRunner=====执行完成");
      }
    }
复制代码


从执行结果来看,ApplicationRunner默认先于CommandLineRunner执行


ApplicationRunner=====执行开始
    []
    [name, age]
    [lisi]
    ApplicationRunner=====执行完成
    CommandLineRunner=====执行开始
    --name=lisi
    --age=12
    CommandLineRunner=====执行完毕
复制代码


2.两者的区别,以及与main方法的联系


两个接口中都有run方法,负责接收来自应用外部的参数,ApplicationRunner的run方法会将外部参数封装成一个ApplicationArguments对象,而CommandLineRunner接口中run方法的参数则为String数组。

我们再回头看看main方法

@SpringBootApplication
    public class AliyunmqApplication {
      public static void main(String[] args) {
        System.out.println("main方法==========执行开始");
        for (int i = 0; i < args.length; i++) {     
          System.out.println(args[i]);      
        }
        System.out.println("main方法==========执行完毕");
        SpringApplication.run(AliyunmqApplication.class, args);
      }
    }
复制代码

我们看到main方法也是接收一个args 数组参数。

执行发现:

main方法==========执行开始
    --name=lisi
    --age=12
    main方法==========执行完毕
    ..............
    ..............
    ApplicationRunner=====执行开始
    []
    [name, age]
    [lisi]
    ApplicationRunner=====执行完成
    CommandLineRunner=====执行开始
    --name=lisi
    --age=12
    CommandLineRunner=====执行完毕
复制代码

main方法的arg参数 和 CommandLineRunner方法的arg数组的值是一样的。

总的来看: ApplicationRunner run方法中的ApplicationArguments 对象,对输入参数做了封装,让我们可以使用 get**()的形式获取参数。CommandLineRunner run方法的arg数组则是原装。

以eclipse为例看参数设置:



image.png

image.png


3. 多个实现类的执行顺序


两种方法

  • 使用@Order(value=整数值)
  • 实现Ordered接口,在方法里return 一个顺序值
相关文章
|
Java Spring
Spring Boot CommandLineRunner接口详解
Spring Boot CommandLineRunner接口详解
Spring Boot CommandLineRunner接口详解
|
3月前
|
Java
Springboot 之 HandlerMethodArgumentResolver 运用
Springboot 之 HandlerMethodArgumentResolver 运用
32 0
|
4月前
|
Java
SpringBoot:第六篇 CommandLineRunner
SpringBoot:第六篇 CommandLineRunner
21 0
|
4月前
|
Java Spring 容器
[Spring 源码] 浅析 SpringApplication#run()
[Spring 源码] 浅析 SpringApplication#run()
|
Java Spring 容器
Spring Boot - ApplicationRunner && CommandLineRunner扩展接口
ApplicationRunner && CommandLineRunner扩展接口
211 0
Spring Boot - ApplicationRunner && CommandLineRunner扩展接口
SpringBoot自定义拦截器
在springboot中有一个叫做ResourceProperties的类,里面就定义了静态资源的默认查找路径: 默认的静态资源路径为: • classpath:/META-INF/resources/ • classpath:/resources/ • classpath:/static/ • classpath:/public 在项目中创建路径进行测试 在static目录下创建index.html文件
|
Java 数据处理 数据安全/隐私保护
Spring boot中CommandLineRunner的使用
日常开发中有可能需要实现项目启动后执行的功能,比如特殊数据处理,权限控制等。Spring boot提供了一种简单的实现方案。即CommandLineRunner
101 0
Spring boot中CommandLineRunner的使用
SpringBoot2.x基础篇:使用CommandLineRunner或ApplicationRunner
如果你想要使用`SpringBoot`构建的项目在启动后运行一些特定的代码,那么`CommandLineRunner`、`ApplicationRunner`都是很好的选择。
|
XML Java 数据格式
SpringBoot源码学习(三) BeanFactory 与 ApplicationContext
### 前言 + [上文](https://www.atatech.org/articles/145056) 已经分析了springboot启动的前半部分, 下面就要还是分析真正核心的spring容器的整个生命周期了, 但是在讲之前还是要把一些基本概念讲清楚。 + 因为这部分的文章在网络上实在是太多了, 随便搜一下就能搜到一脸盆 所以本文只会对我对这部分的认知做一些梳理和归纳 ##
87 0
SpringBoot源码学习(三) BeanFactory 与 ApplicationContext
|
Java 程序员 网络安全
SpringBoot应用使用自定义的ApplicationContext实现类
在学习spring容器初始化的过程中,发现spring容器预留了一些扩展点,我们可以写子类来做功能扩展,今天就来探寻SpringBoot框架下的扩展方式
258 0
SpringBoot应用使用自定义的ApplicationContext实现类