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");
    }
}
相关推荐
WX-bisheyuange5 分钟前
基于SpringBoot的诊疗预约平台
java·spring boot·后端·毕业设计
SimonKing7 分钟前
基于Netty的WebSocket客户端
java·后端·程序员
我爱娃哈哈13 分钟前
SpringBoot + Canal + RabbitMQ:MySQL 数据变更实时同步到缓存与搜索系统
spring boot·rabbitmq·java-rabbitmq
麦兜*20 分钟前
Spring Boot整合MyBatis-Plus实战:简化CRUD操作的最佳实践
spring boot·tomcat·mybatis
麦兜*23 分钟前
Spring Boot 整合 Spring Data JPA 入门:只需注解,告别 SQL
spring boot·后端·sql
Sally璐璐25 分钟前
RESTful与RPC接口终极对比指南
后端·rpc·restful
benpaodeDD1 小时前
黑马SpringBoot3整合springMVC,mybatis
java·spring boot
岁岁种桃花儿1 小时前
Spring Boot核心注解详解:@ResponseBody深度解析与实战
spring boot·spring·mvc
Overt0p1 小时前
抽奖系统(7)
java·开发语言·spring boot·redis·tomcat·rabbitmq
s***87271 小时前
TCP/IP协议栈深度解析技术文章大纲
hive·spring boot