亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring中@PostConstruct的實現(xiàn)方法

 更新時間:2023年06月30日 10:25:20   作者:湘小寶666  
大多數(shù)java程序員都使用過@PostConstruct注解,它的作用就是在Bean初始化完成后執(zhí)行,相當(dāng)于我們常說的init()方法,但是我們看@PostConstruct只有單單的一個注解,它到底是如何實現(xiàn)在Bean初始化完成后就被調(diào)用的呢,本文將詳細給大家介紹一下

前言

大多數(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)文章

最新評論