CommandLineRunner和ApplicationRunner作用及区别——SpringBoot

一、需求与前言
springBoot框架项目,有时候有预加载数据需求——提前加载到缓存中或类的属性中,并且希望执行操作的时间是在容器启动末尾时间执行操作。针对这种场景,SpringBoot提供了两个接口,分别是CommandLineRunner和ApplicationRunner。两个接口都在spring-boot的jar包中(spring-boot的jar包依附关系:spring-boot<-spring-boot-starter<-spring-boot-starter-web),项目只需要依赖spring-boot-starter-web的jar便可使用。

二、接口的使用
2.1、ApplicationRunner接口使用
2.1.1、新建类实现ApplicationRuner接口,并添加注解@Component让容器可以扫描到。如下示例代码:

@Slf4j
@Component
public class AccountConfigService implements ApplicationRunner {

@Autowired
private AccountService accountService;

/**
* 配置信息map
* key:AccountEnum属性name
* value: 明细配置对象AccountBaseConfig
*/
private static ConcurrentHashMap<String, AccountBaseConfig> accountConfigMap = new ConcurrentHashMap<>(3);

/**
* 对外调用
* @return
*/
public static final AccountBaseConfig getAccountConfig(String name){
AccountEnum accountEnum = AccountEnum.valueOf(name);
return accountConfigMap.get(accountEnum);
}

@Override
public void run(ApplicationArguments args) {
log.info("启动预加载数据(ApplicationRunner)...{},{}", args.getSourceArgs(), args.getOptionNames());

AccountResponse cjbResponse = accountService.queryById(AccountEnum.CHENGJIANGBO.getId());
ChengjiangboAccountBaseConfig chengjiangboAccountBaseConfig = new ChengjiangboAccountBaseConfig();
BeanUtils.copyProperties(cjbResponse, chengjiangboAccountBaseConfig);
AccountResponse hmxResponse = accountService.queryById(AccountEnum.HANGMENGXIAN.getId());
HanmengxianAccountBaseConfig hanmengxianAccountBaseConfig = new HanmengxianAccountBaseConfig();
BeanUtils.copyProperties(hmxResponse, hanmengxianAccountBaseConfig);
AccountResponse cjhResponse = accountService.queryById(AccountEnum.CHENGJUNHAN.getId());
ChengjunhanAccountBaseConfig chengjunhanAccountBaseConfig = new ChengjunhanAccountBaseConfig();
BeanUtils.copyProperties(cjhResponse, chengjunhanAccountBaseConfig);

accountConfigMap.put(AccountEnum.CHENGJIANGBO.getName(), chengjiangboAccountBaseConfig);
accountConfigMap.put(AccountEnum.HANGMENGXIAN.getName(), hanmengxianAccountBaseConfig);
accountConfigMap.put(AccountEnum.CHENGJUNHAN.getName(), chengjunhanAccountBaseConfig);
}
}
2.1.2、启动类:

@Slf4j
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
log.info("启动成功...");
}

}
2.1.3、启动日志关键信息截图(注意点:预加载数据在容器启动之后,自定义的日志打印前启动执行。)

2.2、CommandLineRunner接口使用
2.2.1、新建类实现CommandLineRuner接口,并添加注解@Component让容器可以扫描到。如下示例代码:

@Slf4j
@Component
public class AccountConfigService1 implements CommandLineRunner {

@Autowired
private AccountService accountService;

/**
* 配置信息map
* key:AccountEnum属性name
* value: 明细配置对象AccountBaseConfig
*/
private static ConcurrentHashMap<String, AccountBaseConfig> accountConfigMap = new ConcurrentHashMap<>(3);

/**
* 对外调用
* @return
*/
public static final AccountBaseConfig getAccountConfig(String name){
AccountEnum accountEnum = AccountEnum.valueOf(name);
return accountConfigMap.get(accountEnum);
}

@Override
public void run(String... args) {
log.info("启动预加载数据(CommandLineRunner)...{}", args);

AccountResponse cjbResponse = accountService.queryById(AccountEnum.CHENGJIANGBO.getId());
ChengjiangboAccountBaseConfig chengjiangboAccountBaseConfig = new ChengjiangboAccountBaseConfig();
BeanUtils.copyProperties(cjbResponse, chengjiangboAccountBaseConfig);
AccountResponse hmxResponse = accountService.queryById(AccountEnum.HANGMENGXIAN.getId());
HanmengxianAccountBaseConfig hanmengxianAccountBaseConfig = new HanmengxianAccountBaseConfig();
BeanUtils.copyProperties(hmxResponse, hanmengxianAccountBaseConfig);
AccountResponse cjhResponse = accountService.queryById(AccountEnum.CHENGJUNHAN.getId());
ChengjunhanAccountBaseConfig chengjunhanAccountBaseConfig = new ChengjunhanAccountBaseConfig();
BeanUtils.copyProperties(cjhResponse, chengjunhanAccountBaseConfig);

accountConfigMap.put(AccountEnum.CHENGJIANGBO.getName(), chengjiangboAccountBaseConfig);
accountConfigMap.put(AccountEnum.HANGMENGXIAN.getName(), hanmengxianAccountBaseConfig);
accountConfigMap.put(AccountEnum.CHENGJUNHAN.getName(), chengjunhanAccountBaseConfig);
}
}
2.2.2、启动类代码:
与前面代码一致,不变。

2.2.3、启动日志关键信息截图(注意点:预加载数据在容器启动之后,自定义的日志打印前启动执行。)

三、区别
3.1、两个接口的实现方法一样,参数不一样,其他没什么区别。两个参数都可以接收java命令设置的参数及值,如下3.1.1截图。
ApplicatonRunner的实现类需要实现的方法:

@Override
public void run(ApplicationArguments args) {}
CommandLineRunner实现类需要实现的方法

@Override
public void run(String... args) {}
3.1.1、设置命令行参数:--spring.profile.active=test,但ApplicatonRunner接口的方法参数ApplicationArguments(是个对象)比CommandLineRunner接口的方法参数(是个可以接收多个string的参数)功能更强大。ApplicatonRunner接口的方法参数ApplicationArguments既可以获取参数的字符串,也可以直接获取key;CommandLineRunner接口的方法参数只能获取参数的字符串。

关键日志信息截图:

3.2、ApplicationRunner接口的实现方法比CommandLineRunner接口的实现方法前执行(当然也可以设置@Order的值来决定谁先执行),如下图。
3.2.1、正常执行的顺序截图

posted @ 2022-08-06 17:16  liftsail  阅读(411)  评论(0编辑  收藏  举报