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");
    }
}
相关推荐
步行cgn9 分钟前
MyBatis 查询结果字段为 null?一文讲透下划线命名与驼峰映射问题
后端
技术长镜头9 分钟前
别再只会“加索引”:从磁盘页到 B+Tree,彻底理解 MySQL 索引的设计与运行
后端·mysql
生锈的键盘11 分钟前
gRPC 多路复用全链路拆解:从客户端到服务端,中间隔着 ELB 和 Nginx 到底是怎么玩的?
后端
laity1715 分钟前
python发光表白爱心(从零到一实现)
前端·后端
用户9210802628620 分钟前
2. Java 基础该怎么学,先抓业务开发真正用得上的部分
后端
AI老猿博士22 分钟前
SpringBoot自动配置揭秘
后端
程序员爱钓鱼24 分钟前
Go for 循环详解
后端·面试·go
程序员爱钓鱼24 分钟前
Rust impl详解:为Struct定义方法与关联函数
前端·后端·rust
weixin_4316004433 分钟前
NestJS 入门(9):连上数据库,SQL 写在哪?
数据库·后端·sql·学习·nest.js
qq_22589174661 小时前
基于Flask的城市地铁客流量数据预测系统设计与实现
后端·python·flask