Spring?Boot如何處理@Resource示例分析
先造一個(gè)測(cè)試用例
public class TransactionServiceTest { @Resource private IQrcodeAdScheduleService qrcodeAdScheduleService; }
啟動(dòng)Spring Boot調(diào)用棧信息
圖1
解析@Resource對(duì)應(yīng)的bean信息
由上圖可知,在創(chuàng)建完bean實(shí)例后,通過(guò)applyMergedBeanDefinitionPostProcessors()修改beanDefinition結(jié)構(gòu)(針對(duì)這種場(chǎng)景可以理解為解析@Resource對(duì)應(yīng)的bean信息)
protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class<?> beanType, String beanName) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof MergedBeanDefinitionPostProcessor) { MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp; //執(zhí)行CommonAnnotationBeanPostProcessor類(lèi)postProcessMergedBeanDefinition() bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName); } } }
圖2
CommonAnnotationBeanPostProcessor
有圖2可知,處理@Resource的PostProcessor是“CommonAnnotationBeanPostProcessor”,然后看一下CommonAnnotationBeanPostProcessor的部分細(xì)節(jié):
private InjectionMetadata buildResourceMetadata(final Class<?> clazz) { LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<>(); Class<?> targetClass = clazz; do { final LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<>(); ReflectionUtils.doWithLocalFields(targetClass, field -> { if (webServiceRefClass != null && field.isAnnotationPresent(webServiceRefClass)) { if (Modifier.isStatic(field.getModifiers())) { throw new IllegalStateException("@WebServiceRef annotation is not supported on static fields"); } currElements.add(new WebServiceRefElement(field, field, null)); } else if (ejbRefClass != null && field.isAnnotationPresent(ejbRefClass)) { if (Modifier.isStatic(field.getModifiers())) { throw new IllegalStateException("@EJB annotation is not supported on static fields"); } currElements.add(new EjbRefElement(field, field, null)); } //解析@Resource.class else if (field.isAnnotationPresent(Resource.class)) { if (Modifier.isStatic(field.getModifiers())) { throw new IllegalStateException("@Resource annotation is not supported on static fields"); } if (!ignoredResourceTypes.contains(field.getType().getName())) { currElements.add(new ResourceElement(field, field, null)); } } }); }
上面的代碼塊出現(xiàn)了期待已久的“Resource.class”關(guān)鍵字,我們就放心了。
我們?cè)倩仡櫼幌拢?br />其流程是這樣的:在AbstractAutowireCapableBeanFactory.populateBean()->ibp.postProcessPropertyValue()->CommonAnnotationBeanPostProcessor.postProcessPropertyValue()去實(shí)例化@Resource作用的bean;
除了和處理@Autowired不是一個(gè)PostProcessor(處理@AutoWireds是用這個(gè)“AutowiredAnnotationBeanPostProcessor”PostProcessor)其他處理流程和@Autowired的處理流程一毛一樣?。?/p>
以上就是Spring Boot如何處理@Resource示例分析的詳細(xì)內(nèi)容,更多關(guān)于Spring Boot處理@Resource的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Shiro 控制并發(fā)登錄人數(shù)限制及登錄踢出的實(shí)現(xiàn)代碼
本文通過(guò)shiro實(shí)現(xiàn)一個(gè)賬號(hào)只能同時(shí)一個(gè)人使用,本文重點(diǎn)給大家分享Shiro 控制并發(fā)登錄人數(shù)限制及登錄踢出的實(shí)現(xiàn)代碼,需要的朋友參考下吧2017-09-09Java concurrency線程池之線程池原理(一)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java concurrency線程池之線程池原理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Java實(shí)現(xiàn)產(chǎn)生隨機(jī)字符串主鍵的UUID工具類(lèi)
這篇文章主要介紹了Java實(shí)現(xiàn)產(chǎn)生隨機(jī)字符串主鍵的UUID工具類(lèi),涉及java隨機(jī)數(shù)與字符串遍歷、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10基于Spring Boot應(yīng)用ApplicationEvent案例場(chǎng)景
這篇文章主要介紹了基于Spring Boot應(yīng)用ApplicationEvent,利用Spring的機(jī)制發(fā)布ApplicationEvent和監(jiān)聽(tīng)ApplicationEvent,需要的朋友可以參考下2023-03-03Java利用HttpClient模擬POST表單操作應(yīng)用及注意事項(xiàng)
本文主要介紹JAVA中利用HttpClient模擬POST表單操作,希望對(duì)大家有所幫助。2016-04-04SpringBoot中Mybatis + Druid 數(shù)據(jù)訪問(wèn)的詳細(xì)過(guò)程
Spring Boot 底層都是采用 SpringData 的方式進(jìn)行統(tǒng)一處理各種數(shù)據(jù)庫(kù),SpringData也是Spring中與SpringBoot、SpringCloud 等齊名的知名項(xiàng)目,下面看下SpringBoot Mybatis Druid數(shù)據(jù)訪問(wèn)的詳細(xì)過(guò)程,感興趣的朋友一起看看吧2021-11-11