CommandLineRunner and ApplicationRunner
1. Run spring boot as a standalone application (non-web)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.study</groupId>
<artifactId>SpringBootTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>SpringBootTest</name>
<description></description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
package com.commandline.runner; import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class Application implements CommandLineRunner { public static void main(String[] args) { SpringApplication app = new SpringApplication(Application.class);
app.run(args);
} @Override
public void run(String... args) throws Exception {
System.out.println("Hello World");
} }

2. Use CommandLineRunner 启动系统任务
有一些特殊的任务需要在系统启动时执行,例如配置文件加载、数据库初始化等操作。
Spring Boot 项目会在启动时遍历素有CommandLineRunner的实现类并调用其中的 run 方法,如果有多个CommandLineRunner的实现类,那么可以使用@Order对这些实现类的调用顺序进行排序。
如下有2个实现CommandLineRunner 的类,并用 @Order 定义调用顺序。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath />
</parent>
<groupId>com.study</groupId>
<artifactId>SpringBootTest-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>RequestBodyTest</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
import java.util.Arrays; import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; @Component
@Order(1)
public class MyCommandLineRunner1 implements CommandLineRunner { @Override
public void run(String... args) throws Exception {
System.out.println("Runner1>>>" + Arrays.toString(args)); } }
package com.commandline.runner; import java.util.Arrays; import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; @Component
@Order(2)
public class MyCommandLineRunner2 implements CommandLineRunner { @Override
public void run(String... args) throws Exception {
System.out.println("Runner2>>>" + Arrays.toString(args)); } }
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class WebApplication { public static void main(String[] args) { SpringApplication.run(WebApplication.class, args);
} }
配置参数

启动WebApplication, 可以看到,如下运行结果:
CommandLineRunner 实现类的 run 方法, 在SpringBoot Application 启动后被调用。

3. CommandLineRunner 和 ApplicationRunner 的区别
CommandLineRunner 和 ApplicationRunner 基本一致,差别主要体现在参数上:
CommandLineRunner: 参数为 String... args
ApplicationRunner: 参数为 ApplicationArguments
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component; import java.util.List;
import java.util.Set; @Component
public class MyApplicationRunner1 implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
List<String> nonOptionArgs = args.getNonOptionArgs();
System.out.println("NonOptionArgs>>>" + nonOptionArgs);
Set<String> optionNames = args.getOptionNames();
for (String optionName : optionNames) {
System.out.println("key:" + optionName + ";value:" +
args.getOptionValues(optionName));
}
}
}
将项目打包,并在命令行执行:

得到如下执行结果:

CommandLineRunner and ApplicationRunner的更多相关文章
- Spring Boot 2 - 使用CommandLineRunner与ApplicationRunner
本篇文章我们将探讨CommandLineRunner和ApplicationRunner的使用. 在阅读本篇文章之前,你可以新建一个工程,写一些关于本篇内容代码,这样会加深你对本文内容的理解,关于如何 ...
- CommandLineRunner和ApplicationRunner的区别
CommandLineRunner和ApplicationRunner的区别 二者的功能和官方文档一模一样,都是在Spring容器初始化完毕之后执行起run方法 不同点在于,前者的run方法参数是St ...
- 使用CommandLineRunner或ApplicationRunner接口创建bean
在spring boot应用中,我们可以在程序启动之前执行任何任务.为了达到这个目的,我们需要使用CommandLineRunner或ApplicationRunner接口创建bean,spring ...
- 010-Spring Boot 扩展分析-ApplicationContextInitializer、CommandLineRunner、ApplicationRunner
一.常见的两个扩展点 1.ApplicationContextInitializer 1.1.作用实现 作用:接口实在Spring容器执行refresh之前的一个回调. Callback interf ...
- CommandLineRunner和ApplicationRunner
使用场景 我们在开发过程中会有这样的场景:需要在容器启动的时候执行一些内容,比如:读取配置文件信息,数据库连接,删除临时文件,清除缓存信息,在Spring框架下是通过ApplicationListen ...
- 项目启动加载配置,以及IP黑名单,使用CommandLineRunner和ApplicationRunner来实现(一般用在网关进行拦截黑名单)
//使用2个类的run方法都可以在项目启动时加载配置,唯一不同的是他们的参数不一样,CommandLineRunner的run方法参数是基本类型,ApplicationRunner的run方法参数是一 ...
- SpringBoot ApplicationRunner/CommandLineRunner
CommandLineRunner.ApplicationRunner 接口是在容器启动成功后的最后一步回调(类似开机自动启动). CommandLineRunner.ApplicationRunne ...
- SpringBoot之CommandLineRunner接口和ApplicationRunner接口
我们在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的.SpringBoot给我们提供了两个接口来帮助我们实现这种需求.这两个接口分别为CommandLi ...
- Spring Boot 启动加载数据 CommandLineRunner
实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求. 为了解决这样的问题,Spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来 ...
随机推荐
- 富文本编辑器--使用textarea即时更新文本域同步编辑器内容
使用 textarea wangEditor 从v3版本开始不支持 textarea ,但是可以通过onchange来实现 textarea 中提交富文本内容. <div id="di ...
- python的加密方式
MD5加密 这是一种使用非常广泛的加密方式,不可逆的,在日常字符串加密中经常会用到,下面我简单介绍一下这种方式,主要用到Python自带的模块hashlib,测试代码如下,先创建一个md5对象,然后直 ...
- [转] JAVA分为三个体系,JavaSE,JavaEE,JavaME(J2ME)的区别以及各个版
Java SE(JavaPlatform,Standard Edition).Java SE 以前称为 J2SE.它允许开发和部署在桌面.服务器.嵌入式环境和实时环境中使用的 Java 应用程 ...
- python django网站编程视频教程学习资料下载
“人生苦短,我用python”,学python的小伙伴应该都了解这句话的含义.但是,学python,你真正了了解强大的Django框架吗!?据说Django还是由吉普赛的一个吉他手的名字命名的呢,有木 ...
- Delphi 子界类型
- php页面加载完毕后再显示购买按钮
php页面加载完毕后再显示购买按钮 $document.ready(function(){ $("#buybotton").show()})
- windows静态路由
本机:192.168.1.10 本机网关:192.168.1.254 目的IP:188.1.1.10 指定网关:192.168.1.107 最多跳数:10跳 route -p add 188.1 ...
- ora 12518监听程序无法分发客户机连接
首先修改ORACLE的PROCESS.SESSION数量 查看当前ORALCE PROCESS数量 SQL> show parameter process 查看当前ORALCE SESSION数 ...
- Java使用freemarker导出word文档
通过freemarker,以及JAVA,导出word文档. 共分为三步: 第一步:创建模板文件 第二步:通过JAVA创建返回值. 第三步:执行 分别介绍如下: 第一步: 首先创建word文档,按照想要 ...
- 【SQL-分组合并字符串】把相同分组的某个字段合并为同一个字符串(使用函数)
场景:我要把同一个订单同一个客户同一个产品分组合并,同时把该产品所有的库位列举出来,合成一个字符串. 原始数据: 我要得到下面的结果: SQL如下: ==先建个方法== create function ...