SpringBoot中CommandLineRunner的使用

开发中,你有没有遇到这样的场景,项目启动后,立即需要进行一些操作。比如:加载一些初始化数据、执行一段逻辑代码。你可以使用SpringBoot中CommandLineRunner。它可以在项目启动后,执行CommandLineRunner接口实现类的相关逻辑。具体使用如下:

复制代码
@Order(1)
@Component
public class Example  implements CommandLineRunner {


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

        // 执行一些代码 
        
    }
}

特别要注意:多个CommandLineRunner的实现类是串行方式执行。如何在其中某一个实现类中使用死循环逻辑,将导致后续的实现类不能执行。解决该问题的方式为开启一个线程,异步去执行。

相关推荐
IT_陈寒3 分钟前
Vite 5个隐藏功能大揭秘:90%的开发者都不知道这些提速技巧!
前端·人工智能·后端
m***l1157 分钟前
【Spring Boot】Spring AOP中的环绕通知
spring boot·后端·spring
v***5659 分钟前
常见的 Spring 项目目录结构
java·后端·spring
b***666117 分钟前
Spring Framework 中文官方文档
java·后端·spring
8***235522 分钟前
SQL Server2022版+SSMS安装教程(保姆级)
后端·python·flask
n***44323 分钟前
SpringBoot Maven快速上手
spring boot·后端·maven
b***666126 分钟前
【慕伏白教程】Zerotier 连接与简单配置
android·前端·后端
a***113529 分钟前
springcloud springboot nacos版本对应
spring boot·spring·spring cloud
聆风吟º34 分钟前
【Spring Boot 报错已解决】Error creating bean with entityManagerFactory 原因分析与解决方案
java·spring boot·后端
o***741735 分钟前
SpringBoot【十一】mybatis-plus实现多数据源配置,开箱即用!
spring boot·后端·mybatis