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");
    }
}
相关推荐
苏三说技术几秒前
Kafka已正式接入AI
后端
企业数字化笔记1 分钟前
AI写的系统出现504怎么办?接口超时和数据库慢查询排查
数据库·后端
IT_陈寒7 分钟前
Redis的Set操作居然能把我的服务整挂了?
前端·人工智能·后端
momo0611728 分钟前
Redis新手入门 -- 学习笔记
redis·后端
井云AI34 分钟前
vendor 资源包是什么:井云 DSH 客户端打包为什么离不开它
后端·智能体小程序
重生之我是Java开发战士1 小时前
【Spring Cloud】Nacos注册中心
后端·spring·spring cloud
程序员20071 小时前
AI 编程工具链碎片化?基于多端兼容的统一 Agent Rules 架构实践
后端
程序员20071 小时前
拒绝 AI 代码“暗度陈仓”:工程级 AI 编码边界与防护规则设计
后端
yunwei371 小时前
eBPF 教程:追踪 CUDA GPU 操作
linux·后端·性能优化
搬搬砖得了2 小时前
从“我写的”到“我懂它”——论工程师对代码的心理模型
后端