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");
    }
}
相关推荐
谢亮_vipxieliang31 分钟前
Spring Boot 自动配置原理:从 @SpringBootApplication 到自定义 Starter
java·spring boot·后端
我的div丢了肿么办1 小时前
go语言中的空接口和类型断言
后端·go
浪浪山_大橙子1 小时前
公司里的 AI,终于不只会聊天:我用 GPT‑6 把企业工作伙伴开源了
前端·后端·面试
乌暮2 小时前
JVM 原理与实践:从运行时数据区到线上故障排查
java·开发语言·jvm·后端·学习·面试
网腾无限2 小时前
优尼沃OS工程手记:从单体架构到主权智能体集群的改造总结
后端
code2cat2 小时前
【随笔】MCP缓存期限与共享范围:让Agent复用资料时记住边界
java·后端·缓存·ai agent·mcp
llqbzllll2 小时前
线程池里的“幽灵数据”:ThreadLocal 用完不 remove,为什么下个请求还能看到?
后端
量化分析码农2 小时前
【Python量化系统工程实战 #02】每天手动拉数据太烦?用 APScheduler 搭一条「自动采集 + 增量去重」的流水线
后端
YYYing.3 小时前
【设计模式系列 (九) 】装饰器模式
c++·后端·设计模式·装饰器模式·c/c++
蜗牛互联网3 小时前
Gemini 4 Argon的1M输出窗口与长程Agent工程边界
java·人工智能·后端