SpringBoot @PostConstruct和@PreDestroy的使用說明
1. @PostConstruct
1.1 概述
@PostConstruct標記在方法上,在當前類的實例加入到容器之前,會先執(zhí)行@PostConstruct標記的方法。它的執(zhí)行順序是這樣的:
- 先執(zhí)行當前類的構(gòu)造函數(shù)
- 然后執(zhí)行@Autowired標記對象的初始化
- 最后執(zhí)行@PostConstruct標記的方法
- 如果沒有拋出異常,則該對象加入Spring管理容器
1.2 驗證執(zhí)行順序
創(chuàng)建一個空的Spring Boot項目,這步不演示了
創(chuàng)建TestComponent
@Component
public class TestComponent {
public TestComponent(){
System.out.println("TestComponent 構(gòu)造函數(shù)");
}
@PostConstruct
public void init(){
System.out.println("TestComponent PostConstruct");
}
}
創(chuàng)建MyService
public interface MyService {
void Hello(String name);
}
創(chuàng)建MyServiceImpl
@Service
public class MyServiceImpl implements MyService {
public MyServiceImpl(){
System.out.println("MyServiceImpl 構(gòu)造函數(shù)");
}
@PostConstruct
public void init(){
System.out.println("MyServiceImpl PostConstruct");
}
@Override
public void Hello(String name) {
System.out.println("Hello " + name);
}
}
運行項目,看下輸出結(jié)果。
TestComponent和MyServiceImpl分別獨自初始化,構(gòu)造函數(shù)優(yōu)先執(zhí)行
請記住這個執(zhí)行順序
TestComponent 構(gòu)造函數(shù)
TestComponent PostConstruct
MyServiceImpl 構(gòu)造函數(shù)
MyServiceImpl PostConstruct
還沒完,改一下TestComponent,加入引用MyService
@Autowired
private MyService myService;
再執(zhí)行一下,看看結(jié)果有什么變化
TestComponent 構(gòu)造函數(shù)
MyServiceImpl 構(gòu)造函數(shù)
MyServiceImpl PostConstruct
TestComponent PostConstruct
MyServiceImpl執(zhí)行順序往前移動了,證明@Autowired順序在@PostConstruct之前
因此,如果要在TestComponent初始化的時候調(diào)用MyService方法,要寫在@PostConstruct內(nèi)部,不能在構(gòu)造函數(shù)處理(這時候MyServiceImpl還沒初始化,會拋出空指針異常)
@Component
public class TestComponent {
@Autowired
private MyService myService;
public TestComponent(){
System.out.println("TestComponent 構(gòu)造函數(shù)");
//寫在這里必定拋出異常,此時 myService == null
//myService.Hello("張三");
}
@PostConstruct
public void init(){
System.out.println("TestComponent PostConstruct");
//在這里調(diào)用MySerice方法才正確
myService.Hello("張三");
}
}
2. @PreDestroy
首先,來看下Java Doc對這個注解的說明

1: 在對象實例被容器移除的時候,會回調(diào)調(diào)用@PreDestroy標記的方法
2: 通常用來釋放該實例占用的資源
修改上面的TestComponent代碼,加上@PreDestroy代碼
@PreDestroy
public void destroy(){
System.out.println("TestComponent 對象被銷毀了");
}
修改Application main方法,啟動10秒后退出程序
@SpringBootApplication
public class AnnotationTestApplication {
public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(AnnotationTestApplication.class, args);
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ctx.close();
}
}
啟動程序,查看輸出信息
程序退出時會銷毀對象,所以會觸發(fā)我們剛寫的@PreDestroy方法,測試通過。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- springboot中@PostConstruct注解使用小結(jié)
- SpringBoot中@PostConstruct 注解的實現(xiàn)
- springboot啟動加載CommandLineRunner @PostConstruct問題
- SpringBoot中的@PostConstruct注解詳細解析
- SpringBoot使用@PostConstruct注解導入配置方式
- springboot?@PostConstruct無效的解決
- 淺談SpringBoot中的Bean初始化方法?@PostConstruct
- SpringBoot @PostConstruct原理用法解析
- SpringBoot中多個PostConstruct注解執(zhí)行順序控制
相關(guān)文章
Spring Boot Maven Plugin打包異常解決方案
這篇文章主要介紹了Spring Boot Maven Plugin打包異常解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-11-11
解決mybatis三表連接查詢數(shù)據(jù)重復的問題
這篇文章主要介紹了解決mybatis三表連接查詢數(shù)據(jù)重復的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
Java中HttpServletResponse響應中文出現(xiàn)亂碼問題
這篇文章主要介紹了Java中HttpServletResponse響應中文出現(xiàn)亂碼問題的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-06-06
一文帶你理解@RefreshScope注解實現(xiàn)動態(tài)刷新原理
RefeshScope這個注解想必大家都用過,在微服務配置中心的場景下經(jīng)常出現(xiàn),他可以用來刷新Bean中的屬性配置,那大家對他的實現(xiàn)原理了解嗎,它為什么可以做到動態(tài)刷新呢,所以本文小編將給大家詳細介紹@RefreshScope注解實現(xiàn)動態(tài)刷新原理2023-07-07
Spring實戰(zhàn)之獲取其他Bean的屬性值操作示例
這篇文章主要介紹了Spring實戰(zhàn)之獲取其他Bean的屬性值操作,結(jié)合實例形式分析了Spring操作Bean屬性值的相關(guān)配置與實現(xiàn)技巧,需要的朋友可以參考下2019-12-12
Java分支結(jié)構(gòu)和循環(huán)結(jié)構(gòu)原理與用法詳解
這篇文章主要介紹了Java分支結(jié)構(gòu)和循環(huán)結(jié)構(gòu)原理與用法,結(jié)合實例形式分析了java分支結(jié)構(gòu)、循環(huán)結(jié)構(gòu)、跳轉(zhuǎn)語句等相關(guān)概念、原理、使用技巧與操作注意事項,需要的朋友可以參考下2020-02-02

