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 来 ...
随机推荐
- unittest之三:字符串与列表的相互转换与分离数据时的应用
一.分离数据时,需读取文档中存储的数据,但TXT文件的数据读取出来的类型为列表,而测试用例中断言的时候验证的是字符串,所以需要将列表转为字符串 #1字符串————>列表 str1='hello ...
- Redis利用Pipeline加速查询速度的方法
1. RTT Redis 是一种基于客户端-服务端模型以及请求/响应协议的TCP服务.这意味着通常情况下 Redis 客户端执行一条命令分为如下四个过程: 发送命令 命令排队 命令执行 返回结果 客户 ...
- python:split()函数
描述 Python 内置函数 指定分隔符对字符串进行切片 如果参数 num 有指定值,则仅分隔 num 个子字符串 返回分割后的字符串列表. 语法 str.split(str="" ...
- powerDisgner 数据类型对比
powerDisgner 16.5版本支持一下类型数据模型格式 以下数字数据类型可用: Standard data type DBMS-specific physical data type Cont ...
- 删库?半个DBA的跑路经验总结
0. 国内呆不下了,赶紧出国 首先,不要选动车,要选最近的一班飞机,尽快出国,能走高速走高速,不然选人少的路线. 没错,我们 DBA 都是常备护照的. 切记,注意看高德地图实时路况. 我们有个前辈就是 ...
- js之运算符(位运算符)
一.概念 位运算在数字底层(表示数字的32个数位)进行运算的.由于位运算是低级的运算操作,所以速度往往也是最快的,但是它很不直观,许多场合不能够使用.大多数语言都提供了按位运算符,恰当的使用按位运算符 ...
- mysql alter 语句用法,添加、修改、删除字段、索引、主键等
修改表名: ALTER TABLE admin_user RENAME TO a_use //增加主键 [sql] view plaincopy alter table tabelname add ...
- VMWare中CentOS7 设置固定IP且能够访问外网
最近搭建kubernetes集群环境时遇到一个问题,CentOS7在重启后IP发生变化导致集群中etcd服务无法启动后集群环境变得不可用,针对这种情况,必须要对CentOS7设置固定IP且可以访问外网 ...
- Settimer及回调函数的用法
在网上看了settimer的一些用法发现能用的真没有,,,,可能是我没找对地方,大部分都是无脑复制粘贴,浪费了很多时间,如果你是一个对这种定时器一无所知的小白,那么请你看进来一定不会让你失望的! 实用 ...
- linux进程调度的算法
linux进程的调度算法 这节我们来学习一下linux进程的优先级 linux进程的优先级 进程提供了两种优先级,一种是普通的进程优先级,第二个是实时优先级,前者使用SCHEED_NORMAL调度策略 ...