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");
    }
}
相关推荐
小兔子4 分钟前
Unix domain socket 的四个边界:抽象地址没有权限,SO_PEERCRED 是快照
后端
小兔子4 分钟前
no-cache 不是「不缓存」:RFC 9111 里几句常被读反的话
后端
Vecter5 分钟前
于 Spring Boot 和 LangChain4j 的 Web 智能助手
后端
张一凡939 分钟前
把“不要乱改”从提示词里拿出来,放进可校验的提案里
前端·后端·架构
一只爱撸猫的程序猿9 分钟前
当任何人都能生成一万行代码:什么变得稀缺
人工智能·后端·程序员
Potato_土豆14 分钟前
MCP Server 接入多个 AI 客户端:配置差异与 10 个真实踩坑
人工智能·后端·ai编程
低调滴开发16 分钟前
3 行代码写崩一个函数?切片才是 Rust 字符串处理的正确姿势
开发语言·后端·rust
代码丰20 分钟前
跨系统传输大 JSON:直接压缩,还是把数据放进 Redis?
redis·后端·json
1360967572337 分钟前
RAG 第一步就错了:切分粒度决定召回上限
后端
LEE1 小时前
别把 Agent 架构画成组织架构图——子代理该不该拆,用任务级 Eval 说话
前端·后端·ai编程