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");
    }
}
相关推荐
高频因子挖掘机7 分钟前
量化项目的数据架构怎么设计?从第一份行情到可扩展的 Python 研究环境
后端·github·api
考虑考虑8 小时前
JDK26中的Map.ofLazy
java·后端·java ee
云浪9 小时前
Go 并发实战:从零实现一个高并发安全的任务调度器
javascript·后端·go
苏三的开发日记9 小时前
Docker 启动顺序优化(CentOS / Ubuntu)
后端
高频因子挖掘机11 小时前
同一交易日出现重复 K 线:去重前先确认时间、来源和粒度
后端·github·api
feely020812 小时前
《我写了 31 个静态扫描插件,踩了 7 个「假绿」》
后端
橙子家13 小时前
参数校验类异常(Argument Exception)【C# Exception 详解系列一】
后端
掘金挖土13 小时前
前端手摸手跑路之 AI 应用开发(九)
前端·后端
全栈弄潮儿14 小时前
Python实战第8期:函数
后端·python·编程语言