SpringBoot提供了两个接口ApplicationRunner和CommandLineRunner,在容器启动成功之后,SpringApplication的run()方法完成之前回调,可以用来初始化一些数据,加载定时任务等。
1 相同与不同
- 相同:两者功能一致,用法也基本一致;
- 不同:对参数的处理不同,CommandLineRunner接受原始的参数,不做处理;ApplicationRunner则对原始参数做了封装,可以接收key/value形式的参数
- ApplicationRunner的参数分为可选项参数(Optional Arguments)和非可选项参数(Non-Optional Arguments),前者指可key/value型参数,后者指不是key/value型参数。
2 Order
多个ApplicationRunner和CommandLineRunner可以指定执行顺序,在类里面加上@Order(1),数字小优先执行。
3 代码
package com.freedom.demo.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
/**
* @author tobebetter9527
* @description ApplicationRunnerTest1
* @create 2020/05/03 16:13
*/
@Slf4j
@Order(1)
@Component
public class ApplicationRunnerTest1 implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
log.info("EXECUTING : Run method of ApplicationRunnerTest1, Order 1");
final List<String> nonOptionArgs = args.getNonOptionArgs();
final String[] sourceArgs = args.getSourceArgs();
final Set<String> optionNames = args.getOptionNames();
nonOptionArgs.forEach(nonOption -> log.info("## Non Option Args : " + nonOption));
optionNames.forEach(option -> log.info("## Option Names : " + option));
Arrays.stream(sourceArgs).forEach(srcArgs -> log.info("## Source Args : " + srcArgs));
log.info("## Option Value of --optionalArg1 : " + args.getOptionValues("name"));
log.info("## Option Value of --optionalArg2 : " + args.getOptionValues("job"));
}
}
package com.freedom.demo.config;
import com.sun.media.jfxmedia.logging.Logger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.Arrays;
/**
* @author tobebetter9527
* @description CommandLineRunnerTest1
* @create 2020/05/03 17:19
*/
@Slf4j
@Order(2)
@Component
public class CommandLineRunnerTest1 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
log.info("EXECUTING : Run method of CommandLineRunnerTest1, Order 2");
Arrays.stream(args).forEach(arg -> log.info("## CommandLineRunnerTest1 String Args:" + arg));
}
}
package com.freedom.demo.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
/**
* @author tobebetter9527
* @description ApplicationRunnerTest2
* @create 2020/05/03 16:13
*/
@Slf4j
@Order(3)
@Component
public class ApplicationRunnerTest2 implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
log.info("EXECUTING : Run method of ApplicationRunnerTest2, Order 3");
}
}
4 idea设置
5 执行结果
17:43:57.613 [restartedMain] INFO c.f.d.c.ApplicationRunnerTest1 - EXECUTING : Run method of ApplicationRunnerTest1, Order 1
17:43:57.614 [restartedMain] INFO c.f.d.c.ApplicationRunnerTest1 - ## Non Option Args : www.github.com
17:43:57.614 [restartedMain] INFO c.f.d.c.ApplicationRunnerTest1 - ## Non Option Args : ILoveCode
17:43:57.614 [restartedMain] INFO c.f.d.c.ApplicationRunnerTest1 - ## Option Names : name
17:43:57.615 [restartedMain] INFO c.f.d.c.ApplicationRunnerTest1 - ## Option Names : job
17:43:57.615 [restartedMain] INFO c.f.d.c.ApplicationRunnerTest1 - ## Source Args : --name=tobebetter
17:43:57.615 [restartedMain] INFO c.f.d.c.ApplicationRunnerTest1 - ## Source Args : --job=coder
17:43:57.615 [restartedMain] INFO c.f.d.c.ApplicationRunnerTest1 - ## Source Args : www.github.com
17:43:57.615 [restartedMain] INFO c.f.d.c.ApplicationRunnerTest1 - ## Source Args : ILoveCode
17:43:57.615 [restartedMain] INFO c.f.d.c.ApplicationRunnerTest1 - ## Option Value of --optionalArg1 : [tobebetter]
17:43:57.615 [restartedMain] INFO c.f.d.c.ApplicationRunnerTest1 - ## Option Value of --optionalArg2 : [coder]
17:43:57.615 [restartedMain] INFO c.f.d.c.CommandLineRunnerTest1 - EXECUTING : Run method of CommandLineRunnerTest1, Order 2
17:43:57.615 [restartedMain] INFO c.f.d.c.CommandLineRunnerTest1 - ## CommandLineRunnerTest1 String Args:--name=tobebetter
17:43:57.615 [restartedMain] INFO c.f.d.c.CommandLineRunnerTest1 - ## CommandLineRunnerTest1 String Args:--job=coder
17:43:57.615 [restartedMain] INFO c.f.d.c.CommandLineRunnerTest1 - ## CommandLineRunnerTest1 String Args:www.github.com
17:43:57.615 [restartedMain] INFO c.f.d.c.CommandLineRunnerTest1 - ## CommandLineRunnerTest1 String Args:ILoveCode
17:43:57.615 [restartedMain] INFO c.f.d.c.ApplicationRunnerTest2 - EXECUTING : Run method of ApplicationRunnerTest2, Order 3