計時器介紹
啟動加載器實戰
實現方式1
- 實現
CommandLineRunner
接口 - 重寫run方法
- 通過Order進行排序
示例:
@Component
@Order(1)
public class FirstCommandlineRunner implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("\u001B[32m >>> startup first runner<<<");}
}
實現方式2
- 實現
ApplicationRunner
接口 - 重寫run方法
- 通過order排序
@Component
@Order(1)
public class FirstApplicationRunner implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) throws Exception {System.out.println("\u001B[32m >>> startup first application runner<<<");}
}
注意:
- 通過order值指定順序
- order值相同ApplicationRunner實現優先
啟動加載器原理解析
callRunners實現
面試題
- SpringBoot定時器的實現?它有哪些優點?
- 讓你去設計實現一個計時器,你的思路?
- 怎么實現在Spring Boot啟動后執行程序?
-
- 通過啟動加載器進行實現
- 啟動加載器如何實現?
- 啟動加載器的實現有什么異同點?
- 啟動加載器的調用時機?