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");
    }
}
相关推荐
Soofjan2 分钟前
其它(5):Bleve 全文检索
后端
Gopher_HBo2 分钟前
Go语言学习笔记(七)并发
后端
智碳未来科技有限公司4 分钟前
工业双碳实践:基于 SpringBoot + 若依的智碳能源管理系统(zhitan-ems)源码深度解析与落地实战
spring boot·后端·能源
biubiubiu07067 分钟前
SpringBoot生产级日志配置
java·spring boot·后端
碎碎念_4928 分钟前
SpringBoot和MyBatis框架·速通版
spring boot·后端·mybatis
秋天的一阵风26 分钟前
AGENTS.md:你的AI代码助手,需要一份"项目说明书"
前端·后端·ai编程
jserTang34 分钟前
手撕 Claude Code-7:自动压缩与记忆恢复
前端·后端
嘟嘟071736 分钟前
从 TypeScript 到 Bun:一份前端开发者的效率进阶笔记
后端
用户75088370619537 分钟前
循环依赖加 @Lazy 后异常漂移?Spring 三级缓存为什么没兜住?
后端