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");
    }
}
相关推荐
雪宫街道23 分钟前
SpringBoot 向 IOC 容器注册组件的两种姿势:@Configuration 与 @Import
java·spring boot·后端·spring
techdashen27 分钟前
Cargo 1.94 开发周期全解析
开发语言·后端·rust
枕星而眠30 分钟前
Linux守护进程完全指南:从原理到实战
linux·运维·服务器·c++·后端
接着奏乐接着舞1 小时前
springboot mp mybatis plaus
windows·spring boot·mybatis
金融支付架构实战指南1 小时前
Milvus 向量检索服务 + SpringBoot 实战:电商商品语义检索与相似商品推荐
spring boot·后端·milvus·向量检索
齐 飞2 小时前
JDK21虚拟线程
java·后端
fox_lht2 小时前
15.4.循环和迭代器的性能比较
开发语言·后端·学习·rust
摇滚侠2 小时前
SpringMVC 入门到实战 HttpMessageConverter 65-74
java·后端·spring·intellij-idea
Csvn2 小时前
用户与权限管理 — 从创建到精细化管控
后端
金銀銅鐵2 小时前
用 Tkinter 实现简单的论语第一章阅读器
后端·python