CommandLineRunner与ApplicationRunner接口的使用及源码解析
引言
我們?cè)谑褂肧pringBoot搭建項(xiàng)目的時(shí)候,如果希望在項(xiàng)目啟動(dòng)完成之前,能夠初始化一些操作,針對(duì)這種需求,可以考慮實(shí)現(xiàn)如下兩個(gè)接口(任一個(gè)都可以)
org.springframework.boot.CommandLineRunner org.springframework.boot.ApplicationRunner 復(fù)制代碼CommandLineRunner、ApplicationRunner 接口是在容器啟動(dòng)成功后的最后一步回調(diào)(類似開機(jī)自啟動(dòng))。
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容器中時(shí)執(zhí)行其run方法。多個(gè)CommandLineRunner可以被同時(shí)執(zhí)行在同一個(gè)spring上下文中并且執(zhí)行順序是以order注解的參數(shù)順序一致。
If you need access to ApplicationArguments instead of the raw String array consider using ApplicationRunner.
如果你需要訪問ApplicationArguments去替換掉字符串?dāng)?shù)組,可以考慮使用ApplicationRunner類。
實(shí)例demo
定義一個(gè)ServerStartedReport實(shí)現(xiàn)CommandLineRunner,并納入到srping容器中進(jìn)行處理
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啟動(dòng)====="+ LocalDateTime.now());} } 復(fù)制代碼定義一個(gè)ServerSuccessReport實(shí)現(xiàn)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("=====應(yīng)用已經(jīng)成功啟動(dòng)====="+ Arrays.asList(args));} } 復(fù)制代碼啟動(dòng)類測(cè)試,也可以直接在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"));} } 復(fù)制代碼配置參數(shù),然后執(zhí)行啟動(dòng)類
打印結(jié)果ApplicationRunner接口
發(fā)現(xiàn)二者的官方j(luò)avadoc一樣,區(qū)別在于接收的參數(shù)不一樣。CommandLineRunner的參數(shù)是最原始的參數(shù),沒有做任何處理。ApplicationRunner的參數(shù)是ApplicationArguments,是對(duì)原始參數(shù)做了進(jìn)一步的封裝。ApplicationArguments是對(duì)參數(shù)(main方法)做了進(jìn)一步的處理,可以解析--name=value的,我們就可以通過name來獲取value(而CommandLineRunner只是獲取--name=value)
可以接收--foo=bar這樣的參數(shù)。- getOptionNames()方法可以得到foo這樣的key的集合。
- getOptionValues(String name)方法可以得到bar這樣的集合的value。
實(shí)例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"));} } 復(fù)制代碼啟動(dòng)類
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);} } 復(fù)制代碼配置參數(shù)啟動(dòng)
打印結(jié)果總結(jié)
用戶使用CommandLineRunner或者ApplicationRunner接口均可實(shí)現(xiàn)應(yīng)用啟動(dòng)初始化某些功能的需求,如果希望對(duì)參數(shù)有更多的操作,則可以選擇實(shí)現(xiàn)ApplicationRunner接口。
擴(kuò)展閱讀
CommandLineRunner、ApplicationRunner執(zhí)行流程源碼分析
用戶只要實(shí)現(xiàn)這兩個(gè)接口,其中的run方法就會(huì)在項(xiàng)目啟動(dòng)時(shí)候被自動(dòng)調(diào)用,那么究竟是在什么時(shí)候調(diào)用的呢?下面可以看一下Application的啟動(dòng)流程
SpringApplication.run(args)
this.afterRefresh(context, applicationArguments)方法
跟蹤context.getBeansOfType()方法,具體實(shí)現(xiàn)在類DefaultListableBeanFactory中
總結(jié):通過以上分析可知,實(shí)現(xiàn)這兩個(gè)接口的類,在ApplicationContext.run()方法里被執(zhí)行轉(zhuǎn)載于:https://juejin.im/post/5ce3508af265da1bb80c0195
超強(qiáng)干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的CommandLineRunner与ApplicationRunner接口的使用及源码解析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为什么AsyncTask的doInBac
- 下一篇: 给指定的某个commit号加tag并推送