Springboot啟動后立即某個執(zhí)行方法的四種方式
最新需要在項目啟動后立即執(zhí)行某個方法,然后特此記錄下找到的四種方式
注解@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ù)進(jìn)行一些處理。詳見: Spring boot CommandLineRunner啟動任務(wù)傳參
@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 默認(rèn)是ApplicationRunner先執(zhí)行,如果雙方指定了@Order 則按照@Order的大小順序執(zhí)行,大的先執(zhí)行。
總結(jié)
到此這篇關(guān)于Springboot啟動后立即某個執(zhí)行方法的四種方式的文章就介紹到這了,更多相關(guān)Springboot啟動后執(zhí)行方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 詳解SpringBoot啟動項目后執(zhí)行方法的幾種方式
- SpringBoot項目啟動執(zhí)行任務(wù)的多種方法小結(jié)
- SpringBoot實現(xiàn)啟動項目后立即執(zhí)行的方法總結(jié)
- springboot項目啟動后執(zhí)行方法的三種方式
- SpringBoot啟動時自動執(zhí)行代碼的幾種實現(xiàn)方式
- springboot啟動前執(zhí)行方法的四種方式總結(jié)
- Springboot啟動執(zhí)行特定代碼的方式匯總
- springboot 項目容器啟動后如何自動執(zhí)行指定方法
- Springboot項目啟動成功后可通過五種方式繼續(xù)執(zhí)行
相關(guān)文章
解決mybatis-plus使用jdk8的LocalDateTime 查詢時報錯的方法
這篇文章主要介紹了解決mybatis-plus使用jdk8的LocalDateTime 查詢時報錯的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
簡單了解Spring beanfactory循環(huán)依賴命名重復(fù)屬性
這篇文章主要介紹了簡單了解Spring beanfactory循環(huán)依賴命名重復(fù)2大屬性,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
Springboot整合mybatis-plus使用pageHelper進(jìn)行分頁(使用步驟)
PageHelper是一個MyBatis分頁插件,可以方便地實現(xiàn)數(shù)據(jù)庫查詢結(jié)果的分頁功能,在Maven或Gradle項目中引入依賴,并在配置文件中進(jìn)行配置,本文給大家介紹Springboot整合mybatis-plus使用pageHelper進(jìn)行分頁,感興趣的朋友跟隨小編一起看看吧2024-11-11
Java數(shù)據(jù)結(jié)構(gòu)中圖的進(jìn)階詳解
在Java學(xué)習(xí)與應(yīng)用中,數(shù)據(jù)結(jié)構(gòu)無疑是每個人都要接觸的難點,為了更好的學(xué)習(xí)數(shù)據(jù)結(jié)構(gòu)這一塊內(nèi)容,用圖來理解便是最好的方式,讓我們一起來了解本篇內(nèi)容圖的進(jìn)階2022-01-01

