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 小时前
C# 集合类型全览:从 List<T> 到 FrozenDictionary,配一组实测数据
后端
aleimu1 小时前
Windows 11 右键打开 Cmder 2.0:从找不到菜单到默认使用 Cmder
前端·人工智能·后端
货拉拉技术1 小时前
AI Agent 如何进入实时交易系统:货拉拉的三个实践与一套工程方法论
后端·架构·agent
我是谁的程序员2 小时前
iOS 性能分析在 Windows 上怎么做,用 KXBuild 的 kxdevice 替代 Instruments
后端·ios
华为云开发者联盟2 小时前
TaurusDB 慢 SQL 智能诊断:用大模型辅助调优
后端
ServBay2 小时前
AI编程越来越强,为什么软件漏洞却是一个没少?MySQL又爆高危漏洞了
后端·mysql
季风11322 小时前
10.对象关系行为
后端
tann2 小时前
Environment类
后端
鲸鹏芯海微电子科技上海有限公司2 小时前
手机直连卫星,射频前端到底要解决什么?
前端·后端
开心就好20252 小时前
通配符 Bundle ID 和精确 Bundle ID 怎么选,推送、iCloud 能不能用通配符
后端·ios