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");
    }
}
相关推荐
ekuoleung几秒前
量化平台中的 DSL 设计与实现:从规则树到可执行策略
前端·后端
小研说技术6 分钟前
实时通信对比,一场MCP协议的技术革命
前端·后端·面试
ServBay10 分钟前
2026年 Go 开发中没有它就不行的 10 个库
后端·go
SamDeepThinking1 小时前
别让一个超时的第三方http接口拖垮所有接口
java·后端·架构
我母鸡啊1 小时前
软考架构师故事系列-操作系统
后端
阿聪谈架构1 小时前
第08章:MCP 模型上下文协议(上)
人工智能·后端
雨白1 小时前
使用 Kotlin 与 Spring Boot 从零搭建 Web 应用
spring boot·kotlin
mrchan1 小时前
markdown 画图总结
后端
开心就好20251 小时前
全面介绍iOS开发工具:Xcode、AppCode、CocoaPods、Fastlane和Git
后端·ios
一 乐1 小时前
交通感知与车路协同系统|基于springboot + vue交通感知与车路协同系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·交通感知与车路协同系统