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");
    }
}
相关推荐
golang学习记18 分钟前
GitLens 十大神技:彻底改变你在 VS Code 中的 Git 工作流
前端·后端·visual studio code
一鹿高歌40 分钟前
🔥内存炸了!背刺我的竟然是Redisson!!
后端
lizhongxuan41 分钟前
AI 的底层思考
后端
Penge6661 小时前
解密 Kafka 与 RocketMQ 消费模型的核心之战
后端
Nyarlathotep01131 小时前
SpringBoot Starter的用法以及原理
java·spring boot
小码哥_常1 小时前
Spring Boot遇上Maven依赖冲突:打怪升级全攻略
后端
用户7344028193421 小时前
Spring Boot 集成 Redis 并调用 Lua 脚本详解
后端
小码哥_常1 小时前
Spring Boot多模块项目:Parent、BOM、Starter的分工大揭秘
后端
SimonKing2 小时前
GitHub 10万星的OpenCode,正在悄悄改变我们的工作流
java·后端·程序员
Moment2 小时前
OpenClaw 从能聊到能干差的是这 50 个 Skills 😍😍😍
前端·后端·开源