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");
    }
}
相关推荐
程序员爱钓鱼5 分钟前
Go语言WebP图像处理实战:golang.org/x/image/webp
后端·google·go
Nanjo_FanY23 分钟前
Spring Boot 3/4 可观测落地指南
后端
PFinal社区_南丞41 分钟前
Go语言开发AI智能体:从Function Calling到Agent框架
后端·go
货拉拉技术1 小时前
货拉拉海豚平台-大模型推理加速工程化实践
人工智能·后端·架构
神奇小汤圆1 小时前
请不要自己写,Spring Boot非常实用的内置功能
后端
神奇小汤圆1 小时前
突破Netty极限:基于Java 21 FFM API手写高性能网络通信框架
后端
Java编程爱好者2 小时前
给 Spring Boot 接口加了幂等保护:Token 机制 + 结果缓存,一个注解搞定
后端
Java编程爱好者2 小时前
字节二面:Redis 能做消息队列吗?怎么实现?
后端
爱找乐子的李寻欢2 小时前
防止字符串 ID 隐式转换导致的数据越权漏洞
后端
JavaGuide2 小时前
字节二面:Redis 能做消息队列吗?怎么实现?
redis·后端