日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CommandLineRunner与ApplicationRunner接口的使用及源码解析

發(fā)布時(shí)間:2024/4/14 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CommandLineRunner与ApplicationRunner接口的使用及源码解析 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

引言

我們?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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 美女扒开尿口给男人看 | 久久国产成人精品国产成人亚洲 | 国产欧美日韩二区 | 2020国产精品视频 | 久本草精品 | 天天爽夜夜爽 | 欧美入口 | 欧美性吧| 办公室荡乳欲伦交换bd电影 | 懂色av粉嫩av蜜乳av | 日韩国产成人在线 | 黑人巨大精品欧美一区免费视频 | 国产精品视频一二区 | 一本色道久久综合亚洲二区三区 | 高清黄色一级片 | 国产在线视频导航 | 天天伊人网 | 无码av免费精品一区二区三区 | 影音先锋中文字幕一区二区 | 日本性久久 | 99re6这里有精品热视频 | 超碰人人射 | av电影在线播放 | 99极品视频| 日本韩国欧美在线 | 少妇太爽了在线观看 | 国产乱国产 | 色偷偷91| 日韩精品一区二区三区网站 | 亚洲无线视频 | 亚洲精品视频在线看 | 强行无套内谢大学生初次 | 亚洲专区一区二区三区 | 丁香花电影免费播放在线观看 | 摸大乳喷奶水www视频 | 亚洲精品影院 | 久久91 | 九九视频免费看 | 黄色成人小视频 | 欧美无遮挡高潮床戏 | 日本黄色中文字幕 | 制服丝袜在线第一页 | 久久理伦| 日本欧美中文字幕 | 国产又粗又猛又黄又爽无遮挡 | 日韩av地址| 男人天堂a| 青青草手机视频在线观看 | 欧美一区二区三区精品 | 黄色aa视频 | 亚洲天堂免费在线观看视频 | 久久激情免费视频 | 国产精品麻豆欧美日韩ww | 午夜免费播放观看在线视频 | 日韩精品免费视频 | 久久免费在线视频 | 澳门久久久 | 男人午夜影院 | av不卡影院 | 中文字幕在线不卡视频 | 国产又粗又大又黄 | 日本打白嫩屁股视频 | 欧美三级久久 | 91播放在线 | 岛国精品资源网站 | 亚洲精品综合 | 国产精品入口麻豆 | 一区二区欧美在线观看 | 蜜臀人妻四季av一区二区不卡 | 天天操天天射天天舔 | 亚洲91网 | 和黑帮大佬的365 | 久久99精品视频 | 中文字幕第22页 | 国产精品女人久久久 | 欧美日韩在线播放三区四区 | 99热这里精品 | 久久99深爱久久99精品 | 四虎午夜| 9999视频| 河北彩花69xx精品一区 | 大黄一级片 | 99热这里只有精品2 91免费版黄 | 天堂网成人 | 午夜伦伦电影理论片费看 | 成人污污视频在线观看 | 亚洲精品九九 | 国产精品二三区 | 国产艳情片 | 国产aⅴ爽av久久久久成人 | 亚洲精品无码一区二区 | 日韩经典第一页 | 91精品国产一区二区三竹菊影视 | 蜜臀av粉嫩av懂色av | 欧美日韩午夜爽爽 | 成人av手机在线观看 | 日韩激情网 | 青青草精品 | 成人黄色在线观看视频 |