總結(jié)Bean的三種自定義初始化和銷(xiāo)毀方法
Bean三種自定義初始化和銷(xiāo)毀
一. 三種方法概述
在配置類(lèi)中指定 @Bean(initMethod = “init”,destroyMethod = “destory”)注解
實(shí)現(xiàn)InitializingBean接口并重寫(xiě)其afterPropertiesSet方法,實(shí)現(xiàn)DisposableBean接口并重寫(xiě)destroy方法
利用java的JSR250規(guī)范中的@PostConstruct標(biāo)注在init方法上,@PreDestroy標(biāo)注在destroy方法上
二. 方法詳述
1. 方法1:配置類(lèi)中指定
示例代碼
public class CarA { public CarA() { System.out.println("CarA。。。構(gòu)造函數(shù)"); } public void initCarA(){ System.out.println("CarA的init()方法"); } public void destroyCarA(){ System.out.println("CarA的destroy()方法"); } } @Configuration public class ConfigTest { @Bean(initMethod = "initCarA",destroyMethod = "destroyCarA") public CarA carA(){ return new CarA(); } }
執(zhí)行結(jié)果
CarA。。。構(gòu)造函數(shù)
CarA的init()方法服務(wù)啟動(dòng)
CarA的destroy()方法
2. 方法2:實(shí)現(xiàn)接口并重寫(xiě)方法
2.1 示例代碼
public class CarB implements InitializingBean, DisposableBean { public CarB() { System.out.println("CarB。。。構(gòu)造函數(shù)"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("CarB。。。afterPropertiesSet()方法執(zhí)行"); } @Override public void destroy() throws Exception { System.out.println("CarB。。。destroy()方法執(zhí)行"); } } @Configuration public class ConfigTest { @Bean public CarB carB(){ return new CarB(); } }
執(zhí)行結(jié)果
CarB。。。構(gòu)造函數(shù)
CarB。。。afterPropertiesSet()方法執(zhí)行服務(wù)啟動(dòng)
CarB。。。destroy()方法執(zhí)行
2.2 概述
Spring 開(kāi)放了擴(kuò)展接口,允許我們自定義 bean 的初始化和銷(xiāo)毀方法。即當(dāng) Spring 容器在 bean 進(jìn)行到相應(yīng)的生命周期階段時(shí),會(huì)自動(dòng)調(diào)用我們自定義的初始化和銷(xiāo)毀方法。這兩個(gè)擴(kuò)展接口是 InitializingBean 和 DisposableBean 。
InitializingBean 接口說(shuō)明:該接口為 bean 提供了 bean 屬性初始化后的處理方法,它只有 afterPropertiesSet 一個(gè)方法,凡是實(shí)現(xiàn)此接口的類(lèi),在 bean 的屬性初始化后都會(huì)執(zhí)行該方法。
package org.springframework.beans.factory;
public interface InitializingBean { ? ? void afterPropertiesSet() throws Exception; }
DisposableBean 接口說(shuō)明:該接口為單例 bean 提供了在容器銷(xiāo)毀 bean 時(shí)的處理方法,它只有 destroy 一個(gè)方法,凡是實(shí)現(xiàn)此接口的類(lèi),在 bean 被銷(xiāo)毀時(shí)都會(huì)執(zhí)行該方法。
package org.springframework.beans.factory; public interface DisposableBean { ? ? void destroy() throws Exception; }
2.3 方法1 && 方法2
- 相同點(diǎn):都是在 bean 屬性初始化之后需要執(zhí)行的初始化方法。
- 不同點(diǎn)
方法1:代碼不與Spring耦合;執(zhí)行效率較低(通過(guò)反射來(lái)執(zhí)行initMethod 方法)
方法2:代碼與Spring緊耦合;速度更快(將 bean 強(qiáng)制轉(zhuǎn)換成 InitializingBean 接口類(lèi)型,然后直接調(diào)用 afterPropertiesSet 方法)
- 說(shuō)明:afterPropertiesSet 和 initMethod 可以同時(shí)存在,但是 afterPropertiesSet 方法是在 initMethod 方法之前執(zhí)行的。
- 一個(gè) bean 從創(chuàng)建到初始化的過(guò)程總結(jié)
通過(guò)構(gòu)造器創(chuàng)建 bean
屬性注入
執(zhí)行 afterPropertiesSet 方法
執(zhí)行 initMethod 方法
3. 方法3:利用java的JSR250規(guī)范
代碼示例
public class CarC { public CarC() { System.out.println("CarC。。。構(gòu)造函數(shù)"); } @PostConstruct public void initCarC(){ System.out.println("CarC。。。初始化方法initCarC()"); } @PreDestroy public void destroyCarC(){ System.out.println("CarC。。。銷(xiāo)毀方法destroyCarC"); } } @Configuration public class ConfigTest { @Bean public CarC carC(){ return new CarC(); } }
執(zhí)行結(jié)果
CarC。。。構(gòu)造函數(shù)
CarC。。。初始化方法initCarC()服務(wù)啟動(dòng)
CarC。。。銷(xiāo)毀方法destroyCarC
spring初始化后獲取自定義注解Bean
目的是通過(guò)注解將特定類(lèi)的信息(如接口編號(hào))與類(lèi)關(guān)聯(lián),之后可通過(guò)接口編號(hào)獲取對(duì)應(yīng)bean來(lái)執(zhí)行對(duì)應(yīng)邏輯。
一.新建注解類(lèi)
@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Service public @interface ServiceCode { ? ? String code() default "";? ? ? String className() default ""; }
包含接口編號(hào)和beanName信息。
二.新建接口類(lèi)
@ServiceCode(code = "100010", className = "echoService") @Service("echoService") public class EchoService {? }
三.實(shí)現(xiàn)接口ApplicationListener
來(lái)監(jiān)聽(tīng)spring容器初始化完成后執(zhí)行:
@Component @Order(1) public class ServiceInitListener implements ApplicationListener<ContextRefreshedEvent> { ? ? private static final Logger LOGGER = LoggerFactory.getLogger(ServiceInitListener.class);? ? ? @Override ? ? public void onApplicationEvent(ContextRefreshedEvent event) { ? ? ? ? ApplicationContext applicationContext = event.getApplicationContext(); ? ? ? ? //注意 需要時(shí)根容器才能通過(guò)注解獲取到bean,比如event直接獲取的容器中只有一些公共注冊(cè)bean ? ? ? ? if (applicationContext.getParent() != null) { ? ? ? ? ? ? applicationContext = applicationContext.getParent(); ? ? ? ? } ? ? ? ? Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(ServiceCode.class); ? ? ? ? for (Object bean : beansWithAnnotation.values()) { ? ? ? ? ? ? ServiceCode annotation = bean.getClass().getAnnotation(ServiceCode.class); ? ? ? ? ? ? String code = annotation.code(); ? ? ? ? ? ? String className = annotation.className(); ? ? ? ? ? ? //注冊(cè)接口編號(hào)和beanName ? ? ? ? ? ? //在統(tǒng)一入口可通過(guò)code獲取beanName,然后通過(guò)springContext獲取對(duì)應(yīng)bean執(zhí)行自定義邏輯 ? ? ? ? ? ? //或者完成其他邏輯 ? ? ? ? } ? ? }? }
注意:
ContextRefreshedEvent獲取到的上下文環(huán)境不是根spring容器,其中只有部分spring內(nèi)置bean,無(wú)法通過(guò)注解獲取到自定義bean,需要獲取其父容器來(lái)完成操作。我第一次獲取是beanList總為空,后來(lái)發(fā)現(xiàn)其容器內(nèi)部bean沒(méi)有自定義的service bean,獲取父容器后操作一切正常。
通過(guò)@Order注解來(lái)定制執(zhí)行順序,越小越優(yōu)先執(zhí)行。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot自定義bean綁定實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot自定義bean綁定,最常見(jiàn)的配置綁定的場(chǎng)景,是在自定義的bean中通過(guò)@Value注解將某個(gè)屬性和對(duì)應(yīng)的配置綁定2022-10-10解決Idea運(yùn)行junit測(cè)試時(shí)報(bào)Error:[3,17]?程序包org.junit不存在的問(wèn)題
這篇文章主要介紹了Idea運(yùn)行junit測(cè)試時(shí)報(bào)Error:[3,17]?程序包org.junit不存在解決方法,本文給大家分享兩種解決辦法,需要的朋友可以參考下2023-03-03java實(shí)現(xiàn)最短路徑算法之Dijkstra算法
這篇文章主要介紹了java實(shí)現(xiàn)最短路徑算法之Dijkstra算法, Dijkstra算法是最短路徑算法中為人熟知的一種,是單起點(diǎn)全路徑算法,有興趣的可以了解一下2017-10-10Java ThreadLocal的設(shè)計(jì)理念與作用
這篇文章主要介紹了Java ThreadLocal的設(shè)計(jì)理念與作用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03Java實(shí)現(xiàn)十進(jìn)制與二進(jìn)制互轉(zhuǎn)的示例詳解
這篇文章主要為大家詳細(xì)介紹了Java如何實(shí)現(xiàn)十進(jìn)制與二進(jìn)制的互轉(zhuǎn),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定幫助,需要的可以參考一下2022-11-11java有界類(lèi)型參數(shù)的實(shí)例用法
小編給大家整理了一篇關(guān)于java有界類(lèi)型參數(shù)的使用的相關(guān)文章及擴(kuò)展實(shí)例內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。2021-07-07maven查看依賴(lài)樹(shù)的方法實(shí)現(xiàn)
Maven依賴(lài)樹(shù)是以當(dāng)前項(xiàng)目的POM文件為根節(jié)點(diǎn),列出了所有直接或間接依賴(lài)的依賴(lài)樹(shù)結(jié)構(gòu),本文就詳細(xì)的來(lái)介紹一下如何查看,感興趣的可以了解一下2023-08-08IDEA無(wú)法識(shí)別SpringBoot項(xiàng)目的簡(jiǎn)單解決辦法
今天使用idea的時(shí)候,遇到idea無(wú)法啟動(dòng)springboot,所以這篇文章主要給大家介紹了關(guān)于IDEA無(wú)法識(shí)別SpringBoot項(xiàng)目的簡(jiǎn)單解決辦法,需要的朋友可以參考下2023-08-08詳解簡(jiǎn)單基于spring的redis配置(單機(jī)和集群模式)
這篇文章主要介紹了詳解簡(jiǎn)單基于spring的redis配置(單機(jī)和集群模式),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02