欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

CommandLineRunner、ApplicationRunner 接口

发布时间:2024/9/30 编程问答 53 豆豆
生活随笔 收集整理的这篇文章主要介绍了 CommandLineRunner、ApplicationRunner 接口 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

       如果我们想在项目启动后做一些事情(如加载定时任务,初始化工作),可以使用spring提供的CommandLineRunner、ApplicationRunner 接口,在容器启动成功后的最后一步回调(类似开机自启动)。

1. CommandLineRunner接口

/*** Interface used to indicate that a bean should <em>run</em> when it is contained within* a {@link SpringApplication}. Multiple {@link CommandLineRunner} beans can be defined* within the same application context and can be ordered using the {@link Ordered}* interface or {@link Order @Order} annotation.* <p>* If you need access to {@link ApplicationArguments} instead of the raw String array* consider using {@link ApplicationRunner}.** @author Dave Syer* @see ApplicationRunner*/ public interface CommandLineRunner {/*** Callback used to run the bean.* @param args incoming main method arguments* @throws Exception on error*/void run(String... args) throws Exception;}

多个CommandLineRunner可以被同时执行在同一个spring上下文中并且执行顺序是以order注解的参数顺序一致。

下面看一个demo:

@Order(2) @Component public class ServerStartedReport implements CommandLineRunner{@Overridepublic void run(String... args) throws Exception {System.out.println("===========ServerStartedReport启动====="+ LocalDateTime.now());} }

配置参数启动项目:

控制台打印:

[ddd,tyy] ===========ServerStartedReport启动=====2019-02-14T21:31:30.466

2. ApplicationRunner接口

       二者基本一样,区别在于接收的参数不一样。CommandLineRunner的参数是最原始的参数,没有做任何处理,而ApplicationRunner的参数是ApplicationArguments,对原始参数做了进一步的封装。
如我们在这里配置了一些启动参数--foo=hu --log=debug

CommandLineRunner只是获取--name=value。而ApplicationRunner可以解析--name=value,使得我们可以直接通过name来获取value。

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("ApplicationRunner:"+ Arrays.asList(args.getSourceArgs()));System.out.println("getOptionNames:"+args.getOptionNames());System.out.println("getOptionValues:"+args.getOptionValues("foo"));System.out.println("getOptionValues:"+args.getOptionValues("log"));} }

打印结果为:

===========ServerStartedReport启动=====2019-02-14T21:36:23.668 ApplicationRunner:[--foo=hu, --log=debug] getOptionNames:[log, foo] getOptionValues:[hu] getOptionValues:[debug]

注意启动后执行的方法一定要加try catch,因为此处抛出异常会影响项目启动。

总结

以上是生活随笔为你收集整理的CommandLineRunner、ApplicationRunner 接口的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。