SpringBoot @PostConstruct和@PreDestroy的使用說明
1. @PostConstruct
1.1 概述
@PostConstruct標(biāo)記在方法上,在當(dāng)前類的實(shí)例加入到容器之前,會(huì)先執(zhí)行@PostConstruct標(biāo)記的方法。它的執(zhí)行順序是這樣的:
- 先執(zhí)行當(dāng)前類的構(gòu)造函數(shù)
- 然后執(zhí)行@Autowired標(biāo)記對(duì)象的初始化
- 最后執(zhí)行@PostConstruct標(biāo)記的方法
- 如果沒有拋出異常,則該對(duì)象加入Spring管理容器
1.2 驗(yàn)證執(zhí)行順序
創(chuàng)建一個(gè)空的Spring Boot項(xiàng)目,這步不演示了
創(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); } }
運(yùn)行項(xiàng)目,看下輸出結(jié)果。
TestComponent和MyServiceImpl分別獨(dú)自初始化,構(gòu)造函數(shù)優(yōu)先執(zhí)行
請(qǐng)記住這個(gè)執(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í)行順序往前移動(dòng)了,證明@Autowired順序在@PostConstruct之前
因此,如果要在TestComponent初始化的時(shí)候調(diào)用MyService方法,要寫在@PostConstruct內(nèi)部,不能在構(gòu)造函數(shù)處理(這時(shí)候MyServiceImpl還沒初始化,會(huì)拋出空指針異常)
@Component public class TestComponent { @Autowired private MyService myService; public TestComponent(){ System.out.println("TestComponent 構(gòu)造函數(shù)"); //寫在這里必定拋出異常,此時(shí) myService == null //myService.Hello("張三"); } @PostConstruct public void init(){ System.out.println("TestComponent PostConstruct"); //在這里調(diào)用MySerice方法才正確 myService.Hello("張三"); } }
2. @PreDestroy
首先,來看下Java Doc對(duì)這個(gè)注解的說明
1: 在對(duì)象實(shí)例被容器移除的時(shí)候,會(huì)回調(diào)調(diào)用@PreDestroy標(biāo)記的方法
2: 通常用來釋放該實(shí)例占用的資源
修改上面的TestComponent代碼,加上@PreDestroy代碼
@PreDestroy public void destroy(){ System.out.println("TestComponent 對(duì)象被銷毀了"); }
修改Application main方法,啟動(dòng)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(); } }
啟動(dòng)程序,查看輸出信息
程序退出時(shí)會(huì)銷毀對(duì)象,所以會(huì)觸發(fā)我們剛寫的@PreDestroy方法,測(cè)試通過。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- springboot中@PostConstruct注解使用小結(jié)
- SpringBoot中@PostConstruct 注解的實(shí)現(xiàn)
- springboot啟動(dòng)加載CommandLineRunner @PostConstruct問題
- SpringBoot中的@PostConstruct注解詳細(xì)解析
- SpringBoot使用@PostConstruct注解導(dǎo)入配置方式
- springboot?@PostConstruct無效的解決
- 淺談SpringBoot中的Bean初始化方法?@PostConstruct
- SpringBoot @PostConstruct原理用法解析
- SpringBoot中多個(gè)PostConstruct注解執(zhí)行順序控制
相關(guān)文章
Spring Boot Maven Plugin打包異常解決方案
這篇文章主要介紹了Spring Boot Maven Plugin打包異常解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11解決mybatis三表連接查詢數(shù)據(jù)重復(fù)的問題
這篇文章主要介紹了解決mybatis三表連接查詢數(shù)據(jù)重復(fù)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01springBoot接入阿里云oss的實(shí)現(xiàn)步驟
這篇文章主要介紹了springBoot接入阿里云oss的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Java中HttpServletResponse響應(yīng)中文出現(xiàn)亂碼問題
這篇文章主要介紹了Java中HttpServletResponse響應(yīng)中文出現(xiàn)亂碼問題的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06一文帶你理解@RefreshScope注解實(shí)現(xiàn)動(dòng)態(tài)刷新原理
RefeshScope這個(gè)注解想必大家都用過,在微服務(wù)配置中心的場(chǎng)景下經(jīng)常出現(xiàn),他可以用來刷新Bean中的屬性配置,那大家對(duì)他的實(shí)現(xiàn)原理了解嗎,它為什么可以做到動(dòng)態(tài)刷新呢,所以本文小編將給大家詳細(xì)介紹@RefreshScope注解實(shí)現(xiàn)動(dòng)態(tài)刷新原理2023-07-07Spring實(shí)戰(zhàn)之獲取其他Bean的屬性值操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之獲取其他Bean的屬性值操作,結(jié)合實(shí)例形式分析了Spring操作Bean屬性值的相關(guān)配置與實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-12-12Java分支結(jié)構(gòu)和循環(huán)結(jié)構(gòu)原理與用法詳解
這篇文章主要介紹了Java分支結(jié)構(gòu)和循環(huán)結(jié)構(gòu)原理與用法,結(jié)合實(shí)例形式分析了java分支結(jié)構(gòu)、循環(huán)結(jié)構(gòu)、跳轉(zhuǎn)語句等相關(guān)概念、原理、使用技巧與操作注意事項(xiàng),需要的朋友可以參考下2020-02-02