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");
    }
}
相关推荐
用户3952409988035 分钟前
排坑日记:ASP.NET Core 中 "Required field is not provided" 验证错误全记录
后端
用户35218024547536 分钟前
当 Prompt 学会"热更新":Spring Boot × Nacos3 AI 实战
java·spring boot·ai编程
用户8356290780511 小时前
使用 Python 自动化 PowerPoint 形状布局与格式设置
后端·python
Oneslide2 小时前
sudo免密权限配置不生效
后端
站大爷IP2 小时前
为什么Python不用var或let声明变量?
后端
赴星半途2 小时前
NestJS实战-创建AuthService
后端
北冥有鱼2 小时前
mqtt 测试
前端·后端
代码丰2 小时前
使用 TtlExecutors 解决线程池中的 ThreadLocal 上下文丢失问题
后端
阿祖zu3 小时前
别再优化 RAG 了,适配 Agent 的 LLM Wiki 知识库理念
前端·后端·aigc