2019独角兽企业重金招聘Python工程师标准>>>

使用场景的提出;

我们在开发过程中会有这样的场景:需要在容器启动的时候执行一些内容,比如:读取配置文件信息,数据库连接,删除临时文件,清除缓存信息,在Spring框架下是通过ApplicationListener监听器来实现的。在Spring Boot中给我们提供了两个接口来帮助我们实现这样的需求。这两个接口就是我们今天要讲的CommandLineRunnerApplicationRunner他们的执行时机为容器启动完成的时候

CommandLineRunner接口

CommandLineRunner

官方doc:
Interface used to indicate that a bean should run when it is contained within a SpringApplication. Multiple CommandLineRunner beans can be defined within the same application context and can be ordered using the Ordered interface or Order @Order annotation.
接口被用作将其加入spring容器中时执行其run方法。多个CommandLineRunner可以被同时执行在同一个spring上下文中并且执行顺序是以order注解的参数顺序一致。

If you need access to ApplicationArguments instead of the raw String array
consider using ApplicationRunner.
如果你需要访问ApplicationArguments去替换掉字符串数组,可以考虑使用ApplicationRunner类。

先看一个demo:
定义一个ServerStartedReport实现CommandLineRunner,并纳入到srping容器中进行处理

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;import java.time.LocalDateTime;@Order(2)
@Component
public class ServerStartedReport implements CommandLineRunner{@Overridepublic void run(String... args) throws Exception {System.out.println("===========ServerStartedReport启动====="+ LocalDateTime.now());}
}

定义一个ServerSuccessReport实现CommandLineRunner,并纳入到spring容器处理

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;import java.util.Arrays;@Order(1)
@Component
public class ServerSuccessReport implements CommandLineRunner{@Overridepublic void run(String... args) throws Exception {System.out.println("=====应用已经成功启动====="+ Arrays.asList(args));}
}

启动类测试,也可以直接在spring容器访问该值,

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplication
public class Application {public static void main(String[] args) {ConfigurableApplicationContext context =SpringApplication.run(Application.class,args);ApplicationArguments applicationArguments = context.getBean(ApplicationArguments.class);System.out.println("============");System.out.println("name="+applicationArguments.getOptionNames());System.out.println("values===="+applicationArguments.getOptionValues("developer.name"));}
}

配置参数,然后执行启动类

图片.png

打印结果

ApplicationRunner接口

发现二者的官方javadoc一样,区别在于接收的参数不一样。CommandLineRunner的参数是最原始的参数,没有做任何处理。ApplicationRunner的参数是ApplicationArguments,是对原始参数做了进一步的封装。

ApplicationArguments是对参数(main方法)做了进一步的处理,可以解析--name=value的,我们就可以通过name来获取value(而CommandLineRunner只是获取--name=value)

可以接收--foo=bar这样的参数。

--getOptionNames()方法可以得到foo这样的key的集合。
--getOptionValues(String name)方法可以得到bar这样的集合的value。

看一个demo:
定义MyApplicationRunner类继承ApplicationRunner接口,

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;import java.util.Arrays;@Component
public class MyApplicationRunner implements ApplicationRunner{@Overridepublic void run(ApplicationArguments args) throws Exception {System.out.println("===MyApplicationRunner==="+ Arrays.asList(args.getSourceArgs()));System.out.println("===getOptionNames========"+args.getOptionNames());System.out.println("===getOptionValues======="+args.getOptionValues("foo"));System.out.println("==getOptionValues========"+args.getOptionValues("developer.name"));}
}

启动类,

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class,args);}
}

配置参数启动,

打印结果:

转载于:https://my.oschina.net/spinachgit/blog/1863740

Spring Boot实践--CommandLineRunner接口相关推荐

  1. Spring Boot实践——Spring AOP实现之动态代理

    Spring AOP 介绍 AOP的介绍可以查看 Spring Boot实践--AOP实现 与AspectJ的静态代理不同,Spring AOP使用的动态代理,所谓的动态代理就是说AOP框架不会去修改 ...

  2. 太好了 | 这篇写的太好了!Spring Boot + Redis 实现接口幂等性

    Hi ! 我是小小,今天是本周的第四篇,第四篇主要内容是 Spring Boot + Redis 实现接口幂等性 介绍 幂等性的概念是,任意多次执行所产生的影响都与一次执行产生的影响相同,按照这个含义 ...

  3. Spring Boot实践

    Spring Boot实践 在本文中,我将重点介绍Spring Boot特有的实践(大多数时候,也适用于Spring项目).以下依次列出了最佳实践,排名不分先后. 1.使用自定义BOM来维护第三方依赖 ...

  4. Spring Boot 实践折腾记(11):使用 Spring 5的WebFlux快速构建效响应式REST API

    关于Spring 5中的反应式编程支持Reactor类库,上一篇文章< Spring Boot 实践折腾记(10):2.0+版本中的反应式编程支持--Reactor>已经简要介绍过,Spr ...

  5. 使用Spring Boot 的CommandLineRunner遇到的坑

    使用场景 再应用程序开发过程中,往往我们需要在容器启动的时候执行一些操作.Spring Boot中提供了CommandLineRunner和ApplicationRunner两个接口来实现这样的需求. ...

  6. Spring Boot 实践折腾记(15):使用Groovy

    Java是在JVM上运行的最广泛使用的编程语言.不过,还有很多其他基于JVM的语言,比如Groovy,Scala,JRuby,Jython,Kotlin等等.其中,Groovy和Scala现在在Jav ...

  7. scheduled 一秒钟执行一次_spring boot的Scheduled帮你实现定时任务,spring boot实践(11)...

    01 spring boot读取配置信息 02 多环境配置 03 处理全局异常 04 spring boot admin 05 spring mvc + mybatis 06 spring boot ...

  8. Spring boot 梳理 - WebMvcConfigurer接口 使用案例

    转:https://yq.aliyun.com/articles/617307 SpringBoot 确实为我们做了很多事情, 但有时候我们想要自己定义一些Handler,Interceptor,Vi ...

  9. Spring Boot + Dataway :接口不用写,配配就出来?

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 哈库纳 来源 | my.oschina.net ...

最新文章

  1. Py之tornado:tornado库的简介、安装、使用方法之详细攻略
  2. 云原生时代 RocketMQ 运维管控的利器 - RocketMQ Operator
  3. [C++ map dp]codeforces 960F. Pathwalks
  4. linux复制和剪切命令,Linux命令 复制粘贴剪切
  5. 简单一致的Log4j2 Logger命名
  6. sklearn、theano、TensorFlow 以及 theras 的理解
  7. bootstrapr表格父子框_JS组件系列——表格组件神器:bootstrap table(二:父子表和行列调序)...
  8. 【Java】使用springboot运行程序时出现的错误
  9. 谷歌和 Zyxel 各修复一个已遭利用的 0day
  10. windows2003——工作组和域控制器
  11. python流量分析_python 监控流量
  12. 手足之爱,平生一人:他们是中国历史上感情最好的一对兄弟 (苏轼苏辙,邓林武邓林飞)
  13. 威廉玛丽学院计算机专业好吗,威廉玛丽学院计算机硕士语言要求请问在哪
  14. 二线城市疯狂抢人,技术人才何去何从?
  15. Unity 显示FPS
  16. [转载]java图片缩放处理
  17. 第9章第4节:制作商业计划书的目录页面 [PowerPoint精美幻灯片实战教程]
  18. 2021-03-22linux内核编译和安装过程
  19. 江西理工大学南昌校区排名赛 E: 单身狗的种树游戏
  20. Chapter 11 特征选择和稀疏学习

热门文章

  1. 0基础学python编程难吗-对于0基础的人,直接学 Python 编程合适吗?
  2. 九九乘法表口诀python-Python用for循环实现九九乘法表
  3. python100个免费实例-Python实例100个(基于最新Python3.7版本)
  4. python快速编程入门课后程序题答案-Python 入门编程题:1~10(答案)
  5. 搜索github项目stars排名
  6. es中发现结点的角色
  7. LeetCode Text Justification(贪心)
  8. 21天战拖记——Day10:“书柜整理法”再学习(2014-05-13)
  9. Jekyll添加FancyBox 插件
  10. 经常使用排序算法实现[交换排序之冒泡排序、高速排序]