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");
    }
}
相关推荐
spring2997923 分钟前
Spring Boot 实战篇(四):实现用户登录与注册功能
java·spring boot·后端
Rust语言中文社区39 分钟前
【Rust日报】Clone:像进程一样 fork 虚拟机的 Rust KVM VMM
开发语言·后端·rust
uzong39 分钟前
架构师的必修课:分布式系统发布理论设计要点
后端·架构
Dwzun1 小时前
基于Java+SpringBoot+Vue的校园二手物品置换系统设计与实现【附源码+文档+部署视频+讲解】
java·开发语言·spring boot
polaris06301 小时前
Spring Boot 项目开发流程全解析
java·spring boot·log4j
2601_949815331 小时前
Spring Boot中集成MyBatis操作数据库详细教程
数据库·spring boot·mybatis
程序员cxuan1 小时前
vibe coding 凉了,wish coding 来了
人工智能·后端·程序员
货拉拉技术1 小时前
一单多运履约平台的架构演进之路
后端
金融大 k1 小时前
Spring Boot WebSocket 实时行情推送实战:从断线重连到并发优化
spring boot·后端·websocket
编码浪子1 小时前
基于 Rust + Axum 的企业级权限管理系统设计与实现
开发语言·后端·rust