SpringBoot中多個PostConstruct注解執(zhí)行順序控制
項目場景:
多個類中使用@PostConstruct加載先后順序
問題描述
有時候Class A中@PostConstruct注解的方法中的代碼執(zhí)行,需要等待Class B中@PostConstruct 注解方法中的代碼執(zhí)行完后,拿到結果,才能執(zhí)行,也就是中A中某些代碼的執(zhí)行需要依賴B中代碼執(zhí)后的結果,此時就需要B先執(zhí)行完,再執(zhí)行A,
解決方案:
方式一:可以在A中先注入B,那么就會先加載B
@Service
@DependsOn("b")
public class A{
@PostConstruct
public void init() {
System.out.println("A Bean init method called");
}
}@Service
public class B{
@PostConstruct
public void init() {
System.out.println("B Bean init method called");
}
}方式二:使用@Order注解
@Service
@Order(2) // 指定執(zhí)行順序為2
public class A{
@PostConstruct
public void init() {
System.out.println("A Bean init method called");
}
}@Service
@Order(1) // 指定執(zhí)行順序為1
public class B{
@PostConstruct
public void init() {
System.out.println("B Bean init method called");
}
}@Order 值較小的 bean先執(zhí)行
到此這篇關于SpringBoot中多個PostConstruct注解執(zhí)行順序控制的文章就介紹到這了,更多相關SpringBoot PostConstruct 執(zhí)行順序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- springboot中@PostConstruct注解使用小結
- SpringBoot中@PostConstruct 注解的實現(xiàn)
- springboot啟動加載CommandLineRunner @PostConstruct問題
- SpringBoot中的@PostConstruct注解詳細解析
- SpringBoot使用@PostConstruct注解導入配置方式
- springboot?@PostConstruct無效的解決
- 淺談SpringBoot中的Bean初始化方法?@PostConstruct
- SpringBoot @PostConstruct和@PreDestroy的使用說明
- SpringBoot @PostConstruct原理用法解析
相關文章
knife4j3.0.3整合gateway和注冊中心的詳細過程
這篇文章主要介紹了knife4j3.0.3整合gateway和注冊中心的詳細過程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
java的Object里wait()實現(xiàn)原理講解
這篇文章主要介紹了java的Object里wait()實現(xiàn)原理,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
SpringBoot實現(xiàn)動態(tài)定時任務的示例代碼
在SpringBoot項目中簡單使用定時任務,不過由于要借助cron表達式且都提前定義好放在配置文件里,不能在項目運行中動態(tài)修改任務執(zhí)行時間,實在不太靈活?,F(xiàn)在我們就來實現(xiàn)可以動態(tài)修改cron表達式的定時任務,感興趣的可以了解一下2022-10-10

