SpringBoot中CommandLineRunner的使用

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

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


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

        // 执行一些代码 
        
    }
}

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

相关推荐
浮尘笔记1 小时前
Go语言临时对象池:sync.Pool的原理与使用
开发语言·后端·golang
梦梦代码精3 小时前
BuildingAI vs Dify vs 扣子:三大开源智能体平台架构风格对比
开发语言·前端·数据库·后端·架构·开源·推荐算法
REDcker3 小时前
RESTful API设计规范详解
服务器·后端·接口·api·restful·博客·后端开发
危险、4 小时前
一套提升 Spring Boot 项目的高并发、高可用能力的 Cursor 专用提示词
java·spring boot·提示词
sunnyday04264 小时前
基于Netty构建WebSocket服务器实战指南
服务器·spring boot·websocket·网络协议
没有bug.的程序员5 小时前
Java 序列化:Serializable vs. Protobuf 的性能与兼容性深度对比
java·开发语言·后端·反射·序列化·serializable·protobuf
我爱娃哈哈7 小时前
SpringBoot + Spring Security + RBAC:企业级权限模型设计与动态菜单渲染实战
spring boot·后端·spring
小王不爱笑1328 小时前
SpringBoot 配置文件
java·spring boot·后端
想用offer打牌8 小时前
Spring AI vs Spring AI Alibaba
java·人工智能·后端·spring·系统架构
guslegend9 小时前
第6章:SpringBoot 拦截器-监听器实战
spring boot