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");
    }
}
相关推荐
码途行者14 分钟前
每天10分钟学Go:第17课 接口interface-只规定“能做什么“,不管它是什么
开发语言·后端·golang
geovindu3 小时前
python:DecisionTree
开发语言·后端·python·算法
Cosolar3 小时前
从 Java 21 升到 25、Spring Boot 3 升 4:一次可复现的迁移踩坑实录
java·后端·面试
geovindu4 小时前
python: Decision Tree Regressor
开发语言·后端·python·算法·决策树
码事漫谈4 小时前
黄仁勋的来时路
后端
bai2735 小时前
path.join('C:\a', '\b') 给我 C:\a\b,path.resolve 给 C:\b:Windows 上 51 行路径实测
javascript·后端
hackhase5 小时前
Django 多表数据整合:7 张异构表如何收敛成 4 类页面
后端·mysql
Cosolar7 小时前
需求做完了,经验归零了:Agent 真正缺的是第二条闭环
人工智能·后端·agent
CRZZX8 小时前
Agent 上下文管理与可观测性设计
后端·agent
yAdrian8 小时前
将 milkv-duo-s 开发板变成 AP
后端·嵌入式