Springboot啟動(dòng)后執(zhí)行方法小結(jié)
一、注解@PostConstruct
使用注解@PostConstruct是最常見的一種方式,存在的問題是如果執(zhí)行的方法耗時(shí)過長,會(huì)導(dǎo)致項(xiàng)目在方法執(zhí)行期間無法提供服務(wù)。
@Component public class StartInit { // // ? ?@Autowired ? 可以注入bean // ? ?ISysUserService userService; ? ? @PostConstruct ? ? public void init() throws InterruptedException { ? ? ? ? Thread.sleep(10*1000);//這里如果方法執(zhí)行過長會(huì)導(dǎo)致項(xiàng)目一直無法提供服務(wù) ? ? ? ? System.out.println(123456); ? ? } }
二、CommandLineRunner接口
實(shí)現(xiàn)CommandLineRunner接口 然后在run方法里面調(diào)用需要調(diào)用的方法即可,好處是方法執(zhí)行時(shí),項(xiàng)目已經(jīng)初始化完畢,是可以正常提供服務(wù)的。
同時(shí)該方法也可以接受參數(shù),可以根據(jù)項(xiàng)目啟動(dòng)時(shí): java -jar demo.jar arg1 arg2 arg3 傳入的參數(shù)進(jìn)行一些處理。
@Component public class CommandLineRunnerImpl implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println(Arrays.toString(args)); } }
三、實(shí)現(xiàn)ApplicationRunner接口
實(shí)現(xiàn)ApplicationRunner接口和實(shí)現(xiàn)CommandLineRunner接口基本是一樣的。
唯一的不同是啟動(dòng)時(shí)傳參的格式,CommandLineRunner對于參數(shù)格式?jīng)]有任何限制,ApplicationRunner接口參數(shù)格式必須是:–key=value
@Component public class ApplicationRunnerImpl implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { Set<String> optionNames = args.getOptionNames(); for (String optionName : optionNames) { List<String> values = args.getOptionValues(optionName); System.out.println(values.toString()); } } }
四、實(shí)現(xiàn)ApplicationListener
實(shí)現(xiàn)接口ApplicationListener方式和實(shí)現(xiàn)ApplicationRunner,CommandLineRunner接口都不影響服務(wù),都可以正常提供服務(wù),注意監(jiān)聽的事件,通常是ApplicationStartedEvent 或者ApplicationReadyEvent,其他的事件可能無法注入bean。
@Component public class ApplicationListenerImpl implements ApplicationListener<ApplicationStartedEvent> { @Override public void onApplicationEvent(ApplicationStartedEvent event) { System.out.println("listener"); } }
五、四種方式的執(zhí)行順序
注解方式@PostConstruct 始終最先執(zhí)行
如果監(jiān)聽的是ApplicationStartedEvent 事件,則一定會(huì)在CommandLineRunner和ApplicationRunner 之前執(zhí)行。
如果監(jiān)聽的是ApplicationReadyEvent 事件,則一定會(huì)在CommandLineRunner和ApplicationRunner 之后執(zhí)行。
CommandLineRunner和ApplicationRunner 默認(rèn)是ApplicationRunner先執(zhí)行,如果雙方指定了@Order 則按照@Order的大小順序執(zhí)行,大的先執(zhí)行。
到此這篇關(guān)于Springboot啟動(dòng)后執(zhí)行方法小結(jié)的文章就介紹到這了,更多相關(guān)Springboot啟動(dòng)后執(zhí)行內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中實(shí)現(xiàn)WebSocket方法詳解
這篇文章主要介紹了Java中實(shí)現(xiàn)WebSocket方法詳解,WebSocket?是一種新型的網(wǎng)絡(luò)協(xié)議,它允許客戶端和服務(wù)器之間進(jìn)行雙向通信,可以實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)交互,需要的朋友可以參考下2023-07-07Java屬性文件操作之Properties與ResourceBundle詳解
這篇文章主要介紹了Java屬性文件操作之Properties與ResourceBundle詳解,兩個(gè)類都可以讀取屬性文件中以key/value形式存儲的鍵值對,ResourceBundle讀取屬性文件時(shí)操作相對簡單,需要的朋友可以參考下2023-11-11Java面試崗常見問題之ArrayList和LinkedList的區(qū)別
ArrayList和LinkedList作為我們Java中最常使用的集合類,很多人在被問到他們的區(qū)別時(shí),憋了半天僅僅冒出一句:一個(gè)是數(shù)組一個(gè)是鏈表。這樣回答簡直讓面試官吐血。為了讓兄弟們打好基礎(chǔ),我們通過實(shí)際的使用測試,好好說一下ArrayList和LinkedList的區(qū)別這道經(jīng)典的面試題2022-01-01