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 分钟前
MySQL进阶教程(二)—— 储存过程与触发器(下)
开发语言·数据库·后端·sql·mysql·oracle·数据库开发
IT_陈寒5 分钟前
为什么你应该学习Java?
前端·人工智能·后端
AINative软件工程8 分钟前
LLM 应用的 Read-Your-Writes 一致性工程实践:写完就读,模型却返回旧数据
后端
FfHUCisI23 分钟前
无锁 Reactor 网络模型与 gnet / evio 架构拆解
服务器·开发语言·后端·golang
高频因子挖掘机28 分钟前
从第一行 Python 代码开始,个人量化项目应该先接入历史行情还是实时行情?
后端·github·api
高频因子挖掘机29 分钟前
个人开发者如何高效处理 A 股日 K 线数据?基于 QuantDash 的清洗思路与复权实战
后端·github·api
yunwei3742 分钟前
eBPF 实例教程:跟踪 Intel NPU 内核驱动操作
linux·后端·性能优化
高频因子挖掘机2 小时前
股票池代码为什么总有问题?从常见误区到可解释的校验报告
后端·github·api
考虑考虑9 小时前
nginx路由重写
运维·后端·自动化运维
JaguarJack10 小时前
PHP 紧急修复两项严重 RCE 漏洞,8.2 至 8.5 用户应立即升级
后端·php·服务端