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");
    }
}
相关推荐
大阳光男孩9 小时前
Spring Boot 整合 Debezium 实现 MySQL 增量数据监听(嵌入式版)
spring boot·后端·mysql
皮皮林5519 小时前
ThreadLocal 不香了?ScopedValue才是王道?
后端
X-⃢_⃢-X10 小时前
一、第一阶段:认识 Spring Boot
java·spring boot·后端
前端工作日常10 小时前
我学习到的Java程序的生命周期
java·后端
AskHarries12 小时前
GitHub 登录配置流程
后端
xuhaoyu_cpp_java14 小时前
SpringBoot学习(四)
java·经验分享·spring boot·笔记·学习
Sinclair14 小时前
安企CMS的安装-常见安装问题排查
运维·后端
Sinclair14 小时前
安企CMS的安装-安装引导与初始化
运维·后端
Sinclair14 小时前
安企CMS的安装-源码编译安装
运维·后端·go
Sinclair14 小时前
安企CMS的安装-同一服务器多站点部署
运维·后端