• ApplicatioinRunner vs CommandLineRunner


    程序在启动完成的时候可能需要去处理某些业务,Spring Boot程序中可以通过去实现CommandLineRunner和ApplicationRunner接口还完成该操作。

    CommandLineRunner

    简单使用 【启动类直接实现CommandLineRunner

    在重新run方法未执行完成时,对外不访问而言程序不可以访问【spring 容器并未完成初始化】。

    一些简单记录无业务影响的操作应该使用异步操作。

    1. /**
    2. * Application类
    3. */
    4. @SpringBootApplication
    5. public class DemoApplication implements CommandLineRunner {
    6. @Resource
    7. private DemoService deomService;
    8. public static void main(String[] args) {
    9. System.out.println("容器开始创建");
    10. SpringApplication.run(DemoApplication .class, args);
    11. System.out.println("容器创建完成");
    12. }
    13. @Override
    14. public void run(String... args) throws Exception {
    15. System.out.println("容器创建完成?,先执行下列操作:");
    16. // TODO
    17. deomService.sayHello();
    18. }
    19. }

    多个业务顺序执行

    1. @Component
    2. @Order(1)
    3. public class UserRunner implements CommandLineRunner {
    4. public void run(String... args) throws Exception {
    5. System.out.println("UserRunner run");
    6. Thread.sleep(1000);
    7. }
    8. }
    9. @Component
    10. @Order(2)
    11. public class OrderRunner implements CommandLineRunner {
    12. public void run(String... args) throws Exception {
    13. System.out.println("OrderRunner run");
    14. Thread.sleep(1000);
    15. }
    16. }

    ApplicationRunner

    简单使用 【启动类直接实现CommandLineRunner

    1. /**
    2. * Application类
    3. */
    4. @SpringBootApplication
    5. public class DemoApplication implements ApplicationRunner {
    6. @Resource
    7. private DemoService deomService;
    8. public static void main(String[] args) {
    9. System.out.println("容器开始创建");
    10. SpringApplication.run(DemoApplication .class, args);
    11. System.out.println("容器创建完成");
    12. }
    13. @Override
    14. public void run(ApplicationArguments args) throws Exception {
    15. System.out.println("容器创建完成?,先执行下列操作:");
    16. // TODO
    17. deomService.sayHello();
    18. }
    19. }

    多个业务顺序执行

    1. @Component
    2. @Order(1)
    3. public class UserRunner implements ApplicationRunner {
    4. @Override
    5. public void run(ApplicationArguments args) throws Exception {
    6. System.out.println("UserRunner run");
    7. Thread.sleep(1000);
    8. }
    9. }
    10. @Component
    11. @Order(2)
    12. public class OrderRunner implements ApplicationRunner {
    13. @Override
    14. public void run(ApplicationArguments args) throws Exception {
    15. System.out.println("UserRunner run");
    16. Thread.sleep(1000);
    17. }
    18. }

    看起来是不是没啥区别哈?

    主要不同就是ApplicatioinRunner的run()方法的参数ApplicationArguments可以很方便地获取到命令行参数的值。

    浅见 如果出入希望大家留言

  • 相关阅读:
    IB化学介绍及备考
    去雨去雪去雾数据集构建
    python+pytest接口自动化(2)-HTTP协议基础
    Mysql 如何模糊匹配后匹配优化
    智能座舱架构与芯片- (15) 测试篇 下
    C#,数值计算——插值和外推,Base_interp的计算方法与源程序
    什么是 API ?
    图片链接打不开检测工具-免费链接失败检测软件
    LeetCode二叉树系列——144.二叉树的最小深度
    element plus tree树形复选框
  • 原文地址:https://blog.csdn.net/qq_34253002/article/details/126210202