CommandLineRunner和ApplicationRunner作用及区别——SpringBoot

目录

 

一、需求与前言

二、接口的使用

2.1、ApplicationRunner接口使用

2.2、CommandLineRunner接口使用

三、区别

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

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


一、需求与前言

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、正常执行的顺序截图

  • 13
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值