CommandLineRunner、ApplicationRunner 接口是在容器启动成功后的最后一步回调(类似开机自启动)。

二者的官方javadoc一样,区别在于接收的参数不一样。

1、CommandLineRunner的参数是最原始的参数,没有做任何处理。

2、ApplicationRunner的参数是ApplicationArguments,是对原始参数做了进一步的封装。

3、ApplicationArguments是对参数(main方法)做了进一步的处理,可以解析--name=value的,我们就可以通过name来获取value(而CommandLineRunner只是获取--name=value)

CommandLineRunner可接收参数

SpringBoot 学习系列(四) - 项目启动时候初始化资源

ApplicationRunner可接收参数

SpringBoot 学习系列(四) - 项目启动时候初始化资源

实现如下:

SpringBoot 学习系列(四) - 项目启动时候初始化资源 

运行结果

SpringBoot 学习系列(四) - 项目启动时候初始化资源

 具体实例

1、启动类

SpringBoot 学习系列(四) - 项目启动时候初始化资源

2、初始化执行类一

SpringBoot 学习系列(四) - 项目启动时候初始化资源 

3、初始化执行类二

SpringBoot 学习系列(四) - 项目启动时候初始化资源

4、运行结果

SpringBoot 学习系列(四) - 项目启动时候初始化资源 

相关文章:

  • 2021-04-17
  • 2022-12-23
  • 2021-02-23
  • 2021-04-26
  • 2022-01-12
  • 2021-11-14
  • 2021-04-13
  • 2021-11-12
猜你喜欢
  • 2022-12-23
  • 2021-11-02
  • 2022-12-23
  • 2020-06-24
  • 2022-12-23
  • 2021-11-02
相关资源
相似解决方案