SpringBoot中的CommandLineRunner接口

本文介绍SpringBoot中CommandLineRunner和ApplicationRunner接口的使用,通过@Component注解、在SpringBoot入口实现或声明Bean的方式,实现应用启动后执行特定任务。文章探讨了如何预先校验数据、加载数据或执行任务,并讲解了多个实现类的执行顺序控制。

前言

在项目完全启动前,可能需要初始化一些东西、加载数据或者执行一些任务等;SpringBoot提供了CommandLineRunner和ApplicationRunner接口,在容器启动成功后的最后进行回调,遍历所有实现了这两个接口的类加载到Spring 容器中,然后执行接口实现类中的run方法。可以用来预先校验数据,加载数据或者执行任务等等。

实现

使用CommandLineRunner,需要将其注入到spring容器中,所以可以通过@Component、在SpringBoot入口中实现该接口或者声明一个实现接口的bean,然后在启动时能够注入到spring容器中即可。

1.使用@Component注解

@Component
public class ExampleOne implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        // 可以写自己的业务代码 
       System.out.println("使用@component注解的方式");
    }
}

注: 通过使用@Component注解,通过启动springBoot就可以扫描到这个接口实现类,无需再springBoot入口中在进行其他配置。

  1. 在SpringBoot入口中实现该接口
@SpringBootApplication
public class BootApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(BootApplication.class, args);
    }
    @Override
    public void run(String... args) throws Exception {
        System.out.println("在入口配置");
    }
}
  1. 声明一个Bean
public class ExampleThree implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("通过Bean方法注入,可以实现自己 想要做的");
    }
}

在入口中配置(其他位置也行,只要是能注入到Spring 容器中即可)

@SpringBootApplication
public class BootApplication  {
    public static void main(String[] args) {
        SpringApplication.run(BootApplication.class, args);
    }
    @Bean
    public ExampleThree test(){
        return new ExampleThree();
    }
}

实现接口后,如果run方法中的代码抛出异常的话会导致启动失败!核心地方try catch 一下。

多个实现类的执行顺序

当有多个类是实现了CommandLineRunner接口时,可以通过@Order注解指定他们的执行顺序。@Order(value = 1) value 的值越小,越先执行。

最后

实现类在应用成功启动后,去执行相关代码逻辑,且只会执行一次;
我们可以在run()方法里使用任何依赖,因为它们已经初始化好了;

关于ApplicationRunner接口,与CommandLineRunner接口大致相同,如果你需要访问ApplicationArguments去替换掉字符串数组,可以考虑使用ApplicationRunner类。
举例可查看博客地址:

https://www.jianshu.com/p/5d4ffe267596

遇到了,只是为了做个总结!step by step!

Spring Boot 中,`CommandLineRunner` 是一种方便的方式来在应用程序启动时运行一些初始化任务,比如数据迁移、日志配置等。如果你想在 `run` 方法执行完后注入依赖的 Bean,通常不会直接在 `run` 方法内部注入,因为这会违背单例模式,可能导致并发问题。 然而,你可以通过以下几种方式间接实现: 1. **使用 @PostConstruct 注解**: 如果你想确保某个Bean在 `CommandLineRunner` 完成后立即执行,可以在该Bean上添加 `@PostConstruct` 注解,Spring会在所有 `CommandLineRunner` 执行完毕后再调用这个方法。 ```java @Component public class MyComponent { @Autowired private SomeDependency dependency; // 使用 @PostConstruct 注解的方法 @PostConstruct public void init() { // 这里可以使用已经注入的 dependency } // CommandLineRunner 的 run 方法 @Override public void run(String... args) throws Exception { // ... } } ``` 2. **使用 ApplicationRunner** 或者 **ApplicationArgumentsRunner** 替代 CommandLineRunner: 这些接口允许你在应用启动过程中执行更细粒度的任务,并且它们允许在每个命令行参数处理后或整个参数处理完成后注册回调方法。 ```java @Component public class MyApplicationRunner implements ApplicationRunner { @Autowired private SomeDependency dependency; @Override public void run(ApplicationArguments args) { // 在这里,args.getApplicationArguments() 可以获取参数 // 然后在合适的地方注入 dependency // ... } } ``` 3. **Spring Boot Actuator**: 如果是用于服务启动后的配置,可以考虑使用 Actuator 提供的工具如 HealthIndicator 或 MetricsEndpoint,它们会在启动后提供注册点。 重要的是,确保你的 Bean 配置为 Spring 容器管理,并且在整个生命周期内可用,而不是仅在特定的执行阶段。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

喵喵@香菜

感谢观众老爷送的一发火箭!

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

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

打赏作者

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

抵扣说明:

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

余额充值