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");
    }
}
相关推荐
喵个咪6 分钟前
Go 语言 CMS 横评:风行 GoWind 对比传统 PHP/Java CMS 核心优势
前端·后端·cms
面向Google编程8 分钟前
从零学习Kafka:位移与高水位
大数据·后端·kafka
却话巴山夜雨时i9 分钟前
互联网大厂Java面试:从Spring到微服务的全栈挑战
java·spring boot·redis·微服务·面试·kafka·技术栈
Honmaple15 分钟前
驾驭AI的黄金缰绳:Harness Engineering引领2026工程范式变革
后端
Greedy Alg17 分钟前
定长内存池学习记录
c++·后端
黑牛儿22 分钟前
从0开始实现Mysql主从配置实战
服务器·数据库·后端·mysql
起个名特麻烦24 分钟前
SpringBoot全局配置LocalDate/LocalTime/LocalDateTime的序列化和反序列化
java·spring boot·后端
高斯林.神犇29 分钟前
四、依赖注入.spring
java·后端·spring
元宝骑士32 分钟前
SpringBoot 接口接收参数坑:uId 必须传 uid,userId 却正常?
后端
hero.fei33 分钟前
在springboot中使用Resilience4j
java·spring boot·后端