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");
    }
}
相关推荐
IT_陈寒21 分钟前
为什么你应该学习React?
前端·人工智能·后端
RuoyiOffice24 分钟前
SpringBoot+Vue3 单据校验设计:保存草稿、提交审批、退回重填,各该检查什么?
spring boot·vue3·flowable·表单设计·ruoyi office·草稿保存·单据校验
每天一道题29 分钟前
Kafka 为什么会重复消费?从 Producer ACK、Offset 到消息可靠性
后端·微服务
木易 士心1 小时前
Spring Boot 微信公众号后端服务开发实战指南
spring boot·后端·微信
BingoGo1 小时前
别再只用数组了,这 10 种 PHP 数据结构值得了解
后端·php
ss2731 小时前
基于springboot+vue+mysql「校园二手教材循环交易」小程序,三端全跑通
vue.js·spring boot·mysql
JaguarJack1 小时前
别再只用数组了,这 10 种 PHP 数据结构值得了解
后端·php·服务端
程序员海军10 小时前
用了两年多 GPT,我最近开始换模型了,还折腾了个求职避坑 Agent
前端·人工智能·后端
码士集团小青10 小时前
SpringBoot 集成缓存性能之王 Caffeine
spring boot·后端·缓存
excel12 小时前
IndexNow:让搜索引擎更快发现网站更新的主动通知协议
前端·后端·架构