行业资讯 2025年08月6日
0 收藏 0 点赞 594 浏览 1777 个字
摘要 :

文章目录 1.什么是CommandLineRunner? 2.实现CommandLineRunner 3.@Bean CommandLineRunner 这篇文章讲述了什么是CommandLineRunner以及在Spring Boot中实现或使用它……




  • 1.什么是CommandLineRunner?
  • 2.实现CommandLineRunner
  • 3.@Bean CommandLineRunner

这篇文章讲述了什么是CommandLineRunner以及在Spring Boot中实现或使用它的不同方式。

注:本文基于Spring Boot 3.1.2版本进行测试。

1.什么是CommandLineRunner?

CommandLineRunner是Spring Boot中的一个接口。当一个类实现这个接口时,Spring Boot会在加载应用程序上下文后自动运行其run方法。通常,我们使用CommandLineRunner来执行启动任务,如用户或数据库初始化、播种或其他启动活动。

以下是Spring Boot中运行CommandLineRunner的简化流程:

  • 应用程序启动
  • Spring Boot初始化并配置bean、属性和应用程序上下文
  • CommandLineRunner(或ApplicationRunner)方法被执行
  • 应用程序现在可以处理连接或请求

2.实现CommandLineRunner

2.1 我们可以创建一个@Component bean,它实现了CommandLineRunner接口并重写了run()方法。Spring将在Spring应用程序启动期间运行此代码。

@Component
public class DatabaseInitializer implements CommandLineRunner {

  @Autowired
  BookRepository bookRepository;

  @Override
  public void run(String... args) {

      bookRepository.save(
              new Book(\"Book A\",
                      BigDecimal.valueOf(9.99),
                      LocalDate.of(1982, 8, 31))
      );

      System.out.println(\"Database initialized!\");
  }
}

2.2. 另一种常见的方法是让@SpringBootApplication类直接实现CommandLineRunner接口。

@SpringBootApplication
public class StartApplication2 implements CommandLineRunner {

  public static void main(String[] args) {
      SpringApplication.run(StartApplication2.class, args);
  }

  @Autowired
  BookRepository bookRepository;

  @Override
  public void run(String... args) {

      bookRepository.save(
              new Book(\"Book A\",
                      BigDecimal.valueOf(9.99),
                      LocalDate.of(1982, 8, 31))
      );

      System.out.println(\"Database initialized!\");
  }
}

3.@Bean CommandLineRunner

开发人员将CommandLineRunner注解为@Bean也是常见的做法,Spring也将在Spring应用程序启动期间运行此代码。

@SpringBootApplication
public class StartApplication {

  public static void main(String[] args) {
      SpringApplication.run(StartApplication.class, args);
  }

  @Autowired
  BookRepository bookRepository;

  @Bean
  public CommandLineRunner startup() {

      return args -> {
          bookRepository.save(
                  new Book(\"Book A\",
                          BigDecimal.valueOf(9.99),
                          LocalDate.of(1982, 8, 31))
          );
          System.out.println(\"Database initialized!\");

      };
  }
}

以上就是SpringBoot中如何使用 CommandLineRunner及其使用示例的全部内容。

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/9584.html

管理员

相关推荐
2025-08-06

文章目录 一、Reader 接口概述 1.1 什么是 Reader 接口? 1.2 Reader 与 InputStream 的区别 1.3 …

988
2025-08-06

文章目录 一、事件溯源 (一)核心概念 (二)Kafka与Golang的优势 (三)完整代码实现 二、命令…

465
2025-08-06

文章目录 一、证明GC期间执行native函数的线程仍在运行 二、native线程操作Java对象的影响及处理方…

348
2025-08-06

文章目录 一、事务基础概念 二、MyBatis事务管理机制 (一)JDBC原生事务管理(JdbcTransaction)…

456
2025-08-06

文章目录 一、SnowFlake算法核心原理 二、SnowFlake算法工作流程详解 三、SnowFlake算法的Java代码…

517
2025-08-06

文章目录 一、本地Jar包的加载操作 二、本地Class的加载方法 三、远程Jar包的加载方式 你知道Groo…

832
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号