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");
    }
}
相关推荐
夕除几秒前
Spring Security 配置类(SecurityConfig)
java·后端·spring
lfwh2 分钟前
探针程序技术解析:基于 Spring Boot 非 Web 模式的云服务监控告警系统
前端·spring boot·后端
武子康8 分钟前
Java-22 深入浅出 MyBatis - 手写ORM框架3 手写SqlSession、Executor 工作原理
java·后端
ikoala18 分钟前
Codex 不得不装的 12 个插件,都在这了
前端·javascript·后端
摇滚侠32 分钟前
SpringMVC 入门到实战 简介和入门案例 01-13
java·后端·spring·intellij-idea
蝎子莱莱爱打怪33 分钟前
自用推荐|XTerminal:我心中 SSH 客户端的终极形态
java·后端·程序员
道友可好43 分钟前
用 Linter 驾驭 AI:机械化执行的艺术
前端·人工智能·后端
霸道流氓气质1 小时前
阿里云 OSS 从零到实战:概念、配置与 Spring Boot 集成指南
数据库·spring boot·阿里云
可乐ea1 小时前
【Spring Boot + MyBatis|第4篇】MyBatis 动态 SQL:if、where、foreach 使用详解
java·spring boot·后端·sql·mybatis
拾光师1 小时前
Java AIO 详解:异步非阻塞 IO 的实现与实践
后端