CommandLineRunner和ApplicationRunner是Spring Boot所供应的接口,他们都有一个run()要领。一切完成他们的Bean都会在Spring Boot效劳启动以后自动地被挪用。
因为这个特征,它们是一个抱负处所去做一些初始化的事变,或许写一些测试代码。
CommandLineRunner
运用Application完成
在我们新建好工程后,为了简朴我们直接运用Application类完成CommandLineRunner接口,这个类的注解@SpringBootApplication会为我们自动设置。
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()要领被一般地实行了:
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吸收参数。
这类场景在效劳器上迥殊经常使用。比方我们想实行某个操纵,又不想对外部暴露,此时能够运用CommandLineRunner作为该操纵的进口。
下面我们就打成jar包来演示一下。
- 进入终端界面,最先打包
- 打包完成后,实行该jar包,记得先把IDE的效劳停掉。
能够从日记中看到我们也一般地猎取到了参数。经由过程通报参数,在营业逻辑上我们能够依据差别的参数而实行差别的操纵。
上面我们提到的只是一个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
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。
另一种场景是我们在效劳器上须要实行某个操纵,比方修改数据库用户的数据,而又找不到适宜的实行进口,那末这就是它们抱负的运用场景了。
以上就是CommandLineRunner与ApplicationRunner的引见的细致内容,更多请关注ki4网别的相干文章!