ApplicationRunner 和 CommandLineRunner 功能一致,用法也基本一致,唯一的区别主要体现在对参数的处理上,ApplicationRunner 可以接收更多类型的参数(ApplicationRunner 除了可以接收 CommandLineRunner 的参数之外,还可以接收 key/value形式的参数)。

 
 

 
 

一、创建MyApplicationRunner类实现ApplicationRunner接口

 
 

 
 

二、重写run()方法并接收更多具体参数

 
 

//args.getSourceArgs();则表示获取命令行中的所有参数。

String[]sourceArgs=args.getSourceArgs();

System.out.println("sourceArgs1>>>>>>>>>>"+Arrays.toString(sourceArgs));

//args.getNonOptionArgs();可以用来获取命令行中的无key参数(和CommandLineRunner一样)。

List<String>nonOptionArgs=args.getNonOptionArgs();

System.out.println("nonOptionArgs1>>>>>>>>>>>"+nonOptionArgs);

//args.getOptionNames();可以用来获取所有key/value形式的参数的key。

Set<String>optionNames=args.getOptionNames();

System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

for(StringoptionName:optionNames){

//args.getOptionValues(key));可以根据key获取key/value形式的参数的value。

System.out.println(optionName+">>>>>>>>>>"+args.getOptionValues(optionName));

}

System.out.println("\n");

System.out.println(">>>>>>>>>>>>>>>>>applicationrunner1结束>>>>>>>>>>>>>>>>>>>>>>>");

 
 

三、项目打包,命令行传参数

 
 

D:\IdeaProjects\interceptordemo\target>java -jar demo-0.0.1-SNAPSHOT.jar --name=hufei hufei www.baidu.com

 
 

demo-0.0.1-SNAPSHOT.jar 这个是项目打包的jar包

 
 

 
 

四、效果图

 
 

 
 

 
 

ApplicationRunner接口的更多相关文章

  1. 使用CommandLineRunner或ApplicationRunner接口创建bean

    在spring boot应用中,我们可以在程序启动之前执行任何任务.为了达到这个目的,我们需要使用CommandLineRunner或ApplicationRunner接口创建bean,spring ...

  2. SpringBoot之CommandLineRunner接口和ApplicationRunner接口

    我们在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的.SpringBoot给我们提供了两个接口来帮助我们实现这种需求.这两个接口分别为CommandLi ...

  3. SpringBoot之ApplicationRunner接口和@Order注解

    我们在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的.SpringBoot给我们提供了ApplicationRunner接口来帮助我们实现这种需求.该接 ...

  4. SpringBoot ApplicationRunner/CommandLineRunner

    CommandLineRunner.ApplicationRunner 接口是在容器启动成功后的最后一步回调(类似开机自动启动). CommandLineRunner.ApplicationRunne ...

  5. SpringBoot的ApplicationRunner

    Article1 在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的.SpringBoot给我们提供了两个接口来帮助我们实现这种需求.这两个接口分别为Co ...

  6. SpringBoot扩展点之二:ApplicationRunner和CommandLineRunner的扩展

    CommandLineRunner并不是Spring框架原有的概念,它属于SpringBoot应用特定的回调扩展接口: public interface CommandLineRunner { /** ...

  7. Spring接口

    FactoryBean接口 Spring中有两种类型的Bean:一种是普通的JavaBean:另一种就是工厂Bean(FactoryBean),这两种Bean都受Spring的IoC容器管理. Fac ...

  8. Spring Boot 定义系统启动任务,你会几种方式?

    在 Servlet/Jsp 项目中,如果涉及到系统任务,例如在项目启动阶段要做一些数据初始化操作,这些操作有一个共同的特点,只在项目启动时进行,以后都不再执行,这里,容易想到web基础中的三大组件( ...

  9. Java项目启动时执行指定方法的几种方式

    很多时候我们都会碰到需要在程序启动时去执行的方法,比如说去读取某个配置,预加载缓存,定时任务的初始化等.这里给出几种解决方案供大家参考. 1. 使用@PostConstruct注解 这个注解呢,可以在 ...

随机推荐

  1. vtkTestHull将多个平面围成一个凸面体

    1.vtkHull produce an n-sided convex hull vtkHull is a filter which will produce an n-sided convex hu ...

  2. MyBatis框架原理2:SqlSession运行过程

    获取SqlSession对象 SqlSession session = sqlSessionFactory.openSession(); 首先通过SqlSessionFactory的openSessi ...

  3. jitamin配置(nginx设置)

    server { listen 66; server_name 192.168.2.253; root "/var/www/jitamin/public"; location / ...

  4. 描述下什么是springcloud,springcloud中的组件有哪些?分别描述下它的原理?

    1.什么是springcloud,springcloud中的组件有哪些? Spring cloud是一个基于Spring Boot实现的服务治理工具包,在微服务架构中用于管理和协调服务的微服务:就是把 ...

  5. Autumn is deep, alas! I stand on the grass in the shadow of the evergreen trees.

    essence. n. 本质 flush.n. 脸红 v. 刷洗 initiate.v.开始 intrinsic.固执的 mainfest.a.显然的 intuition.n.直觉上的 refrain ...

  6. 【神经网络与深度学习】Caffe使用step by step:caffe框架下的基本操作和分析

    caffe虽然已经安装了快一个月了,但是caffe使用进展比较缓慢,果然如刘老师说的那样,搭建起来caffe框架环境比较简单,但是完整的从数据准备->模型训练->调参数->合理结果需 ...

  7. XSS-笔记

     Cross Site Script  跨站脚本 是一种客户端代码的注入  而命令注入.sql注入都是客户端代码的注入.   XSS攻击行为的目标为:1.窃取目标的cookie信息 2.执行CSRF脚 ...

  8. 网络架构,七层协议,三次握手四次挥手,socket套接字简单编程

    一.单机架构 应用领域: 植物大战僵尸 office 二.CS架构 应用领域: QQ 大型网络游戏 计算机发展初期用户去取数据,直接就去主机拿,从这里开始就分出了客户端和服务端. 客户端:用户安装的软 ...

  9. java json对象转换

    引入的jar包: commons-beanutils-1.9.2.jar commons-collections-3.2.1.jar commons-lang-2.6.jar commons-logg ...

  10. notes-19-05-10

    一 mysql查找一个表中字段相同的数据  2019-05-10 15:51:03   多字段 ) 二 Referer的作用?2019-05-17 10:03:48 (来自网络) 1.防盗链我在www ...