Spring boot中CommandLineRunner的使用

简介: 日常开发中有可能需要实现项目启动后执行的功能,比如特殊数据处理,权限控制等。Spring boot提供了一种简单的实现方案。即CommandLineRunner

背景

日常开发中有可能需要实现项目启动后执行的功能,比如特殊数据处理,权限控制等。Spring boot提供了一种简单的实现方案。即CommandLineRunner


CommandLineRunner

/**
 * Interface used to indicate that a bean should <em>run</em> when it is contained within
 * a {@link SpringApplication}. Multiple {@link CommandLineRunner} beans can be defined
 * within the same application context and can be ordered using the {@link Ordered}
 * interface or {@link Order @Order} annotation.
 * <p>
 * If you need access to {@link ApplicationArguments} instead of the raw String array
 * consider using {@link ApplicationRunner}.
 *
 * @author Dave Syer
 * @since 1.0.0
 * @see ApplicationRunner
 */
@FunctionalInterface
public interface CommandLineRunner {

    /**
     * Callback used to run the bean.
     * @param args incoming main method arguments
     * @throws Exception on error
     */
    void run(String... args) throws Exception;

}

通过看源码,可以得知:

  • 这是一个接口,我们可以自定义实现类来实现该接口及实现run方法
  • 多个实现类可以被同时执行在同一个spring上下文中并且执行顺序是以order注解的参数顺序一致

案例说明

1.单个实现类

@Component
@Slf4j
public class InitService implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
   
        log.info("项目启动初始化...");
    }
}

在这里插入图片描述

2.多个实现类

多个实现类的时候,如何保证顺序呢?

SpringBoot在项目启动后会遍历所有实现CommandLineRunner的实体类并执行run方法,如果需要按照一定的顺序去执行,那么就需要在实体类上使用一个@Order注解(或者实现Order接口)来表明顺序

@Component
@Slf4j
@Order(value = 2)
public class InitService implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        log.info("项目启动初始化1...");
    }
}
@Component
@Slf4j
@Order(value = 1)
public class InitService2 implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
       log.info("项目启动初始化2....");
    }
}
@Component
@Slf4j
@Order(value = 3)
public class InitService3 implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
      log.info("项目启动初始化3...");
    }
}

在这里插入图片描述


目录
相关文章
|
3月前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
102 0
|
3月前
SpringBoot+Mybatis-Plus+PageHelper分页+多条件查询
SpringBoot+Mybatis-Plus+PageHelper分页+多条件查询
76 0
|
10月前
|
JSON Java 数据格式
三万字盘点Spring/Boot的那些常用扩展点(下)
Spring对于每个Java后端程序员来说肯定不陌生,日常开发和面试必备的。本文就来盘点Spring/SpringBoot常见的扩展点,同时也来看看常见的开源框架是如何基于这些扩展点跟Spring/SpringBoot整合的。 话不多说,直接进入正题。
|
3月前
|
XML Java 数据库连接
Spring Boot的数据访问之Spring Data JPA以及Hibernate的实战(超详细 附源码)
Spring Boot的数据访问之Spring Data JPA以及Hibernate的实战(超详细 附源码)
72 0
|
2月前
|
运维 Java 关系型数据库
Spring运维之boot项目bean属性的绑定读取与校验
Spring运维之boot项目bean属性的绑定读取与校验
36 2
|
2月前
|
存储 运维 Java
Spring运维之boot项目开发关键之日志操作以及用文件记录日志
Spring运维之boot项目开发关键之日志操作以及用文件记录日志
40 2
|
2月前
|
Java Maven
springboot项目打jar包后,如何部署到服务器
springboot项目打jar包后,如何部署到服务器
142 1
|
2月前
|
XML 运维 Java
Spring运维之boot项目打包jar和插件运行并且设置启动时临时属性和自定义配置文件
Spring运维之boot项目打包jar和插件运行并且设置启动时临时属性和自定义配置文件
33 1
|
2月前
springboot2.4.5使用pagehelper分页插件
springboot2.4.5使用pagehelper分页插件
43 0
|
2月前
|
缓存 运维 Java
Spring运维之boot项目多环境(yaml 多文件 proerties)及分组管理与开发控制
Spring运维之boot项目多环境(yaml 多文件 proerties)及分组管理与开发控制
27 0