springboot CommandLineRunner接口實(shí)現(xiàn)自動(dòng)任務(wù)加載功能
CommandLineRunner接口可以實(shí)現(xiàn)任務(wù)的自動(dòng)加載,當(dāng)項(xiàng)目啟動(dòng)完后,就會(huì)自動(dòng)去執(zhí)行CommandLineRunner接口里的run方法,你可以實(shí)現(xiàn)多個(gè)CommandLineRunner的實(shí)例,使用order來控制執(zhí)行的順序!
/** * 項(xiàng)目啟動(dòng)后自動(dòng)運(yùn)行的代碼CommandLineRunner */@Component@Order(1)public class MyStartupRunner1 implements CommandLineRunner { private Logger logger = LoggerFactory.getLogger(MyStartupRunner1.class); @Override public void run(String... args) throws Exception { logger.info('MyStartupRunner1里的數(shù)據(jù)'); }}@Component@Order(2)public class MyStartupRunner2 implements CommandLineRunner { private Logger logger = LoggerFactory.getLogger(MyStartupRunner1.class); @Override public void run(String... args) throws Exception { logger.info('MyStartupRunner2里的數(shù)據(jù)'); }}
程序在啟動(dòng)之后,可以看到控制臺(tái)的日志,它們被執(zhí)行了。
2020-05-26 10:25:07.400 INFO 27788 --- [ main] .d.s.w.r.o.CachingOperationNameGenerator : Generating unique operation named: getUsingGET_12020-05-26 10:25:07.433 INFO 27788 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ’’2020-05-26 10:25:07.435 INFO 27788 --- [ main] com.lind.basic.BasicApplication : Started BasicApplication in 6.967 seconds (JVM running for 8.118)2020-05-26 10:25:07.437 INFO 27788 --- [ main] com.lind.basic.init.MyStartupRunner1 : MyStartupRunner1里的數(shù)據(jù)2020-05-26 10:25:07.437 INFO 27788 --- [ main] com.lind.basic.init.MyStartupRunner1 : MyStartupRunner2里的數(shù)據(jù)
總結(jié)
到此這篇關(guān)于springboot CommandLineRunner接口實(shí)現(xiàn)自動(dòng)任務(wù)加載的文章就介紹到這了,更多相關(guān)spring boot CommandLineRunner現(xiàn)自動(dòng)任務(wù)加載內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 利用單元測(cè)試對(duì)PHP代碼進(jìn)行檢查2. python如何實(shí)現(xiàn)word批量轉(zhuǎn)HTML3. python excel和yaml文件的讀取封裝4. moment轉(zhuǎn)化時(shí)間戳出現(xiàn)Invalid Date的問題及解決5. Java8內(nèi)存模型PermGen Metaspace實(shí)例解析6. python3實(shí)現(xiàn)往mysql中插入datetime類型的數(shù)據(jù)7. python爬蟲實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊8. python 實(shí)現(xiàn)圍棋游戲(純tkinter gui)9. python 基于Appium控制多設(shè)備并行執(zhí)行10. Python加載數(shù)據(jù)的5種不同方式(收藏)
