淺談SpringBoot中的Bean初始化方法?@PostConstruct
注解說明
- 使用注解: @PostConstruct
- 效果:在Bean初始化之后(構(gòu)造方法和@Autowired之后)執(zhí)行指定操作。經(jīng)常用在將構(gòu)造方法中的動作延遲。
- 備注:Bean初始化時候的執(zhí)行順序: 構(gòu)造方法 -> @Autowired -> @PostConstruct
代碼示例
注解示例
@Component
public class PostConstructTest1 {
@Autowired
PostConstructTest2 postConstructTest2;
public PostConstructTest1() {
// postConstructTest2.hello();
}
@PostConstruct
public void init() {
// some init function
}
}
在Bean的初始化操作中,有時候會遇到調(diào)用其他Bean的時候報空指針錯誤。這時候就可以將調(diào)用另一個Bean的方法這個操作放到@PostConstruct注解的方法中,將其延遲執(zhí)行。
錯誤示例
@Component
public class PostConstructTest1 {
@Autowired
PostConstructTest2 postConstructTest2;
public PostConstructTest1() {
postConstructTest2.hello();
}
}
@Component
public class PostConstructTest2 {
public void hello() {
System.out.println("hello, i am PostConstructTest2");
}
}

正確示例
@Component
public class PostConstructTest1 {
@Autowired
PostConstructTest2 postConstructTest2;
public PostConstructTest1() {
postConstructTest2.hello();
}
}
@Component
public class PostConstructTest1 {
@Autowired
PostConstructTest2 postConstructTest2;
public PostConstructTest1() {
// postConstructTest2.hello();
}
@PostConstruct
public void init() {
postConstructTest2.hello();
}
}

SpringBoot @PostConstruct雖好,也要慎用
做過SpringBoot開發(fā)的話,肯定對@PostConstruct比較熟悉。在一個Bean組件中,標記了@PostConstruct的方法會在Bean構(gòu)造完成后自動執(zhí)行方法的邏輯。
1 問題的產(chǎn)生
先說下SpringBoot中Bean的加載過程,簡單點說就是SpringBoot會把標記了Bean相關(guān)注解(例如@Component、@Service、@Repository等)的類或接口自動初始化全局的單一實例,如果標記了初始化順序會按照用戶標記的順序,否則按照默認順序初始化。在初始化的過程中,執(zhí)行完一個Bean的構(gòu)造方法后會執(zhí)行該Bean的@PostConstruct方法(如果有),然后初始化下一個Bean。
那么: 如果@PostConstruct方法內(nèi)的邏輯處理時間較長,就會增加SpringBoot應用初始化Bean的時間,進而增加應用啟動的時間。因為只有在Bean初始化完成后,SpringBoot應用才會打開端口提供服務,所以在此之前,應用不可訪問。
2 案例模擬
為了模擬上面說的情況,在SpringBoot項目中建兩個組件類ComponentOne和ComponentTwo。耗時的初始化邏輯放在ComponentOne中,并設(shè)置ComponentOne的初始化順序在ComponentTwo之前。完整代碼如下:
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ComponentOne {
private Logger logger = LoggerFactory.getLogger(this.getClass());
public ComponentOne() {
this.logger.info("ComponentOne 初始化完成");
}
@PostConstruct
public void init() {
this.logger.info("ComponentOne 模擬耗時邏輯開始");
try {
//這里休眠5秒模擬耗時邏輯
Thread.sleep(1000 * 5);
} catch (InterruptedException e) {
logger.info("模擬邏輯耗時失敗", e);
}
this.logger.info("ComponentOne 模擬耗時邏輯完成");
}
}
@Component
@Order(Ordered.HIGHEST_PRECEDENCE + 1)
public class ComponentTwo {
private Logger logger = LoggerFactory.getLogger(this.getClass());
public ComponentTwo() {
this.logger.info("ComponentTwo 初始化完成");
}
@PostConstruct
public void init() {
this.logger.info("ComponentTwo 初始化完成后處理");
}
}
啟動應用,初始化部分日志如下:

3 總結(jié)
所以,如果應用有一些初始化操作,有以下幾點建議:
- 輕量的邏輯可放在Bean的@PostConstruct方法中
- 耗時長的邏輯如果放在@PostConstruct方法中,可使用獨立線程執(zhí)行
- 初始化操作放在CommandLineRunner或ApplicationRunner的實現(xiàn)組件中
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- springboot中@PostConstruct注解使用小結(jié)
- SpringBoot中@PostConstruct 注解的實現(xiàn)
- springboot啟動加載CommandLineRunner @PostConstruct問題
- SpringBoot中的@PostConstruct注解詳細解析
- SpringBoot使用@PostConstruct注解導入配置方式
- springboot?@PostConstruct無效的解決
- SpringBoot @PostConstruct和@PreDestroy的使用說明
- SpringBoot @PostConstruct原理用法解析
- SpringBoot中多個PostConstruct注解執(zhí)行順序控制
相關(guān)文章
java數(shù)據(jù)結(jié)構(gòu)與算法之希爾排序詳解
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)與算法之希爾排序,結(jié)合實例形式分析了希爾排序的概念、原理、實現(xiàn)方法與相關(guān)注意事項,需要的朋友可以參考下2017-05-05
Java如何將BigDecimal類型的值轉(zhuǎn)成double類型
這篇文章主要給大家介紹了關(guān)于Java如何將BigDecimal類型的值轉(zhuǎn)成double類型的相關(guān)資料,需要注意精度損失和范圍限制,使用doubleValue方法進行轉(zhuǎn)換,并在高精度計算時格外小心,需要的朋友可以參考下2024-12-12
SpringBoot 策略模式實現(xiàn)切換上傳文件模式
策略模式是指有一定行動內(nèi)容的相對穩(wěn)定的策略名稱,這篇文章主要介紹了SpringBoot 策略模式 切換上傳文件模式,需要的朋友可以參考下2023-11-11
SpringMVC中@RequestMapping注解的實現(xiàn)
RequestMapping是一個用來處理請求地址映射的注解,本文主要介紹了SpringMVC中@RequestMapping注解的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-01-01
java使用htmlparser提取網(wǎng)頁純文本例子
這篇文章主要介紹了java使用htmlparser提取網(wǎng)頁純文本例子,需要的朋友可以參考下2014-04-04

