CommandLineRunner和ApplicationRunner作用及区别——SpringBoot

本文主要是介绍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 {@Autowiredprivate 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);} @Overridepublic 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 {@Autowiredprivate 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);}@Overridepublic 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、正常执行的顺序截图

这篇关于CommandLineRunner和ApplicationRunner作用及区别——SpringBoot的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/965319

相关文章

Spring boot整合dubbo+zookeeper的详细过程

《Springboot整合dubbo+zookeeper的详细过程》本文讲解SpringBoot整合Dubbo与Zookeeper实现API、Provider、Consumer模式,包含依赖配置、... 目录Spring boot整合dubbo+zookeeper1.创建父工程2.父工程引入依赖3.创建ap

MyBatis中$与#的区别解析

《MyBatis中$与#的区别解析》文章浏览阅读314次,点赞4次,收藏6次。MyBatis使用#{}作为参数占位符时,会创建预处理语句(PreparedStatement),并将参数值作为预处理语句... 目录一、介绍二、sql注入风险实例一、介绍#(井号):MyBATis使用#{}作为参数占位符时,会

SpringBoot结合Docker进行容器化处理指南

《SpringBoot结合Docker进行容器化处理指南》在当今快速发展的软件工程领域,SpringBoot和Docker已经成为现代Java开发者的必备工具,本文将深入讲解如何将一个SpringBo... 目录前言一、为什么选择 Spring Bootjavascript + docker1. 快速部署与

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

创建Java keystore文件的完整指南及详细步骤

《创建Javakeystore文件的完整指南及详细步骤》本文详解Java中keystore的创建与配置,涵盖私钥管理、自签名与CA证书生成、SSL/TLS应用,强调安全存储及验证机制,确保通信加密和... 目录1. 秘密键(私钥)的理解与管理私钥的定义与重要性私钥的管理策略私钥的生成与存储2. 证书的创建与

浅析Spring如何控制Bean的加载顺序

《浅析Spring如何控制Bean的加载顺序》在大多数情况下,我们不需要手动控制Bean的加载顺序,因为Spring的IoC容器足够智能,但在某些特殊场景下,这种隐式的依赖关系可能不存在,下面我们就来... 目录核心原则:依赖驱动加载手动控制 Bean 加载顺序的方法方法 1:使用@DependsOn(最直

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr