SpringBoot项目启动时的初始化操作

SpringBoot项目启动时的初始化操作包括:

类的静态代码、类的构造函数、@Autowired装载变量、@PostConstruct修饰方法、实现ApplicationRunner接口、实现CommandLineRunner接口,其执行的先后次序为:

static > constructer > @Autowired > @PostConstruct > ApplicationRunner > CommandLineRunner

这里主要介绍实现ApplicationRunner接口、CommandLineRunner接口的方式进行初始化操作

实现 ApplicationRunner 接口

java 复制代码
@Component
@Order(1) /// 多个类实现该接口的优先级
public class MyApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("order1:MyApplicationRunner");
 
    }
}

实现 CommandLineRunner 接口

java 复制代码
@Component
@Order(2)  /// 多个类实现该接口的优先级
public class MyCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... strings) throws Exception {
        System.out.println("order2:MyCommandLineRunner");
    }
}
相关推荐
用户8356290780513 小时前
无需 Office:Python 批量转换 PPT 为图片
后端·python
啊哈灵机一动3 小时前
使用golang搭建一个nes 模拟器
后端
间彧4 小时前
SpringBoot + ShardingSphere 读写分离实战指南
后端
砍材农夫4 小时前
订单超时
后端
树獭叔叔5 小时前
06-大模型如何"学习":从梯度下降到AdamW优化器
后端·aigc·openai
用户8307196840825 小时前
Spring Boot 项目中日期处理的最佳实践
java·spring boot
得鹿5 小时前
MySQL基础架构与存储引擎、索引、事务、锁、日志
后端
程序员飞哥5 小时前
Block科技公司裁员四千人,竟然是因为 AI ?
人工智能·后端·程序员
JavaEdge在掘金5 小时前
Claude Code 直连 Ollama / LM Studio:本地、云端开源模型都能跑
后端
LSTM975 小时前
使用 Python 将 TXT 转换为 PDF (自动分页)
后端