Spring中@PostConstruct的實現(xiàn)方法
前言
大多數(shù)java
程序員都使用過@PostConstruct
注解,它的作用就是在Bean
初始化完成后執(zhí)行,相當(dāng)于我們常說的init()
方法。但是我們看@PostConstruct
只有單單的一個注解,它到底是如何實現(xiàn)在Bean
初始化完成后就被調(diào)用的呢?
源碼分析
我們通過idea
搜索發(fā)現(xiàn),只有CommonAnnotationBeanPostProcessor
這個類使用了@PostConstruct
:
public?class?CommonAnnotationBeanPostProcessor?extends?InitDestroyAnnotationBeanPostProcessor?implementsInstantiationAwareBeanPostProcessor,?BeanFactoryAware,?Serializable?{ ? ? ?public?CommonAnnotationBeanPostProcessor() { ? ? ? ?this.setOrder(2147483644); ? ? ? ?// 給initAnnotationType賦值 ? ? ? ?this.setInitAnnotationType(PostConstruct.class); ? ? ? ?this.setDestroyAnnotationType(PreDestroy.class); ? ? ? ?this.ignoreResourceType("javax.xml.ws.WebServiceContext"); ? ? ? ?if?(jndiPresent) { ? ? ? ? ? ?this.jndiFactory?=?new?SimpleJndiBeanFactory(); ? ? ? } ? ? } }
通過源碼發(fā)現(xiàn),這顯然就是一個BeanPostProcessor
的子類啊,它在Spring
的生命周期中起作用,所以我們可以重點關(guān)注postProcessBeforeInitialization()
方法:
?public?Object?postProcessBeforeInitialization(Object?bean,?String?beanName)?throws?BeansException?{ ? ? ? ?// 獲取被@PostConstruct注解的方法元數(shù)據(jù) ? ? ? ?InitDestroyAnnotationBeanPostProcessor.LifecycleMetadata?metadata?=?this.findLifecycleMetadata(bean.getClass()); ? ? ? ? ?try?{ ? ? ? ? ? ?// 調(diào)用目標方法 ? ? ? ? ? ?metadata.invokeInitMethods(bean,?beanName); ? ? ? ? ? ?return?bean; ? ? ? }?catch?(InvocationTargetException?var5) { ? ? ? ? ? ?throw?new?BeanCreationException(beanName,?"Invocation of init method failed",?var5.getTargetException()); ? ? ? }?catch?(Throwable?var6) { ? ? ? ? ? ?throw?new?BeanCreationException(beanName,?"Failed to invoke init method",?var6); ? ? ? } ? }
當(dāng)Bean
初始化完成后,postProcessBeforeInitialization()
方法將被調(diào)用,所有被注解了@PostConstruct
都會被調(diào)用,無論這個方法是在父類還是子類中:
? ?private?InitDestroyAnnotationBeanPostProcessor.LifecycleMetadata?findLifecycleMetadata(Class<?>?clazz) { ? ? ? ?// 如果緩存為null,那么構(gòu)建緩存;這個緩存是存儲Bean中所有被@PostConstruct注解的方法元數(shù)據(jù) ? ? ? ?if?(this.lifecycleMetadataCache?==?null) { ? ? ? ? ? ?// 構(gòu)建緩存 ? ? ? ? ? ?return?this.buildLifecycleMetadata(clazz); ? ? ? }?else?{ ? ? ? ? ?// 如果緩存不為null,那么從緩存中取出所有被@PostConstruct注解的方法元數(shù)據(jù) ? ? ? ? ? ?InitDestroyAnnotationBeanPostProcessor.LifecycleMetadata?metadata?=(InitDestroyAnnotationBeanPostProcessor.LifecycleMetadata)this.lifecycleMetadataCache.get(clazz); ? ? ? ? ?// 如果緩存中取出來的元數(shù)據(jù)為null,這段代碼這種寫法是考慮到現(xiàn)在有多個線程,用了加鎖操作保證只有一個線程去構(gòu)建緩存buildLifecycleMetadata() ? ? ? ? ? ?if?(metadata?==?null) { ? ? ? ? ? ? ? ?synchronized(this.lifecycleMetadataCache) { ? ? ? ? ? ? ? ? ? ?metadata?=?(InitDestroyAnnotationBeanPostProcessor.LifecycleMetadata)this.lifecycleMetadataCache.get(clazz); ? ? ? ? ? ? ? ? ?// 如果此時還沒拿到元數(shù)據(jù),就去構(gòu)建緩存 ? ? ? ? ? ? ? ? ? ?if?(metadata?==?null) { ? ? ? ? ? ? ? ? ? ? ? ?// 收集好元數(shù)據(jù) ? ? ? ? ? ? ? ? ? ? ? ?metadata?=?this.buildLifecycleMetadata(clazz); ? ? ? ? ? ? ? ? ? ? ? ?// 構(gòu)建緩存 ? ? ? ? ? ? ? ? ? ? ? ?this.lifecycleMetadataCache.put(clazz,?metadata); ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ?// 返回元數(shù)據(jù) ? ? ? ? ? ? ? ? ? ?return?metadata; ? ? ? ? ? ? ? } ? ? ? ? ? }?else?{ ? ? ? ? ? ? ? ?// 返回元數(shù)據(jù) ? ? ? ? ? ? ? ?return?metadata; ? ? ? ? ? } ? ? ? } ? }
1.緩存為null的情況下直接構(gòu)建緩存;
2.緩存不為null,就從緩存中取被注解的方法元數(shù)據(jù),沒取到就構(gòu)建緩存;
所以我們重點看看緩存是如何構(gòu)建的:
? ?private?InitDestroyAnnotationBeanPostProcessor.LifecycleMetadata?buildLifecycleMetadata(Class<?>?clazz) { ? ? ? ?// 如果這個類一定沒有被initAnnotationType或destroyAnnotationType注解 ? ? ? ?// 此時initAnnotationType就是我們的@PostConstruct注解 ? ? ? ?if?(!AnnotationUtils.isCandidateClass(clazz,?Arrays.asList(this.initAnnotationType,?this.destroyAnnotationType))) { ? ? ? ? ? ?return?this.emptyLifecycleMetadata; ? ? ? }?else?{ ? ? ? ? ? ?// 準備好列表來裝被注解的方法 ? ? ? ? ? ?List<InitDestroyAnnotationBeanPostProcessor.LifecycleElement>?initMethods?=?new?ArrayList(); ? ? ? ? ? ?List<InitDestroyAnnotationBeanPostProcessor.LifecycleElement>?destroyMethods?=?new?ArrayList(); ? ? ? ? ? ?Class?targetClass?=?clazz; ? ? ? ? ? ?// 準備循環(huán)向上遍歷所有的父類 ? ? ? ? ? ?do?{ ? ? ? ? ? ? ? ?List<InitDestroyAnnotationBeanPostProcessor.LifecycleElement>?currInitMethods?=?new?ArrayList(); ? ? ? ? ? ? ? ?List<InitDestroyAnnotationBeanPostProcessor.LifecycleElement>?currDestroyMethods?=?new?ArrayList(); ? ? ? ? ? ? ? ?ReflectionUtils.doWithLocalMethods(targetClass, (method)?->?{ ? ? ? ? ? ? ? ? ? ?// 如果這個方法被@PostConstruct注解,那么就構(gòu)建元數(shù)據(jù)并放進currInitMethods中 ? ? ? ? ? ? ? ? ? ?if?(this.initAnnotationType?!=?null?&&?method.isAnnotationPresent(this.initAnnotationType)) { ? ? ? ? ? ? ? ? ? ? ? ?InitDestroyAnnotationBeanPostProcessor.LifecycleElement?element?=?newInitDestroyAnnotationBeanPostProcessor.LifecycleElement(method); ? ? ? ? ? ? ? ? ? ? ? ?currInitMethods.add(element); ? ? ? ? ? ? ? ? ? ? ? ?if?(this.logger.isTraceEnabled()) { ? ? ? ? ? ? ? ? ? ? ? ? ? ?this.logger.trace("Found init method on class ["?+?clazz.getName()?+?"]: "?+?method); ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ?// 下面是判斷是否被destroyAnnotationType注解 ? ? ? ? ? ? ? ? ? ?if?(this.destroyAnnotationType?!=?null?&&?method.isAnnotationPresent(this.destroyAnnotationType)) { ? ? ? ? ? ? ? ? ? ? ? ?currDestroyMethods.add(new?InitDestroyAnnotationBeanPostProcessor.LifecycleElement(method)); ? ? ? ? ? ? ? ? ? ? ? ?if?(this.logger.isTraceEnabled()) { ? ? ? ? ? ? ? ? ? ? ? ? ? ?this.logger.trace("Found destroy method on class ["?+?clazz.getName()?+?"]: "?+?method); ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }); ? ? ? ? ? ? ? ?// 先把當(dāng)前類被注解的方法元數(shù)據(jù)列表放進initMethods頭部 ? ? ? ? ? ? ? ?initMethods.addAll(0,?currInitMethods); ? ? ? ? ? ? ? ?destroyMethods.addAll(currDestroyMethods); ? ? ? ? ? ? ? ?// 獲取當(dāng)前類的父類 ? ? ? ? ? ? ? ?targetClass?=?targetClass.getSuperclass(); ? ? ? ? ? ? ?// 準備遍歷父類是否有被注解的方法,有的話收集好放進initMethods頭部 ? ? ? ? ? }?while(targetClass?!=?null?&&?targetClass?!=?Object.class); ? ? ? ? ?// 返回構(gòu)建好的元數(shù)據(jù) ? ? ? ? ? ?return?initMethods.isEmpty()?&&?destroyMethods.isEmpty()???this.emptyLifecycleMetadata?:?newInitDestroyAnnotationBeanPostProcessor.LifecycleMetadata(clazz,?initMethods,?destroyMethods); ? ? ? } ? }
所以我們通過上述源碼的分析,最后得出以下結(jié)論:
1.
Bean
的父類方法也可以使用@PostConstruct
注解;2.執(zhí)行的時候是先執(zhí)行被
@PostConstruct
注解的父類方法,再執(zhí)行被@PostConstruct
注解的子類方法;3.被
@PostConstruct
注解的方法不能有任何參數(shù),可以通過new InitDestroyAnnotationBeanPostProcessor.LifecycleElement(method)
源碼驗證;
以上就是Spring中@PostConstruct的實現(xiàn)方法的詳細內(nèi)容,更多關(guān)于Spring @PostConstruct實現(xiàn)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java中 String和StringBuffer的區(qū)別實例詳解
這篇文章主要介紹了java中 String和StringBuffer的區(qū)別實例詳解的相關(guān)資料,一個小的例子,來測試String和StringBuffer在時間和空間使用上的差別,需要的朋友可以參考下2017-04-04SpringMVC JSON數(shù)據(jù)交互實現(xiàn)過程解析
這篇文章主要介紹了SpringMVC JSON數(shù)據(jù)交互實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10Mybatis Plus 大數(shù)據(jù)游標分頁的實現(xiàn)
使用MyBatis Plus的游標分頁,我們可以輕松應(yīng)對大數(shù)據(jù)量的場景,本文主要介紹了Mybatis Plus 大數(shù)據(jù)游標分頁的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-07-07Springboot通過Scheduled實現(xiàn)定時任務(wù)代碼
這篇文章主要介紹了Springboot通過Scheduled實現(xiàn)定時任務(wù)代碼,具有一定參考價值,需要的朋友可以了解下。2017-11-11微服務(wù)架構(gòu)設(shè)計RocketMQ基礎(chǔ)及環(huán)境整合
這篇文章主要介紹了微服務(wù)架構(gòu)設(shè)計入門RocketMQ的基礎(chǔ)及環(huán)境整合實現(xiàn)步驟,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-10-10IO密集型任務(wù)設(shè)置線程池線程數(shù)實現(xiàn)方式
這篇文章主要介紹了IO密集型任務(wù)設(shè)置線程池線程數(shù)實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07