Springboot啟動后執(zhí)行方法小結(jié)
一、注解@PostConstruct
使用注解@PostConstruct是最常見的一種方式,存在的問題是如果執(zhí)行的方法耗時過長,會導(dǎo)致項目在方法執(zhí)行期間無法提供服務(wù)。
@Component
public class StartInit {
//
// ? ?@Autowired ? 可以注入bean
// ? ?ISysUserService userService;
? ? @PostConstruct
? ? public void init() throws InterruptedException {
? ? ? ? Thread.sleep(10*1000);//這里如果方法執(zhí)行過長會導(dǎo)致項目一直無法提供服務(wù)
? ? ? ? System.out.println(123456);
? ? }
}二、CommandLineRunner接口
實現(xiàn)CommandLineRunner接口 然后在run方法里面調(diào)用需要調(diào)用的方法即可,好處是方法執(zhí)行時,項目已經(jīng)初始化完畢,是可以正常提供服務(wù)的。
同時該方法也可以接受參數(shù),可以根據(jù)項目啟動時: java -jar demo.jar arg1 arg2 arg3 傳入的參數(shù)進行一些處理。
@Component
public class CommandLineRunnerImpl implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println(Arrays.toString(args));
}
}
三、實現(xiàn)ApplicationRunner接口
實現(xiàn)ApplicationRunner接口和實現(xiàn)CommandLineRunner接口基本是一樣的。
唯一的不同是啟動時傳參的格式,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());
}
}
}
四、實現(xiàn)ApplicationListener
實現(xiàn)接口ApplicationListener方式和實現(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 事件,則一定會在CommandLineRunner和ApplicationRunner 之前執(zhí)行。
如果監(jiān)聽的是ApplicationReadyEvent 事件,則一定會在CommandLineRunner和ApplicationRunner 之后執(zhí)行。
CommandLineRunner和ApplicationRunner 默認是ApplicationRunner先執(zhí)行,如果雙方指定了@Order 則按照@Order的大小順序執(zhí)行,大的先執(zhí)行。
到此這篇關(guān)于Springboot啟動后執(zhí)行方法小結(jié)的文章就介紹到這了,更多相關(guān)Springboot啟動后執(zhí)行內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Caused?by:?java.lang.NumberFormatException:?For?input?s
這篇文章主要介紹了Caused?by:?java.lang.NumberFormatException:?For?input?string:?“port“,本文給大家分享完美解決方法,需要的朋友可以參考下2023-01-01
Java程序順序結(jié)構(gòu)中邏輯控制語句詳解流程
在程序開發(fā)的過程之中一共會存在有三種程序邏輯:順序結(jié)構(gòu)、分支結(jié)構(gòu)、循環(huán)結(jié)構(gòu),對于之前所編寫的代碼大部分都是順序結(jié)構(gòu)的定義,即:所有的程序?qū)凑斩x的代碼順序依次執(zhí)行2021-10-10
list,set,map,數(shù)組之間的相互轉(zhuǎn)換詳細解析
以下是對Java中l(wèi)ist,set,map,數(shù)組之間的相互轉(zhuǎn)換進行了詳細的分析介紹,需要的朋友可以過來參考下2013-09-09
Spring中XmlWebApplicationContext的實現(xiàn)
XmlWebApplicationContext是Spring?Framework中的一個重要類,本文主要介紹了Spring中XmlWebApplicationContext,具有一定的參考價值,感興趣的可以了解一下2024-08-08
SpringBoot通過JSON傳遞請求參數(shù)的實例詳解
這篇文章主要介紹了SpringBoot通過JSON傳遞請求參數(shù),示例介紹SpringMVC如何通過JSON格式傳遞入?yún)?,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-11-11

