SpringBoot啟動后執(zhí)行方法的五種實(shí)現(xiàn)方式
在 SpringBoot 工程 啟動后, 執(zhí)行方法的五種方式:
1、實(shí)現(xiàn) CommandLineRunner 接口
項(xiàng)目初始化完畢后,才會調(diào)用方法,提供服務(wù)
@Component
public class StartInit2 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("CommandLineRunner====================");
}
}2、實(shí)現(xiàn) ApplicationRunner 接口
同 CommandLineRunner。只是傳參格式不一樣。CommandLineRunner:沒有任何限制;ApplicationRunner:key-value
@Component
public class StartInit3 implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) {
System.out.println("ApplicationRunner=================");
}
}3、實(shí)現(xiàn) ApplicationListener 接口
項(xiàng)目初始化完畢后,才會調(diào)用方法,提供服務(wù)。注意監(jiān)聽的事件,通常是 ApplicationStartedEvent 或者 ApplicationReadyEvent,其他的事件可能無法注入 bean。
@Component
public class StartInit4 implements ApplicationListener<ApplicationStartedEvent> {
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
System.out.println("ApplicationListener================ApplicationStartedEvent");
}
}- 如果監(jiān)聽的是
ApplicationStartedEvent事件,則 ApplicationListener 一定會在 CommandLineRunner 和 ApplicationRunner 之前執(zhí)行; - 如果監(jiān)聽的是
ApplicationReadyEvent事件,則 ApplicationListener 一定會在 CommandLineRunner 和 ApplicationRunner 之后執(zhí)行;
順序:
默認(rèn)是 ApplicationRunner 先執(zhí)行,如果雙方指定了@Order 則按照 @Order的大小順序執(zhí)行,小的先執(zhí)行。
原理:
- SpringApplication 的run方法會執(zhí)行afterRefresh方法。
- afterRefresh方法會執(zhí)行callRunners方法。
- callRunners方法會調(diào)用所有實(shí)現(xiàn)ApplicationRunner和CommondLineRunner接口的方法callRunners方法會調(diào)用所有實(shí)現(xiàn)ApplicationRunner和CommondLineRunner接口的方法
4、@PostConstruct 注解
在項(xiàng)目初始化過程中,就會調(diào)用此方法。如果業(yè)務(wù)邏輯執(zhí)行很耗時,可能會導(dǎo)致項(xiàng)目啟動失敗。
@Component
public class StartInit {
@PostConstruct
public void init() {
System.out.println("@PostConstruct===============================");
}
}5、實(shí)現(xiàn) InitializingBean 接口
項(xiàng)目啟動時,調(diào)用此方法
@Component
public class StartInit6 implements InitializingBean {
@Override
public void afterPropertiesSet() {
System.out.println("InitializingBean====================");
}
}總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot構(gòu)建docker鏡像并推送到阿里云
本文主要介紹了springboot構(gòu)建docker鏡像并推送到阿里云,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
IntelliJ IDEA 2020 安裝和常用配置(推薦)
這篇文章主要介紹了IntelliJ IDEA 2020 安裝和常用配置(推薦),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Java中List.contains(Object?object)方法使用
本文主要介紹了Java中List.contains(Object?object)方法,使用List.contains(Object?object)方法判斷ArrayList是否包含一個元素對象,感興趣的可以了解一下2022-04-04
如何使用HttpClient發(fā)送java對象到服務(wù)器
這篇文章主要介紹了如何使用HttpClient發(fā)送java對象到服務(wù)器,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11
使用springboot aop來實(shí)現(xiàn)讀寫分離和事物配置
這篇文章主要介紹了使用springboot aop來實(shí)現(xiàn)讀寫分離和事物配置,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
idea查看properties中文變成unicode碼的解決方案
這篇文章主要介紹了idea查看properties中文變成unicode碼的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06

