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");
    }
}
相关推荐
骇客野人几秒前
Springboot 的 配置文件 application.yml 和 bootrap.yml 的由来和作用
java·spring boot·后端
岁月宁静14 分钟前
三、《从零手撸 Agent》 · system prompt 与核心参数:调好你的旋钮
后端·python·agent
xcl092517 分钟前
幼儿托管系统开发实战:从需求分析到部署上线全指南
java·spring boot·需求分析
步行cgn20 分钟前
Spring Boot 中的 YAML 完全指南
android·spring boot·firefox
bcbnb31 分钟前
Flutter-Notebook代码混淆:Android与iOS平台安全配置
后端·ios
泡海椒42 分钟前
SpringBoot 集成 JQuick-Curl:Spring 环境完整接入教程,第三方接口调用别再到处手写模板类
后端
落木萧萧82543 分钟前
MyBatis 关联查询的四种写法,为什么最后都变回了手写 XML
java·数据库·后端
csdn2015_43 分钟前
java springboot项目,接收到的参数多个逗号
java·开发语言·spring boot
cui_hao_nan44 分钟前
Spring AOP 自调用、代理与 private 方法引发的诡异 NPE
java·后端·spring·ai开发
摇滚侠1 小时前
《SpringBoot 3:入门与应用实战》第 13 章 整合 MyBatis MyBatis 简单开发 阅读笔记 39
spring boot·笔记·mybatis