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");
    }
}
相关推荐
何中应1 分钟前
关于查询方式的总结与讨论
后端·缓存·查询
计算机毕设指导62 分钟前
基于微信小程序的精致护肤购物系统【源码文末联系】
java·spring boot·微信小程序·小程序·tomcat·maven·intellij-idea
Victor3566 分钟前
Hibernate(36)Hibernate如何处理多对多关系?
后端
Victor3568 分钟前
Hibernate(35)什么是Hibernate的聚合函数?
后端
进阶的小名13 分钟前
[超轻量级消息队列(MQ)] Redis 不只是缓存:我用 Redis Stream 实现了一个 MQ(自定义注解方式)
数据库·spring boot·redis·缓存·消息队列·个人开发
何中应18 分钟前
@Autowrited和@Resource注解的区别及使用场景
java·开发语言·spring boot·后端·spring
源代码•宸19 分钟前
Golang语法进阶(Context)
开发语言·后端·算法·golang·context·withvalue·withcancel
christine-rr19 分钟前
linux常用命令(9)——查看系统与硬件信息
linux·运维·服务器·网络·后端
源代码•宸20 分钟前
Golang语法进阶(Sync、Select)
开发语言·经验分享·后端·算法·golang·select·pool
IT_陈寒22 分钟前
2024年JavaScript开发者必备的10个ES13新特性实战指南
前端·人工智能·后端