Spring?Boot?@Autowired?@Resource屬性賦值時(shí)機(jī)探究
@Resource
先貼出測(cè)試類
@Service public class TransactionServiceTest { @Resource private IQrcodeAdScheduleService qrcodeAdScheduleService; }
Spring Boot啟動(dòng)之后調(diào)用棧信息
圖1
圖2
由圖1,圖2可知InjectionMetadata.inject()執(zhí)行屬性織入邏輯,下面是部分細(xì)節(jié)
protected void inject(Object target, @Nullable String requestingBeanName, @Nullable PropertyValues pvs) throws Throwable { if (this.isField) { Field field = (Field) this.member; ReflectionUtils.makeAccessible(field); //通過(guò)反射對(duì)目標(biāo)target對(duì)象也就是我們之前定義的TransactionServiceTest的屬性賦值 field.set(target, getResourceToInject(target, requestingBeanName)); } }
其中,CommonAnnotationBeanPostProcessor.ResourceElement的member屬性存儲(chǔ)的是Filed信息,對(duì)于本示例就是:
圖3
@Autowired
對(duì)于@Autowired來(lái)說(shuō),就是AutowiredAnnotationBeanPostProcessor.AutowiredFieldElement.inject():
protected void inject(Object bean, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable { Field field = (Field) this.member; Object value; if (this.cached) { value = resolvedCachedArgument(beanName, this.cachedFieldValue); } else { DependencyDescriptor desc = new DependencyDescriptor(field, this.required); desc.setContainingClass(bean.getClass()); Set<String> autowiredBeanNames = new LinkedHashSet<>(1); Assert.state(beanFactory != null, "No BeanFactory available"); TypeConverter typeConverter = beanFactory.getTypeConverter(); try { //遞歸調(diào)用createBean()實(shí)例化目標(biāo)bean的屬性bean實(shí)例 value = beanFactory.resolveDependency(desc, beanName, autowiredBeanNames, typeConverter); } catch (BeansException ex) { throw new UnsatisfiedDependencyException(null, beanName, new InjectionPoint(field), ex); } synchronized (this) { if (!this.cached) { if (value != null || this.required) { this.cachedFieldValue = desc; registerDependentBeans(beanName, autowiredBeanNames); if (autowiredBeanNames.size() == 1) { String autowiredBeanName = autowiredBeanNames.iterator().next(); if (beanFactory.containsBean(autowiredBeanName) && beanFactory.isTypeMatch(autowiredBeanName, field.getType())) { this.cachedFieldValue = new ShortcutDependencyDescriptor( desc, autowiredBeanName, field.getType()); } } } else { this.cachedFieldValue = null; } this.cached = true; } } } if (value != null) { //通過(guò)field進(jìn)行賦值 ReflectionUtils.makeAccessible(field); field.set(bean, value); } } }
其他的流程一毛一樣啊~
以上就是Spring Boot @Autowired @Resource屬性賦值時(shí)機(jī)探究的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot @Autowired @Resource的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java抽象類、繼承及多態(tài)和適配器的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java抽象類、繼承及多態(tài)和適配器的實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-06-06Java中避免NullPointerException的方法總結(jié)
這篇文章主要介紹了Java中避免NullPointerException的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-07-07Java Applet查找素?cái)?shù)小程序代碼實(shí)例
這篇文章主要介紹了Java Applet查找素?cái)?shù)小程序代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02SpringBoot實(shí)現(xiàn)抽獎(jiǎng)算法的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何通過(guò)SpringBoot實(shí)現(xiàn)抽獎(jiǎng)算法,文中的示例代碼簡(jiǎn)潔易懂,具有一定的參考價(jià)值,感興趣的小伙伴可以了解一下2023-06-06MyBatis類型處理器TypeHandler的作用及說(shuō)明
這篇文章主要介紹了MyBatis類型處理器TypeHandler的作用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03java工具類之實(shí)現(xiàn)java獲取文件行數(shù)
這篇文章主要介紹了一個(gè)java工具類,可以取得當(dāng)前項(xiàng)目中所有java文件總行數(shù),代碼行數(shù),注釋行數(shù),空白行數(shù),需要的朋友可以參考下2014-03-03基于JWT的spring boot權(quán)限驗(yàn)證技術(shù)實(shí)現(xiàn)教程
這篇文章主要給大家介紹了關(guān)于基于JWT的spring boot權(quán)限驗(yàn)證技術(shù)實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11