真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

騷話@Autowired的底層工作原理,spring的自動裝配

前情回顧

Spring拓展接口之BeanPostProcessor,我們來看看它的底層實現(xiàn)中講到了spring對BeanPostProcessor的底層支持,并且知道了BeanPostProcessor的兩個方法:postProcessBeforeInitialization、postProcessAfterInitialization的執(zhí)行時機,沒看的小伙伴可以回過頭去看看。本來spring的自動裝配是打算放到上一篇博文中詳細講解的,可后來覺得篇幅可能太大了,既然你們都感覺出來了,那我也就明人不說暗話了,之所以沒放到上篇講解,確實是因為篇幅太大了。好了,我們言歸正傳,之所以沒放到上篇來講,篇幅只是原因之

創(chuàng)新互聯(lián)公司技術(shù)團隊十年來致力于為客戶提供成都做網(wǎng)站、網(wǎng)站建設(shè)、品牌網(wǎng)站制作成都全網(wǎng)營銷推廣、搜索引擎SEO優(yōu)化等服務(wù)。經(jīng)過多年發(fā)展,公司擁有經(jīng)驗豐富的技術(shù)團隊,先后服務(wù)、推廣了上千家網(wǎng)站,包括各類中小企業(yè)、企事單位、高校等機構(gòu)單位。

一,最主要的原因是發(fā)現(xiàn)我犯錯了! 犯什么錯了呢?

我想當(dāng)然了! 理所當(dāng)然的認(rèn)為自動裝配是在AutowiredAnnotationBeanPostProcessor的postProcessBeforeInitialization或postProcessAfterInitialization中實現(xiàn)的,我們來看下AutowiredAnnotationBeanPostProcessor類繼承圖

騷話@Autowired的底層工作原理,spring的自動裝配cdn.xitu.io/2019/9/18/16d4477ee7effa62?imageView2/0/w/1280/h/960/format/webp/ignore-error/1">

它間接實現(xiàn)了BeanPostProcessor,我們再去看下那兩個方法(在父類InstantiationAwareBeanPostProcessorAdapter中)

@Override?public?Object?postProcessBeforeInitialization(Object?bean,?String?beanName)?throws?BeansException?{??return?bean;?}?@Override?public?Object?postProcessAfterInitialization(Object?bean,?String?beanName)?throws?BeansException?{??return?bean;?}?復(fù)制代碼

竟然啥也沒干,只是簡單的return bean; 當(dāng)自己深以為然的認(rèn)知被推翻時,那感覺真是斃了狗了

所以自動裝配不能和BeanPostProcessor放一塊講,不得不開兩篇來分開講,我們都知道:強扭的瓜不甜!

自動裝配簡單示例

我們先來看一個簡單的自動裝配的示例,完整實例代碼:spring-boot-BeanPostProcessor

AnimalServiceImpl

@Service?public?class?AnimalServiceImpl?implements?IAnimalService?{??@Autowired??private?Dog?dog;??@Resource??private?Cat?cat;??@Inject??private?Pig?pig;??@Override??public?void?printName()?{??System.out.println(dog.getName());??System.out.println(cat.getName());??System.out.println(pig.getName());??}?}?復(fù)制代碼

AnimalTest

@RunWith(SpringRunner.class)?@SpringBootTest(classes={Application.class})?public?class?AnimalTest?{??@Autowired??private?IAnimalService?animalService;??@Test??public?void?test()?{??animalService.printName();??}?}?復(fù)制代碼

運行結(jié)果

我們在AnimalConfig中只是將Dog、Cat、Pig的實例注冊到了spring容器,那為什么AnimalServiceImpl實例能夠直接應(yīng)用這些實例了,我們并沒有手動的將這些實例賦值到AnimalServiceImpl實例呀? 這其實就是spring提供的自動裝配功能,雖然我們沒有手動的將這些實例賦值到AnimalServiceImpl實例,但是我們發(fā)現(xiàn)AnimalServiceImpl的屬性實例上多了一些注解:@Autowired、@Resource、@Inject,spring通過這些注解自動完成了屬性實例的注入,而不需要我們手動的去賦值了;那么spring是如何實現(xiàn)自動裝配的呢? 我們慢慢往下看(注意:后文主要以@Autowired為例來講解)

自動裝配源碼解析

AutowiredAnnotationBeanPostProcessor的實例化與注冊

不管怎么說,AutowiredAnnotationBeanPostProcessor終歸還是一個BeanPostProcessor,那么它的實例化與注冊(注冊到spring的beanFactory)過程與BeanPostProcessor的實例化與注冊一樣,在spring的啟動過程中,刷新上下文(refresh)的時候,會調(diào)用registerBeanPostProcessors(beanFactory)方法完成BeanPostProcessor的實例化與注冊,后續(xù)再調(diào)用finishBeanFactoryInitialization(beanFactory)實例化非延遲加載的單例bean時,會用到上述注冊的BeanPostProcessor

AutowiredAnnotationBeanPostProcessor的構(gòu)造方法值得我們看看

public?AutowiredAnnotationBeanPostProcessor()?{??this.autowiredAnnotationTypes.add(Autowired.class);??this.autowiredAnnotationTypes.add(Value.class);??try?{??this.autowiredAnnotationTypes.add((Class)??ClassUtils.forName("javax.inject.Inject",?AutowiredAnnotationBeanPostProcessor.class.getClassLoader()));??logger.info("JSR-330?'javax.inject.Inject'?annotation?found?and?supported?for?autowiring");??}??catch?(ClassNotFoundException?ex)?{??//?JSR-330?API?not?available?-?simply?skip.??}?}?復(fù)制代碼

默認(rèn)情況下,AutowiredAnnotationBeanPostProcessor支持@Autowired和@Value,如果類路徑下有java.inject.Inject(也就是引入了javax.inject.jar),那么也支持@Inject注解,是不是與我們最初的認(rèn)知有些不一樣?

將支持的注解放到了autowiredAnnotationTypes屬性中,后續(xù)會用到該屬性

bean的實例化與依賴注入

默認(rèn)情況下,spring會把spring容器中的bean當(dāng)成non-lazy-init singleton來處理(有些特殊的bean除外),也就是說會在spring的啟動過程中就會逐個實例化這些bean,并對這些bean進行依賴注入;當(dāng)我們真正用到這些bean的時候,直接用就行,不用再去實例化,也不用再去注入bean的相關(guān)依賴,spring是不是很厲害?

具體是不是說的這樣,大家準(zhǔn)備好花生、瓜子和啤酒,好戲即將開始

我們先找到正確的入口,然后用下圖省略掉無聊的前戲,直接進入高潮:doCreateBean

doCreateBean內(nèi)容如下

protected?Object?doCreateBean(final?String?beanName,?final?RootBeanDefinition?mbd,?final?@Nullable?Object[]?args)??throws?BeanCreationException?{??//?Instantiate?the?bean.??BeanWrapper?instanceWrapper?=?null;??if?(mbd.isSingleton())?{??instanceWrapper?=?this.factoryBeanInstanceCache.remove(beanName);??}??if?(instanceWrapper?==?null)?{??//?創(chuàng)建bean實例??instanceWrapper?=?createBeanInstance(beanName,?mbd,?args);??}??final?Object?bean?=?instanceWrapper.getWrappedInstance();??Class?beanType?=?instanceWrapper.getWrappedClass();??if?(beanType?!=?NullBean.class)?{??mbd.resolvedTargetType?=?beanType;??}??//?Allow?post-processors?to?modify?the?merged?bean?definition.??//?允許后置處理器來修改bean定義??synchronized?(mbd.postProcessingLock)?{??if?(!mbd.postProcessed)?{??try?{??//?調(diào)用MergedBeanDefinitionPostProcessor的postProcessMergedBeanDefinition方法??//?AutowiredAnnotationBeanPostProcessor實現(xiàn)了MergedBeanDefinitionPostProcessor,即MergedBeanDefinitionPostProcessor的MergedBeanDefinitionPostProcessor會被調(diào)用??applyMergedBeanDefinitionPostProcessors(mbd,?beanType,?beanName);??}??catch?(Throwable?ex)?{??throw?new?BeanCreationException(mbd.getResourceDescription(),?beanName,??"Post-processing?of?merged?bean?definition?failed",?ex);??}??mbd.postProcessed?=?true;??}??}??//?Eagerly?cache?singletons?to?be?able?to?resolve?circular?references?立即緩存單例以便能夠解析循環(huán)引用??//?even?when?triggered?by?lifecycle?interfaces?like?BeanFactoryAware.??boolean?earlySingletonExposure?=?(mbd.isSingleton()?&&?this.allowCircularReferences?&&??isSingletonCurrentlyInCreation(beanName));??if?(earlySingletonExposure)?{??if?(logger.isDebugEnabled())?{??logger.debug("Eagerly?caching?bean?'"?+?beanName?+??"'?to?allow?for?resolving?potential?circular?references");??}??addSingletonFactory(beanName,?()?->?getEarlyBeanReference(beanName,?mbd,?bean));??}??//?Initialize?the?bean?instance.??Object?exposedObject?=?bean;??try?{??//?填充bean,包含依賴注入??populateBean(beanName,?mbd,?instanceWrapper);??//?初始化bean,BeanPostProcessor的兩個方法在此中被調(diào)用??exposedObject?=?initializeBean(beanName,?exposedObject,?mbd);??}??catch?(Throwable?ex)?{??if?(ex?instanceof?BeanCreationException?&&?beanName.equals(((BeanCreationException)?ex).getBeanName()))?{??throw?(BeanCreationException)?ex;??}??else?{??throw?new?BeanCreationException(??mbd.getResourceDescription(),?beanName,?"Initialization?of?bean?failed",?ex);??}??}??if?(earlySingletonExposure)?{??Object?earlySingletonReference?=?getSingleton(beanName,?false);??if?(earlySingletonReference?!=?null)?{??if?(exposedObject?==?bean)?{??exposedObject?=?earlySingletonReference;??}??else?if?(!this.allowRawInjectionDespiteWrapping?&&?hasDependentBean(beanName))?{??String[]?dependentBeans?=?getDependentBeans(beanName);??Set?actualDependentBeans?=?new?LinkedHashSet<>(dependentBeans.length);??for?(String?dependentBean?:?dependentBeans)?{??if?(!removeSingletonIfCreatedForTypeCheckOnly(dependentBean))?{??actualDependentBeans.add(dependentBean);??}??}??if?(!actualDependentBeans.isEmpty())?{??throw?new?BeanCurrentlyInCreationException(beanName,??"Bean?with?name?'"?+?beanName?+?"'?has?been?injected?into?other?beans?["?+??StringUtils.collectionToCommaDelimitedString(actualDependentBeans)?+??"]?in?its?raw?version?as?part?of?a?circular?reference,?but?has?eventually?been?"?+??"wrapped.?This?means?that?said?other?beans?do?not?use?the?final?version?of?the?"?+??"bean.?This?is?often?the?result?of?over-eager?type?matching?-?consider?using?"?+??"'getBeanNamesOfType'?with?the?'allowEagerInit'?flag?turned?off,?for?example.");??}??}??}??}??//?Register?bean?as?disposable.??try?{??registerDisposableBeanIfNecessary(beanName,?bean,?mbd);??}??catch?(BeanDefinitionValidationException?ex)?{??throw?new?BeanCreationException(??mbd.getResourceDescription(),?beanName,?"Invalid?destruction?signature",?ex);??}??return?exposedObject;?}?復(fù)制代碼

我們重點看下posProcessMergedBeanDefinition方法和populateBean方法

posProcessMergedBeanDefinition

可以看到會讀取bean的field和method上的注解,并判斷該注解是否在autowiredAnnotationTypes中,如果在則將field封裝成AutowiredFiledElement對象、將method封裝成AutoWiredMethodElement對象,并存放到InjectionMetadata對象的Set checkedElements屬性中,最后將該InjectionMetadata對象緩存到了AutowiredAnnotationBeanPostProcessor的Map injectionMetadataCache屬性中;說白了就是將bean中被@Autowried(當(dāng)然還包括@Value、@Inject)修飾的field、method找出來,封裝成InjectionMetadata對象并緩存起來,就這么簡單。不僅僅是上圖中的animalServiceImpl這一個bean,spring中所有的非延遲加載的bean都會走這個創(chuàng)建流程。是不是很簡單,是不是干勁十足了

populateBean

騷話@Autowired的底層工作原理,spring的自動裝配

調(diào)用AutowiredAnnotationBeanPostProcessor的postProcessPropertyValues方法,從injectionMetadataCache中獲取當(dāng)前bean的依賴信息,比如animalServiceImpl依賴的dog、pig(有人可能會有這樣的疑問:cat呢? cat是被@Resource修飾的,而@Resource不是由AutowiredAnnotationBeanPostProcessor支持,后續(xù)會講由誰支持),然后逐個將依賴bean注入到目標(biāo)bean(將dog、pig實例注入到animalServiceImpl實例中);依賴bean從哪來呢?還是從beanFactory中獲取,如果不存在,則又回到bean的創(chuàng)建過程把依賴bean(dog、pig)創(chuàng)建出來,流程與創(chuàng)建animalServiceImpl實例一模一樣,也就說在animalServiceImpl實例的依賴注入過程中會把dog、pig對象也創(chuàng)建出來,而不是等到spring逐個實例化bean的過程中輪到dog、pig才實例化dog、pig,那后續(xù)輪到dog、pig時怎么辦了,spring會把創(chuàng)建的bean緩存起來,下次就直接從緩存中取了。上圖只演示Field的,Method也差不太多,就不演示了,都是通過反射實現(xiàn)的 。

總結(jié)

1、bean的創(chuàng)建與初始化

(1)instanceWrapper = createBeanInstance(beanName, mbd, args) 創(chuàng)建目標(biāo)bean實例;

(2)applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName) 尋找目標(biāo)bean的依賴;

(3)populateBean(beanName, mbd, instanceWrapper) 填充目標(biāo)bean,完成依賴注入; (這里的循環(huán)依賴,有興趣的可以自行去琢磨下)

(4)initializeBean(beanName, exposedObject, mbd) 初始化目標(biāo)bean

2、自動裝配與自動配置

自動配置一般而言說的是spring的@Autowired,是spring的特性之一,而自動配置是springboot的@Configuration,是springboot的特性之一

3、Spring支持幾下幾種自動裝配的注解

@Autowired、@Inject、@Resource以及@Value,用的最多的應(yīng)該是@Autowired(至少我是這樣的),@Inject和@Value也是由AutowiredAnnotationBeanPostProcessor支持,而@Resource是由CommonAnnotationBeanPostProcessor支持(還支持@PostConstruct、@PreDestroy等注解)

關(guān)于@Value與@Autowired,不知道大家是否清楚他們之間的區(qū)別,不清楚的可以看看:Spring: @Value vs. @Autowired或者spring的官方文檔,總結(jié)下:@Value >= @Autowired,只是平時應(yīng)用中,@Value更多的是用來注入配置值(如:@Value("${db.url}")),而@Autowired則是bean對象的注入


網(wǎng)站題目:騷話@Autowired的底層工作原理,spring的自動裝配
標(biāo)題網(wǎng)址:http://weahome.cn/article/jppece.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部