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");
    }
}
相关推荐
Lyra_Infra28 分钟前
故障排查报告:Spring boot连接 Nacos 异常及启动失败
java·spring boot
星栈独行32 分钟前
Node 框架怎么选?Express、Koa、Egg、NestJS 场景化选型指南
后端·程序人生·node.js
你为她披上外套时我正站在窗外43 分钟前
拆解 siwi-download:Rust 异步下载器是怎么炼成的
后端
苍何1 小时前
WAIC深度体验:能跨端使用的 Agent 才是好 Agent!
后端
程序员清风1 小时前
推荐几个我常听的AI播客!
java·后端·面试
JavaGuide2 小时前
Kimi K3 实战:全栈项目、Java 项目改造与 3A 游戏 Demo
后端·ai编程
wuqingshun3141592 小时前
如何理解Spring Boot中的starter?
java·spring boot·后端
AskHarries2 小时前
PayPal 接入避坑
后端
谭光志2 小时前
深入浅出 RAG:用一个可运行的 Demo 讲透完整链路
前端·后端·ai编程
wuqingshun3141592 小时前
SpringBoot是如何实现自动配置的
java·spring boot·后端