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");
    }
}
相关推荐
庞轩px1 小时前
第七篇:Spring扩展点——如何优雅地介入Bean的创建流程
java·后端·spring·bean·aware·扩展点
ltl1 小时前
Q/K/V 三件套:把 Bahdanau 抽象成一个公式
后端
千叶风行3 小时前
Text-to-SQL 技术设计与注意事项
前端·人工智能·后端
阿kun要赚马内4 小时前
后端数据操作组合:Pydantic与ORM
后端·python·orm·sqlalchemy
花米徐5 小时前
技术洞察精选 | 2026年4月28日 — 5月4日
后端·python·flask
阿维的博客日记5 小时前
Spring Cloud 为什么需要服务注册与发现中心这些东西?
后端·spring·spring cloud
笑而不语5 小时前
13|元数据过滤检索:UserContext 与按用户查知识
后端
用户095367515836 小时前
Go:浮点数如何进行比较?
后端·go
Zeus_6 小时前
如何更好的创建skill
后端
千云6 小时前
AI Coding 落地探索日志 · 初篇 · 启程记
后端·ai编程