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í)行順序內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- springboot中@PostConstruct注解使用小結
- SpringBoot中@PostConstruct 注解的實現(xiàn)
- springboot啟動加載CommandLineRunner @PostConstruct問題
- SpringBoot中的@PostConstruct注解詳細解析
- SpringBoot使用@PostConstruct注解導入配置方式
- springboot?@PostConstruct無效的解決
- 淺談SpringBoot中的Bean初始化方法?@PostConstruct
- SpringBoot @PostConstruct和@PreDestroy的使用說明
- SpringBoot @PostConstruct原理用法解析
相關文章
spring-data-jpa+Alibaba Druid多數(shù)據(jù)源案例實踐
本文介紹基于Spring Data JPA與Alibaba Druid配置多數(shù)據(jù)源的方案,通過定義三個獨立數(shù)據(jù)源、分別設置JPA配置及實體/倉庫包,實現(xiàn)訂單、用戶、詳情表的分離管理2025-07-07
springboot報錯Invalid?bound?statement?(not?found)的解決
本文主要介紹了springboot報錯Invalid?bound?statement?(not?found)的解決,遇到這種問題通常是沒有配置好配置文件,下面就來具體介紹一下解決方法,感興趣的可以了解一下2025-03-03
IDEA maven compile報錯OutOfMemoryError(內存溢出)解決及jvm分析
遇到Maven編譯時報OutOfMemoryError錯誤通常因為默認的堆內存大小不足,本文就來介紹一下OutOfMemoryError(內存溢出)解決,具有一定的參考價值,感興趣的可以了解一下2024-10-10
JAVA利用順序表實現(xiàn)“楊輝三角”的思路及代碼示例
楊輝三角形是中國古代數(shù)學的杰出研究成果之一,是我國北宋數(shù)學家賈憲于1050年首先發(fā)現(xiàn)并使用的,這篇文章主要介紹了JAVA利用順序表實現(xiàn)楊輝三角的思路及代碼示例,需要的朋友可以參考下2025-01-01

