SpringBoot中CommandLineRunner的使用

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

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


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

        // 执行一些代码 
        
    }
}

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

相关推荐
superman超哥1 小时前
Rust String与&str的内部实现差异:所有权与借用的典型案例
开发语言·后端·rust·rust string·string与str·内部实现·所有权与借用
愈努力俞幸运1 小时前
rust安装
开发语言·后端·rust
踏浪无痕1 小时前
JobFlow 负载感知调度:把任务分给最闲的机器
后端·架构·开源
UrbanJazzerati1 小时前
Python自动化统计工具实战:Python批量分析Salesforce DML操作与错误处理
后端·面试
我爱娃哈哈2 小时前
SpringBoot + Seata + Nacos:分布式事务落地实战,订单-库存一致性全解析
spring boot·分布式·后端
nil2 小时前
记录protoc生成代码将optional改成omitepty问题
后端·go·protobuf
麦兜*2 小时前
【springboot】图文详解Spring Boot自动配置原理:为什么@SpringBootApplication是核心?
android·java·spring boot·spring·spring cloud·tomcat
superman超哥2 小时前
Rust 范围模式(Range Patterns):边界检查的优雅表达
开发语言·后端·rust·编程语言·rust范围模式·range patterns·边界检查
yangminlei3 小时前
Spring Boot——日志介绍和配置
java·spring boot
云上凯歌3 小时前
02 Spring Boot企业级配置详解
android·spring boot·后端