Spring Boot ApplicationRunner & CommandLineRunner

本文介绍 Spring Boot ApplicationRunner 和 CommandLineRunner 的使用场景及方法。


目录

  • ApplicationRunnerCommandLineRunner 简介
  • 开发环境
  • 基础示例

ApplicationRunnerCommandLineRunner 简介

ApplicationRunnerCommandLineRunner 的作用是在 SpringBootApplication 环境加载完成后并正式运行前执行指定任务。


开发环境

  • JDK 8

基础示例

  1. 创建 Spring Boot 工程,参考:IntelliJ IDEA 创建 Spring Boot 工程

  2. 创建三个配置类,在其构造函数中添加日志打印,通过日志可以看出配置加载过程顺序。其中一个实现 CommandLineRunner 接口,一个实现 ApplicationRunner 接口,并在 run 方法中也添加日志打印,可以通过日志看出该方法执行过程顺序。

package tutorial.spring.boot.config;

import org.springframework.context.annotation.Configuration;

@Configuration
public class XConfig {

    public XConfig() {
        System.out.println("XConfig constructed");
    }
}
package tutorial.spring.boot.config;

import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;

@Configuration
public class YConfig implements CommandLineRunner {

    public YConfig() {
        System.out.println("YConfig constructed");
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("CommandLineRunner run");
    }
}
package tutorial.spring.boot.config;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ZConfig implements ApplicationRunner {

    public ZConfig() {
        System.out.println("ZConfig constructed");
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("ApplicationRunner run");
    }
}
  1. 修改 main 方法所在主类,添加日志打印,作为应用运行前最后一步操作。
package tutorial.spring.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootRunnerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootRunnerApplication.class, args);
        System.out.println("Application started successfully!");
    }

}
  1. 运行应用
2020-07-18 17:41:33.549  INFO 15204 --- [           main] t.s.boot.SpringBootRunnerApplication     : Starting SpringBootRunnerApplication on ... (D:\Projects\Tutorial\spring-boot-tutorial\spring-boot-runner\target\classes started by ... in D:\Projects\Tutorial\spring-boot-tutorial)
2020-07-18 17:41:33.551  INFO 15204 --- [           main] t.s.boot.SpringBootRunnerApplication     : No active profile set, falling back to default profiles: default
XConfig constructed
YConfig constructed
ZConfig constructed
2020-07-18 17:41:33.825  INFO 15204 --- [           main] t.s.boot.SpringBootRunnerApplication     : Started SpringBootRunnerApplication in 0.468 seconds (JVM running for 0.926)
ApplicationRunner run
CommandLineRunner run
Application started successfully!

从日志中可以看出,ApplicationRunnerCommandLineRunnerrun 方法在 SpringBootApplication 启动成功后执行。

注意:ApplicationRunnerCommandLineRunnerrun 方法间的执行顺序可以通过 @Order 注解或 Ordered 接口控制。