淺談SpringBoot中的Bean初始化方法?@PostConstruct
注解說(shuō)明
- 使用注解: @PostConstruct
- 效果:在Bean初始化之后(構(gòu)造方法和@Autowired之后)執(zhí)行指定操作。經(jīng)常用在將構(gòu)造方法中的動(dòng)作延遲。
- 備注:Bean初始化時(shí)候的執(zhí)行順序: 構(gòu)造方法 -> @Autowired -> @PostConstruct
代碼示例
注解示例
@Component
public class PostConstructTest1 {
@Autowired
PostConstructTest2 postConstructTest2;
public PostConstructTest1() {
// postConstructTest2.hello();
}
@PostConstruct
public void init() {
// some init function
}
}
在Bean的初始化操作中,有時(shí)候會(huì)遇到調(diào)用其他Bean的時(shí)候報(bào)空指針錯(cuò)誤。這時(shí)候就可以將調(diào)用另一個(gè)Bean的方法這個(gè)操作放到@PostConstruct注解的方法中,將其延遲執(zhí)行。
錯(cuò)誤示例
@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雖好,也要慎用
做過(guò)SpringBoot開(kāi)發(fā)的話,肯定對(duì)@PostConstruct比較熟悉。在一個(gè)Bean組件中,標(biāo)記了@PostConstruct的方法會(huì)在Bean構(gòu)造完成后自動(dòng)執(zhí)行方法的邏輯。
1 問(wèn)題的產(chǎn)生
先說(shuō)下SpringBoot中Bean的加載過(guò)程,簡(jiǎn)單點(diǎn)說(shuō)就是SpringBoot會(huì)把標(biāo)記了Bean相關(guān)注解(例如@Component、@Service、@Repository等)的類(lèi)或接口自動(dòng)初始化全局的單一實(shí)例,如果標(biāo)記了初始化順序會(huì)按照用戶(hù)標(biāo)記的順序,否則按照默認(rèn)順序初始化。在初始化的過(guò)程中,執(zhí)行完一個(gè)Bean的構(gòu)造方法后會(huì)執(zhí)行該Bean的@PostConstruct方法(如果有),然后初始化下一個(gè)Bean。
那么: 如果@PostConstruct方法內(nèi)的邏輯處理時(shí)間較長(zhǎng),就會(huì)增加SpringBoot應(yīng)用初始化Bean的時(shí)間,進(jìn)而增加應(yīng)用啟動(dòng)的時(shí)間。因?yàn)橹挥性贐ean初始化完成后,SpringBoot應(yīng)用才會(huì)打開(kāi)端口提供服務(wù),所以在此之前,應(yīng)用不可訪問(wèn)。
2 案例模擬
為了模擬上面說(shuō)的情況,在SpringBoot項(xiàng)目中建兩個(gè)組件類(lèi)ComponentOne和ComponentTwo。耗時(shí)的初始化邏輯放在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 模擬耗時(shí)邏輯開(kāi)始");
try {
//這里休眠5秒模擬耗時(shí)邏輯
Thread.sleep(1000 * 5);
} catch (InterruptedException e) {
logger.info("模擬邏輯耗時(shí)失敗", e);
}
this.logger.info("ComponentOne 模擬耗時(shí)邏輯完成");
}
}
@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 初始化完成后處理");
}
}
啟動(dòng)應(yīng)用,初始化部分日志如下:

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

