Spring Boot:CommandLineRunner 接口


#Spring Boot#


在容器启动完成时,继承 CommandLineRunner 接口的类的 run 方法会被自动执行(前提是这个类被Spring 管理)。

入门

示例代码结构:

demo01
├── build.gradle
└── src
    ├── main
    │   ├── java
    │   │   └── demo
    │   │       ├── CustomCommandLineRunner.java
    │   │       └── Demo01Application.java
    │   └── resources
    └── test
        ├── java
        └── resources

build.gradle 内容:

buildscript {
    ext {
        springBootVersion = '2.1.3.RELEASE'
    }
    repositories {
        maven { url 'http://mirrors.cloud.tencent.com/nexus/repository/maven-public/' }
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    maven { url 'http://mirrors.cloud.tencent.com/nexus/repository/maven-public/' }
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

Demo01Application 内容:

package demo;

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

@SpringBootApplication
public class Demo01Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo01Application.class, args);
    }

}

CustomCommandLineRunner 内容:

package demo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class CustomCommandLineRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("This is CustomCommandLineRunner");
        for (int i=0; i<args.length; ++i) {
            System.out.printf("%d: %s\n", i, args[i]);
        }
    }
}

运行Demo01Application,会看到:

This is CustomCommandLineRunner

run 方法的args 是命令行参数,我们试下加上命令行参数运行。

命令行进入demo01目录:

$ gradle build
$ find  . -name "*.jar"
./build/libs/demo01-0.0.1-SNAPSHOT.jar

执行jar包:

$ java -jar ./build/libs/demo01-0.0.1-SNAPSHOT.jar hello 123
This is CustomCommandLineRunner
0: hello
1: 123

多个 CommandLineRunner

可以用Order注解,指定执行顺序。

示例代码结构:

demo02
├── build.gradle
└── src
    ├── main
    │   ├── java
    │   │   └── demo
    │   │       ├── CustomCommandLineRunner01.java
    │   │       ├── CustomCommandLineRunner02.java
    │   │       └── Demo02Application.java
    │   └── resources
    └── test
        ├── java
        └── resources

CustomCommandLineRunner01 内容:

package demo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(1) // 值越小,越先执行
public class CustomCommandLineRunner01 implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("This is CustomCommandLineRunner01");
        for (int i=0; i<args.length; ++i) {
            System.out.printf("%d: %s\n", i, args[i]);
        }
    }
}

CustomCommandLineRunner02 内容:

package demo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(0) // 值越小,越先执行
public class CustomCommandLineRunner02 implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("This is CustomCommandLineRunner02");
        for (int i=0; i<args.length; ++i) {
            System.out.printf("%d: %s\n", i, args[i]);
        }
    }
}

如果去掉Order注解,Demo02Application运行结果是:

This is CustomCommandLineRunner02
This is CustomCommandLineRunner01

不去掉Order注解,Demo02Application运行结果是:

This is CustomCommandLineRunner01
This is CustomCommandLineRunner02

Order 中的值越小,越先执行。

@SpringBootApplication 与 CommandLineRunner 一起用

示例代码结构:

demo03
├── build.gradle
└── src
    ├── main
    │   ├── java
    │   │   └── demo
    │   │       ├── Demo03Application.java
    │   │       └── SayHello.java
    │   └── resources
    └── test
        ├── java
        └── resources

SayHello 内容:

package demo;

import org.springframework.stereotype.Component;

@Component
public class SayHello {

    public String sayHello(String msg) {
        return "Hello " + msg;
    }

}

Demo03Application 内容:

package demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

@SpringBootApplication
@Component
public class Demo03Application implements CommandLineRunner {
    @Autowired
    private SayHello sayHello;

    public static void main(String[] args) {
        SpringApplication.run(Demo03Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("This is Demo03Application");
        System.out.println(sayHello.sayHello("World"));
    }

}

运行 Demo03Application,输出:

This is Demo03Application
Hello World

( 本文完 )