SpringBoot - 实现启动时执行指定任务(CommandLineRunner、ApplicationRunner)

有时一些特殊的任务需要在系统启动时执行,例如配置文件加载、数据库初始化等操作。Spring Boot 提供了两种解决方案:CommandLineRunner 和 ApplicationRunner。二者使用方式大体一致,差别主要体现在参数上。
一、使用 CommandLineRunner

1,基本介绍

Spring Boot 项目在启动时会遍历所有的 CommandLineRunner 的实现类并调用其中的 run 方法。

  • 如果整个系统中有多个 CommandLineRunner 的实现类,那么可以使用 @Order 注解对这些实现类的调用顺序进行排序(数字越小越先执行)。
  • run 方法的参数是系统启动是传入的参数,即入口类中 main 方法的参数(在调用 SpringApplication.run 方法时被传入 Spring Boot 项目中)

2,使用样例

(1)首先在项目中添加两个 CommandLineRunner 新建两个类,它们内容分别如下,就是把启动时传入的参数打印出来:

@Component
@Order(1)
public class MyCommandLineRunner1 implements CommandLineRunner {
    @Override
    publ
### CommandLineRunner 的作用与用法 #### 1. **CommandLineRunner 接口的定义** `CommandLineRunner` 是 Spring Boot 提供的一个接口,主要用于在应用程序启动完成后执行某些特定任务或代码块。它是一种非常灵活的机制,允许开发者在应用启动过程中插入自定义逻辑[^1]。 #### 2. **运行时机** `CommandLineRunner` 的 `run` 方法会在以下阶段被调用: - 当 `SpringApplication.run()` 被触发时,Spring Boot 开始启动应用程序。 - Spring 容器(通常是 ApplicationContext)完成初始化后,所有 Bean 已经被创建并完成了依赖注入。 - 此时,Spring Boot 会自动扫描所有实现了 `CommandLineRunner` 接口的 Bean,并按照一定顺序依次调用其 `run` 方法。 - 所有 `CommandLineRunner` 实现类的 `run` 方法执行完毕后,应用程序正式进入就绪状态[^2]。 #### 3. **基本用法** 要使用 `CommandLineRunner`,可以通过以下方式实现: ##### (1) 创建一个组件类并实现 `CommandLineRunner` 通过标注 `@Component` 将其实例化为 Spring 容器中的一个 Bean,同时重写 `run` 方法,在其中编写需要执行的业务逻辑。 ```java @Component public class MyInitService implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("项目启动成功后的初始化操作..."); } } ``` ##### (2) 多个实现类的执行顺序 如果存在多个 `CommandLineRunner` 实现类,则默认情况下它们会被随机执行。为了控制执行顺序,可以引入 `@Order` 注解或者实现 `Ordered` 接口来指定优先级。数值越小,优先级越高。 ```java @Component @Order(1) public class FirstTask implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("第一个任务执行"); } } @Component @Order(2) public class SecondTask implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("第二个任务执行"); } } ``` #### 4. **应用场景** `CommandLineRunner` 可广泛应用于以下场景: - 数据库初始化:例如加载初始数据到数据库中[^5]。 - 缓存预热:提前填充缓存以提高性能。 - 日志记录:打印启动日志或其他调试信息。 - 自动测试:在集成测试环境中模拟用户行为。 #### 5. **与其他接口的关系** 除了 `CommandLineRunner`,Spring Boot 还提供了另一个类似的接口——`ApplicationRunner`。两者的主要区别在于参数的不同以及适用场景的选择上[^4]。 | 特性 | CommandLineRunner | ApplicationRunner | |-------------------|------------------------------------------|----------------------------------------| | 参数类型 | String 数组 | ApplicationArguments 对象 | | 使用场景 | 更适合处理命令行传参 | 支持更复杂的参数解析 | 尽管如此,对于大多数简单的启动任务来说,`CommandLineRunner` 已经足够满足需求。 --- ### 示例代码 以下是完整的示例代码展示如何使用 `CommandLineRunner` 来执行启动任务: ```java package com.example.demo; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Component; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Component public static class StartupTask implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("Spring Boot 启动执行任务!"); } } } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

地表最强菜鸡

你的鼓励将是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值