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");
    }
}
相关推荐
楚兴1 分钟前
DeepSeek Harness 到底在做什么?拆开 Agent 的运行时
人工智能·后端·架构
名字还没想好☜7 分钟前
Go 标准库 flag 包实战:参数解析、子命令、自定义 Value 类型与默认值
开发语言·后端·golang·go
米花米唐10 分钟前
Spring AI 2.0 MCP服务器搭建与使用
后端
程序猿DD26 分钟前
OctaFuse Gateway 2.9.0:用户限流控制、自定义转发头与管理后台升级
后端·api
三千星27 分钟前
Java开发者转型AI工程化Week 5:让RAG学会思考、看见与自检
后端
一个Ai的杂货铺29 分钟前
exit 0 也会撒谎:一个"绿灯盲跑"了一个月的定时任务,尸检报告
后端
北漂EDA码路31 分钟前
现代 C++20:move、forward 与完美转发,EDA 大型程序为什么离不开它
后端
泡海椒33 分钟前
JQuick-Java快速入门教程:轻量级规则引擎环境搭建与首个脚本运行实战
后端
stark张宇1 小时前
分布式事务最全图解(6种方案):从强一致的2PC到最终对账,彻底搞懂数据一致性
分布式·后端
IT毕设实战小研2 小时前
基于大数据的WTA职业网球赛事演变与竞技格局数据可视化分析
大数据·后端·爬虫·python·信息可视化·课程设计