Spring Boot命令行運(yùn)行器的實(shí)現(xiàn)方法
CommandLineRunner是一個(gè)帶有run方法的簡(jiǎn)單spring引導(dǎo)接口。Spring Boot啟動(dòng)后將自動(dòng)調(diào)用實(shí)現(xiàn)CommandLineRunner接口的所有bean的run方法。
Command Line Runner在加載應(yīng)用程序上下文之后以及Spring Application run方法完成之前執(zhí)行,相當(dāng)于你的應(yīng)用的初始化過程,一般用來實(shí)現(xiàn)一些數(shù)據(jù)預(yù)先加載或預(yù)先處理。
@SpringBootApplication
<b>public</b> <b>class</b> DemoApplication implements CommandLineRunner {
<b>private</b> <b>final</b> Logger logger = LoggerFactory.getLogger(DemoApplication.<b>class</b>);
<b>public</b> <b>static</b> <b>void</b> main(String args) {
SpringApplication.run(DemoApplication.<b>class</b>, args);
}
@Override
<b>public</b> <b>void</b> run(String... strings) throws Exception {
....
}
}
上面的run方法參數(shù)是命令行參數(shù),使用java -jar 啟動(dòng)這個(gè)應(yīng)用的命令行參數(shù)。
如果有多個(gè)命令行運(yùn)行器,可以進(jìn)行排序:
@Component
@Order(1)
<b>public</b> <b>class</b> AnotherDatabaseLoader implements CommandLineRunner {
@Component
@Order(2)
<b>public</b> <b>class</b> DataLoader implements CommandLineRunner {
另外一種在主應(yīng)用的寫法:
@SpringBootApplication
<b>public</b> <b>class</b> UnsplashApplication {
<b>public</b> <b>static</b> <b>void</b> main(String args) {
SpringApplication.run(UnsplashApplication.<b>class</b>, args);
}
@Bean
CommandLineRunner runner(){
<b>return</b> args -> {
System.out.println(<font>"CommandLineRunner running in the UnsplashApplication class..."</font><font>);
};
}
}
</font>
總結(jié)
以上所述是小編給大家介紹的Spring Boot命令行運(yùn)行器的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
springboot?實(shí)戰(zhàn):異常與重定向問題
這篇文章主要介紹了springboot實(shí)戰(zhàn):異常與重定向問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
如何通過Java實(shí)現(xiàn)時(shí)間軸過程解析
這篇文章主要介紹了如何通過Java實(shí)現(xiàn)時(shí)間軸過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
Java實(shí)現(xiàn)微信支付的項(xiàng)目實(shí)踐
最近的一個(gè)項(xiàng)目中涉及到了支付業(yè)務(wù),其中用到了微信支付和支付寶支付,本文就來介紹一下Java實(shí)現(xiàn)微信支付的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
Java多線程 Guarded Suspension設(shè)計(jì)模式
這篇文章主要介紹了Java多線程 Guarded Suspension設(shè)計(jì)模式,Guarded Suspension意為保護(hù)暫停,其核心思想是僅當(dāng)服務(wù)進(jìn)程準(zhǔn)備好時(shí),才提供服務(wù),文章圍繞Java多線程 Guarded Suspension展開內(nèi)容,需要的朋友可以參考一下2021-10-10
java 中Spark中將對(duì)象序列化存儲(chǔ)到hdfs
這篇文章主要介紹了java 中Spark中將對(duì)象序列化存儲(chǔ)到hdfs的相關(guān)資料,需要的朋友可以參考下2017-06-06

