Spring Boot命令行運行器的實現(xiàn)方法
CommandLineRunner是一個帶有run方法的簡單spring引導(dǎo)接口。Spring Boot啟動后將自動調(diào)用實現(xiàn)CommandLineRunner接口的所有bean的run方法。
Command Line Runner在加載應(yīng)用程序上下文之后以及Spring Application run方法完成之前執(zhí)行,相當(dāng)于你的應(yīng)用的初始化過程,一般用來實現(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 啟動這個應(yīng)用的命令行參數(shù)。
如果有多個命令行運行器,可以進行排序:
@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命令行運行器的實現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
淺談idea live template高級知識_進階(給方法,類,js方法添加注釋)
下面小編就為大家?guī)硪黄獪\談idea live template高級知識_進階(給方法,類,js方法添加注釋)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06SpringBoot中@Value獲取值和@ConfigurationProperties獲取值用法及比較
在Spring Boot中,@Value注解是一個非常有用的特性,它允許我們將外部的配置注入到我們的Bean中,@ConfigurationProperties用于將配置文件中的屬性綁定到 Java Bean 上,本文介紹了@Value獲取值和@ConfigurationProperties獲取值用法及比較,需要的朋友可以參考下2024-08-08spring boot 配置freemarker及如何使用freemarker渲染頁面
springboot中自帶的頁面渲染工具為thymeleaf 還有freemarker這兩種模板引擎,本文重點給大家介紹spring boot 配置freemarker及如何使用freemarker渲染頁面,感興趣的朋友一起看看吧2023-10-10MySQL如何設(shè)置自動增長序列SEQUENCE的方法
本文主要介紹了MySQL如何設(shè)置自動增長序列SEQUENCE的方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12基于Java+SpringBoot+Vue前后端分離實現(xiàn)倉庫管理系統(tǒng)
這篇文章主要介紹了一個完整的倉庫管理系統(tǒng)是基于Java+Springboot + Vue前后端分離編寫的,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06Mybatis的TypeHandler實現(xiàn)數(shù)據(jù)加解密詳解
這篇文章主要介紹了Mybatis基于TypeHandler實現(xiàn)敏感數(shù)據(jù)加密詳解,Typehandler是mybatis提供的一個接口,通過實現(xiàn)這個接口,可以實現(xiàn)jdbc類型數(shù)據(jù)和java類型數(shù)據(jù)的轉(zhuǎn)換,需要的朋友可以參考下2024-01-01