Spring?Boot如何處理@Resource示例分析
先造一個測試用例
public class TransactionServiceTest { @Resource private IQrcodeAdScheduleService qrcodeAdScheduleService; }
啟動Spring Boot調(diào)用棧信息
圖1
解析@Resource對應的bean信息
由上圖可知,在創(chuàng)建完bean實例后,通過applyMergedBeanDefinitionPostProcessors()修改beanDefinition結構(針對這種場景可以理解為解析@Resource對應的bean信息)
protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class<?> beanType, String beanName) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof MergedBeanDefinitionPostProcessor) { MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp; //執(zhí)行CommonAnnotationBeanPostProcessor類postProcessMergedBeanDefinition() bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName); } } }
圖2
CommonAnnotationBeanPostProcessor
有圖2可知,處理@Resource的PostProcessor是“CommonAnnotationBeanPostProcessor”,然后看一下CommonAnnotationBeanPostProcessor的部分細節(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”關鍵字,我們就放心了。
我們再回顧一下,
其流程是這樣的:在AbstractAutowireCapableBeanFactory.populateBean()->ibp.postProcessPropertyValue()->CommonAnnotationBeanPostProcessor.postProcessPropertyValue()去實例化@Resource作用的bean;
除了和處理@Autowired不是一個PostProcessor(處理@AutoWireds是用這個“AutowiredAnnotationBeanPostProcessor”PostProcessor)其他處理流程和@Autowired的處理流程一毛一樣啊!
以上就是Spring Boot如何處理@Resource示例分析的詳細內(nèi)容,更多關于Spring Boot處理@Resource的資料請關注腳本之家其它相關文章!
相關文章
Shiro 控制并發(fā)登錄人數(shù)限制及登錄踢出的實現(xiàn)代碼
本文通過shiro實現(xiàn)一個賬號只能同時一個人使用,本文重點給大家分享Shiro 控制并發(fā)登錄人數(shù)限制及登錄踢出的實現(xiàn)代碼,需要的朋友參考下吧2017-09-09Java concurrency線程池之線程池原理(一)_動力節(jié)點Java學院整理
這篇文章主要為大家詳細介紹了Java concurrency線程池之線程池原理,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06Java實現(xiàn)產(chǎn)生隨機字符串主鍵的UUID工具類
這篇文章主要介紹了Java實現(xiàn)產(chǎn)生隨機字符串主鍵的UUID工具類,涉及java隨機數(shù)與字符串遍歷、轉(zhuǎn)換等相關操作技巧,需要的朋友可以參考下2017-10-10基于Spring Boot應用ApplicationEvent案例場景
這篇文章主要介紹了基于Spring Boot應用ApplicationEvent,利用Spring的機制發(fā)布ApplicationEvent和監(jiān)聽ApplicationEvent,需要的朋友可以參考下2023-03-03Java利用HttpClient模擬POST表單操作應用及注意事項
本文主要介紹JAVA中利用HttpClient模擬POST表單操作,希望對大家有所幫助。2016-04-04SpringBoot中Mybatis + Druid 數(shù)據(jù)訪問的詳細過程
Spring Boot 底層都是采用 SpringData 的方式進行統(tǒng)一處理各種數(shù)據(jù)庫,SpringData也是Spring中與SpringBoot、SpringCloud 等齊名的知名項目,下面看下SpringBoot Mybatis Druid數(shù)據(jù)訪問的詳細過程,感興趣的朋友一起看看吧2021-11-11