前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot之CommandLineRunner接口

springboot之CommandLineRunner接口

作者头像
叔牙
发布2020-11-19 15:21:03
4330
发布2020-11-19 15:21:03
举报
文章被收录于专栏:一个执拗的后端搬砖工

springboot之CommandLineRunner接口

在这篇文章中,我们将讨论和探究springboot中的CommandLineRunner接口,将涉及这个接口的不同特性,以及何时使用这个接口。

介绍

springboot中的CommandLineRunner接口提供了在应用程序完全启动时运行特定代码段的方式,这个接口会在应用启动后被springboot自动调用。

1CommandLineRunner接口

@Component public class CustomCommandLineRunner implements CommandLineRunner { private static final Logger LOG = LoggerFactory.getLogger(CustomCommandLineRunner.class); @Override public void run(String...args) throws Exception { LOG.info("Custom command line runner is excuted with command line arguments: {}", Arrays.toString(args)); } }

CommandLineRunner接口只提供了一个运run方法,它在SpringApplication.run(...)方法执行完成之前被调用,如果运行应用,我们可以在服务器上看到如下日志:

2018-07-06 21:54:11.096 INFO 27045 --- [ main] c.j.SpringBootExampleApplication : Started SpringBootExampleApplication in 2.195 seconds (JVM running for 2.998) 2018-07-06 21:54:11.098 INFO 27045 --- [ main] c.j.commandline.CustomCommandLineRunner : Custom command line runner is excuted with command line arguments: []

2CommandLineRunner顺序

我们可以在应用程序中使用任意数量的CommandLineRunner,如果我们想要以特定的顺序调用我们的CommandLineRunner,我们有以下两种方式:

  • 实现 org.springframework.core.Ordered 接口
  • 使用 @Order 注解

2.1:使用Ordered接口排序

实现Ordered接口和getOrder()方法,为自定义运行程序提供优先级:

@Component public class CustomCommandLineRunner implements CommandLineRunner, Ordered { private static final Logger LOG = LoggerFactory.getLogger(CustomCommandLineRunner.class); @Override public void run(String...args) throws Exception { LOG.info("Custom command line runner is excuted with command line arguments: {}", Arrays.toString(args)); } @Override public int getOrder() { return 2; } }

2.2:使用@Order注解排序

使用@Order注解,为自定义运行程序提供优先权:

@Component @Order(1) public class CustomCommandLineRunner2 implements CommandLineRunner { private static final Logger LOG = LoggerFactory.getLogger(CustomCommandLineRunner2.class); @Override public void run(String...args) throws Exception { LOG.info("Calling second command line runner with arguments {}", Arrays.toString(args)); } }

如果运行我们的应用程序,则在服务器控制台上可以看到以下输出:

2018-07-06 22:03:13.906 INFO 27190 --- [ main] c.j.SpringBootExampleApplication : Started SpringBootExampleApplication in 1.811 seconds (JVM running for 2.555) 2018-07-06 22:03:13.907 INFO 27190 --- [ main] c.j.c.CustomCommandLineRunner2 : Calling second command line runner with arguments [] 2018-07-06 22:03:13.907 INFO 27190 --- [ main] c.j.commandline.CustomCommandLineRunner : Custom command line runner is excuted with command line arguments: [] Copy

数字越低,优先级越高。

3何时使用CommandLineRunner接口

在springboot应用中,CommandLineRunner接口是一个非常重要的工具,下面试CommandLineRunner接口的一些常用的应用场景:

  • 准备应用程序初始化数据
  • 来自外部依赖服务的源数据

总结

在这篇篇幅较短的文章中,我们探究了CommandLineRunner接口,介绍了CommandLineRunner接口的常用使用场景,以及根据应用的具体需求创建CommandLineRunner接口实例和设置优先级。

CommandLineRunner接口的作用和spring应用中的ApplicationContextAware接口类似,都是在应用启动之后做一些初始化动作。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-11-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 PersistentCoder 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1CommandLineRunner接口
  • 2CommandLineRunner顺序
  • 3何时使用CommandLineRunner接口
相关产品与服务
日志服务
日志服务(Cloud Log Service,CLS)是腾讯云提供的一站式日志服务平台,提供了从日志采集、日志存储到日志检索,图表分析、监控告警、日志投递等多项服务,协助用户通过日志来解决业务运维、服务监控、日志审计等场景问题。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档