本篇文章咱们将探讨CommandLineRunner和ApplicationRunner的使用。java
在阅读本篇文章以前,你能够新建一个工程,写一些关于本篇内容代码,这样会加深你对本文内容的理解,关于如何快速建立新工程,能够参考个人这篇博客:spring
CommandLineRunner和ApplicationRunner是Spring Boot所提供的接口,他们都有一个run()方法。全部实现他们的Bean都会在Spring Boot服务启动以后自动地被调用。服务器
因为这个特性,它们是一个理想地方去作一些初始化的工做,或者写一些测试代码。app
在咱们新建好工程后,为了简单咱们直接使用Application类实现CommandLineRunner接口,这个类的注解@SpringBootApplication会为咱们自动配置。ide
package cn.examplecode.sb2runner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Sb2runnerApplication implements CommandLineRunner { private static Logger logger = LoggerFactory.getLogger(Sb2runnerApplication.class); public static void main(String[] args) { SpringApplication.run(Sb2runnerApplication.class, args); } @Override public void run(String... args) throws Exception { logger.info("服务已启动,执行command line runner。"); for (int i = 0; i < args.length; ++i) { logger.info("args[{}]: {}", i, args[i]); } } }
接下来咱们直接启动服务,查看日志以下,发现run()方法被正常地执行了:spring-boot
Tomcat started on port(s): 8080 (http) with context path '' Started Sb2runnerApplication in 2.204 seconds (JVM running for 3.161) 服务已启动,执行command line runner。
run()方法有个可变参数args,这个参数是用来接收命令行参数的,咱们下面来加入参数来测试一下:学习
而后重启服务,观察日志,能够看到参数被正常地接收到了:测试
Tomcat started on port(s): 8080 (http) with context path '' Started Sb2runnerApplication in 1.888 seconds (JVM running for 2.41) 服务已启动,执行command line runner。 args[0]: --param=sth
以前咱们说过使用Spring Boot的一大优点就是能够将工程直接打包成一个jar包而不须要单独部署。打包成jar包后能够直接执行该jar包进行服务的启动,这样在执行jar包时咱们就能够传入命令行参数,让CommandLineRunner接收参数。spa
这种场景在服务器上特别经常使用。好比咱们想执行某个操做,又不想对外部暴露,此时可使用CommandLineRunner做为该操做的入口。
下面咱们就打成jar包来演示一下。
能够从日志中看到咱们也正常地获取到了参数。经过传递参数,在业务逻辑上咱们能够根据不一样的参数而执行不一样的操做。
上面咱们提到的只是一个CommandLineRunner,若是咱们有多个CommandLineRunner怎么办呢?怎么控制它们执行的顺序呢?
下面咱们就来介绍如何指定执行的顺序。
Spring Boot为咱们提供了一个注解"@Order",能够用来指定执行的顺序,好比咱们工程里面有三个CommandLineRunner:
@Component @Order(1) public class CommandRunner1 implements CommandLineRunner { private static Logger logger = LoggerFactory.getLogger(CommandRunner1.class); @Override public void run(String... args) throws Exception { logger.info("执行第一个command line runner..."); } } @Component @Order(2) public class CommandRunner2 implements CommandLineRunner { private static Logger logger = LoggerFactory.getLogger(CommandRunner2.class); @Override public void run(String... args) throws Exception { logger.info("执行第二个command line runner..."); } } @Component @Order(3) public class CommandRunner3 implements CommandLineRunner { private static Logger logger = LoggerFactory.getLogger(CommandRunner3.class); @Override public void run(String... args) throws Exception { logger.info("执行第三个command line runner..."); } }
咱们能够在该类的上面直接加入@Order注解,而后Spring Boot就会按照咱们注解指定的顺序从小到大的执行了。很简单,是否是?
Tomcat started on port(s): 8080 (http) with context path '' Started Sb2runnerApplication in 1.764 seconds (JVM running for 2.292) 执行第一个command line runner... 执行第二个command line runner... 执行第三个command line runner...
ApplicationRunner与CommandLineRunner作的事情是同样的,也是在服务启动以后其run()方法会被自动地调用,惟一不一样的是ApplicationRunner会封装命令行参数,能够很方便地获取到命令行参数和参数值。
@Component public class ApplicationRunner1 implements ApplicationRunner { private static Logger logger = LoggerFactory.getLogger(ApplicationRunner1.class); @Override public void run(ApplicationArguments args) throws Exception { logger.info("执行application runner..."); logger.info("获取到参数: " + args.getOptionValues("param")); } }
执行结果:
咱们能够发现,经过run()方法的参数ApplicationArguments能够很方便地获取到命令行参数的值。
因此若是你的工程须要获取命令行参数的话,建议你使用ApplicationRunner。
不管是CommandLineRunner仍是ApplicationRunner,它们的目的都是在服务启动以后执行一些操做。若是须要获取命令行参数时则建议使用ApplicationRunner。
另外一种场景是咱们在服务器上须要执行某个操做,好比修正数据库用户的数据,而又找不到合适的执行入口,那么这就是它们理想的使用场景了。
个人博客中其余关于Spring Boot的全部文章能够点击这里找到,欢迎关注!
若是有问题能够留言,或者给我发邮件lloyd@examplecode.cn,期待咱们共同窗习与成长!