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");
    }
}
相关推荐
吾疾唯君医4 小时前
Java SpringBoot集成积木报表实操记录
java·spring boot·spring·导出excel·积木报表·数据文件下载
IT_陈寒5 小时前
为什么你应该学习JavaScript?
前端·人工智能·后端
淇奥76 小时前
【MyBatis-Plus】MyBatis-Plus 学习笔记
后端
_code_bear_6 小时前
OpenSpec CLI 与 OPSX 工作流说明
前端·后端·架构
用户8356290780516 小时前
使用 Python 在 PowerPoint 中添加并控制音频播放
后端·python
用户8356290780516 小时前
使用 Python 在 PowerPoint 中生成并自定义饼图与环形图
后端·python
念何架构之路6 小时前
Go语言常见并发模式
开发语言·后端·golang
Cosolar6 小时前
大模型应用开发面试 • 第4期|A2A、复杂挑战与具身智能
人工智能·后端·面试
迷渡7 小时前
聊一聊 Bun 用 Rust 重写这件事
开发语言·后端·rust
王中阳Go7 小时前
秒杀、分库分表、全链路追踪:一个电商微服务的架构全拆解
后端·go