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");
    }
}
相关推荐
Flittly10 分钟前
【AgentScope Java新手村系列】(10)实战-多Agent天气助手
java·spring boot·spring
张不才17 分钟前
一个静默吞数据的时间戳陷阱
后端
李少兄17 分钟前
从原理到实战:Spring IoC/DI 核心知识体系与高频面试题全解
java·后端·spring
ServBay21 分钟前
ServBay 1.30.0 更新:双平台引入 MCP 服务,AI 编程助手成为全栈本地运维
后端·ai编程
张不才42 分钟前
分页查出来的数据总少几条?可能是 MyBatis 后置过滤的坑
后端
Windeal42 分钟前
Agent ToolCall 循环怎么定制?PI Extension 与 DeepAgents Middleware 两条岔路深度对比
后端·openai
鱼人44 分钟前
targets 包实战:R 语言数据分析流水线自动化管理方案
后端
时雨__1 小时前
一文搞懂 Python 并发:GIL、多线程/多进程/协程怎么选
后端
Anson4321 小时前
Dubbo架构深度分析
后端
站大爷IP1 小时前
global和nonlocal到底有什么区别?
后端