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");
    }
}
相关推荐
超级无敌攻城狮12 分钟前
Agent 到底是怎么跑起来的
前端·后端·架构
Devin~Y12 分钟前
从Spring Boot到Spring AI:音视频AIGC内容社区Java大厂面试三轮连环问(含Kafka/Redis/安全/可观测性答案)
java·spring boot·redis·spring cloud·kafka·spring security·resilience4j
二妹的三爷20 分钟前
私有化部署DeepSeek并SpringBoot集成使用(附UI界面使用教程-支持语音、图片)
spring boot·后端·ui
神奇小汤圆22 分钟前
程序员面试必备的Java八股文,适合所有的Java求职者
后端
覆东流23 分钟前
第3天:Python print深入与格式化输出
开发语言·后端·python
StockTV32 分钟前
SpringBoot对接黄金白银期货数据API
java·spring boot·后端
techdashen1 小时前
Rust 正式成立 Types Team:类型系统终于有了专属团队
开发语言·后端·rust
小谢小哥1 小时前
43-Kafka 核心原理与实战
后端·架构
金銀銅鐵1 小时前
[git] 如何找到已经“丢失”的 commit?
git·后端
消失的旧时光-19431 小时前
Spring Boot 核心机制之 @Conditional:从原理到实战(一次讲透)
java·spring boot·后端