這篇文章將為大家詳細(xì)講解有關(guān)Spring Ioc中怎么加載Bean,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
鄂托克網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),鄂托克網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為鄂托克上千多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的鄂托克做網(wǎng)站的公司定做!
//真正實(shí)現(xiàn)向IOC容器獲取Bean的功能,也是觸發(fā)依賴注入功能的地方 protectedT doGetBean(final String name, @Nullable final Class requiredType, @Nullable final Object[] args, boolean typeCheckOnly) throws BeansException { //根據(jù)指定的名稱獲取被管理Bean的名稱,剝離指定名稱中對容器的相關(guān)依賴 // 如果指定的是別名,將別名轉(zhuǎn)換為規(guī)范的Bean名稱 <1> final String beanName = transformedBeanName(name); Object bean; // Eagerly check singleton cache for manually registered singletons. // 從緩存中獲取已被創(chuàng)建過的單例Bean <2> Object sharedInstance = getSingleton(beanName); //如果緩存中有 if (sharedInstance != null && args == null) { if (logger.isDebugEnabled()) { if (isSingletonCurrentlyInCreation(beanName)) { logger.debug("Returning eagerly cached instance of singleton bean '" + beanName + "' that is not fully initialized yet - a consequence of a circular reference"); } else { logger.debug("Returning cached instance of singleton bean '" + beanName + "'"); } } //注意:BeanFactory是管理容器中Bean的工廠 // FactoryBean是創(chuàng)建創(chuàng)建對象的工廠Bean,兩者之間有區(qū)別 //獲取給定Bean的實(shí)例對象,該對象要么是 bean 實(shí)例本身,要么就是 FactoryBean 創(chuàng)建的 Bean 對象 //(為什么要再次獲取呢,因?yàn)樯厦娅@取的sharedInstance不一定是完整的) <3> bean = getObjectForBeanInstance(sharedInstance, name, beanName, null); } else { // Fail if we're already creating this bean instance: // We're assumably within a circular reference. // 因?yàn)?nbsp;Spring 只解決單例模式下的循環(huán)依賴,在原型模式下如果存在循環(huán)依賴則會拋出異常。 <4> if (isPrototypeCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException(beanName); } // Check if bean definition exists in this factory. //對IOC容器中是否存在指定名稱的BeanDefinition進(jìn)行檢查,首先檢查是否 //能在當(dāng)前的BeanFactory中獲取的所需要的Bean,如果不能則委托當(dāng)前容器 //的父級容器去查找,如果還是找不到則沿著容器的繼承體系向父級容器查找 BeanFactory parentBeanFactory = getParentBeanFactory(); //當(dāng)前容器的父級容器存在,且當(dāng)前容器中不存在指定名稱的Bean if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { // Not found -> check parent. //解析指定Bean名稱的原始名稱 String nameToLookup = originalBeanName(name); // 若為 AbstractBeanFactory 類型,委托父類處理 if (parentBeanFactory instanceof AbstractBeanFactory) { return ((AbstractBeanFactory) parentBeanFactory).doGetBean( nameToLookup, requiredType, args, typeCheckOnly); } else if (args != null) { // Delegation to parent with explicit args. //委派父級容器根據(jù)指定名稱和顯式的參數(shù)查找 return (T) parentBeanFactory.getBean(nameToLookup, args); } else { // No args -> delegate to standard getBean method. //委派父級容器根據(jù)指定名稱和類型查找 return parentBeanFactory.getBean(nameToLookup, requiredType); } } // 創(chuàng)建的Bean是否需要進(jìn)行類型驗(yàn)證,一般不需要 <5> if (!typeCheckOnly) { //向容器標(biāo)記指定的Bean已經(jīng)被創(chuàng)建 markBeanAsCreated(beanName); } try { //從容器中獲取 beanName 相應(yīng)的 GenericBeanDefinition 對象,并將其轉(zhuǎn)換為 RootBeanDefinition 對象 // 主要解決Bean繼承時子類合并父類公共屬性問題 <6> final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); // 檢查給定的合并的 BeanDefinition (是否為抽象類) checkMergedBeanDefinition(mbd, beanName, args); // Guarantee initialization of beans that the current bean depends on. // 處理所依賴的 bean @DependsOn() // 獲取當(dāng)前Bean所有依賴Bean的名稱 <7> String[] dependsOn = mbd.getDependsOn(); //如果有依賴 if (dependsOn != null) { for (String dep : dependsOn) { //校驗(yàn)該依賴是否已經(jīng)注冊過給當(dāng)前 Bean if (isDependent(beanName, dep)) { //已注冊,拋出異常 throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'"); } //沒有,則先注冊依賴的bean registerDependentBean(dep, beanName); //遞歸調(diào)用getBean(),先生成依賴的bean getBean(dep); } } // Create bean instance. //創(chuàng)建單例Bean <8> if (mbd.isSingleton()) { //這里使用了一個匿名內(nèi)部類,創(chuàng)建Bean實(shí)例對象,并且注冊給所依賴的對象 sharedInstance = getSingleton(beanName, () -> { try { //創(chuàng)建一個指定Bean實(shí)例對象,如果有父級繼承,則合并子類和父類的定義 return createBean(beanName, mbd, args); } catch (BeansException ex) { // Explicitly remove instance from singleton cache: It might have been put there // eagerly by the creation process, to allow for circular reference resolution. // Also remove any beans that received a temporary reference to the bean. //顯式地從容器單例模式Bean緩存中清除實(shí)例對象 destroySingleton(beanName); throw ex; } }); //獲取給定Bean的實(shí)例對象 bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd); } //創(chuàng)建多例Bean else if (mbd.isPrototype()) { //原型模式(Prototype)是每次都會創(chuàng)建一個新的對象 Object prototypeInstance = null; try { //加載前置處理,默認(rèn)的功能是注冊當(dāng)前創(chuàng)建的原型對象 beforePrototypeCreation(beanName); //創(chuàng)建指定Bean對象實(shí)例 prototypeInstance = createBean(beanName, mbd, args); } finally { //加載后置處理,默認(rèn)的功能告訴IOC容器指定Bean的原型對象不再創(chuàng)建 afterPrototypeCreation(beanName); } //獲取給定Bean的實(shí)例對象 bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd); } //要創(chuàng)建的Bean既不是Singleton也不是Prototype //如:request、session、application等生命周期 else { String scopeName = mbd.getScope(); final Scope scope = this.scopes.get(scopeName); //Bean定義資源中沒有配置生命周期范圍,則Bean定義不合法 if (scope == null) { throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'"); } try { //這里又使用了一個匿名內(nèi)部類,獲取一個指定生命周期范圍的實(shí)例 Object scopedInstance = scope.get(beanName, () -> { //前置處理 beforePrototypeCreation(beanName); try { return createBean(beanName, mbd, args); } finally { //后置處理 afterPrototypeCreation(beanName); } }); //獲取給定Bean的實(shí)例對象 bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd); } catch (IllegalStateException ex) { throw new BeanCreationException(beanName, "Scope '" + scopeName + "' is not active for the current thread; consider " + "defining a scoped proxy for this bean if you intend to refer to it from a singleton", ex); } } } catch (BeansException ex) { cleanupAfterBeanCreationFailure(beanName); throw ex; } } // Check if required type matches the type of the actual bean instance. //對創(chuàng)建的Bean實(shí)例對象進(jìn)行類型檢查 <9> if (requiredType != null && !requiredType.isInstance(bean)) { try { T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType); if (convertedBean == null) { throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } return convertedBean; } catch (TypeMismatchException ex) { if (logger.isDebugEnabled()) { logger.debug("Failed to convert bean '" + name + "' to required type '" + ClassUtils.getQualifiedName(requiredType) + "'", ex); } throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } } return (T) bean; }
代碼很長,需要一些耐心,下面我們來逐步分析這段代碼:
<1>處:具體分析,見2.1獲取原始beanName
<2>處: 具體分析,見2.2從緩存中獲取單例bean
<3>處: 具體分析,見2.3獲取最終的bean實(shí)例對象
<4>處: 具體分析,見2.4原型模式依賴檢查(Prototype)和從 parentBeanFactory 獲取 Bean
<5>處: 具體分析,見2.5標(biāo)記bean為已創(chuàng)建或即將創(chuàng)建
<6>處: 具體分析,見2.6獲取BeanDefinition
<7>處: 具體分析,見2.7bean依賴處理
<8>處: 具體分析,見2.8不同作用域bean的實(shí)例化
<9>處: 具體分析,見2.9類型轉(zhuǎn)換
原型模式依賴檢查,對應(yīng)代碼如下:
if (isPrototypeCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException(beanName); }
跟蹤進(jìn)去:
/** Names of beans that are currently in creation */ private final ThreadLocal
Spring 只處理單例模式下得循環(huán)依賴,對于原型模式的循環(huán)依賴直接拋出異常。
Spring會把正在創(chuàng)建的原型模式Bean存入ThreadLoacl
,在這里通過ThreadLoacl來判斷當(dāng)前Bean是否已經(jīng)創(chuàng)建。
從 parentBeanFactory 獲取 Bean,對應(yīng)代碼如下:
// Check if bean definition exists in this factory. //對IOC容器中是否存在指定名稱的BeanDefinition進(jìn)行檢查,首先檢查是否 //能在當(dāng)前的BeanFactory中獲取的所需要的Bean,如果不能則委托當(dāng)前容器 //的父級容器去查找,如果還是找不到則沿著容器的繼承體系向父級容器查找 BeanFactory parentBeanFactory = getParentBeanFactory(); //當(dāng)前容器的父級容器存在,且當(dāng)前容器中不存在指定名稱的Bean if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { // Not found -> check parent. //解析指定Bean名稱的原始名稱 String nameToLookup = originalBeanName(name); // 若為 AbstractBeanFactory 類型,委托父類處理 if (parentBeanFactory instanceof AbstractBeanFactory) { return ((AbstractBeanFactory) parentBeanFactory).doGetBean( nameToLookup, requiredType, args, typeCheckOnly); } else if (args != null) { // Delegation to parent with explicit args. //委派父級容器根據(jù)指定名稱和顯式的參數(shù)查找 return (T) parentBeanFactory.getBean(nameToLookup, args); } else { // No args -> delegate to standard getBean method. //委派父級容器根據(jù)指定名稱和類型查找 return parentBeanFactory.getBean(nameToLookup, requiredType); } }
如果當(dāng)前容器緩存中沒有相對應(yīng)的 BeanDefinition 對象,則會嘗試從父類工廠(parentBeanFactory)中加載,然后再去遞歸調(diào)用 getBean(...) 方法
對應(yīng)代碼如下:
//創(chuàng)建的Bean是否需要進(jìn)行類型驗(yàn)證,一般不需要 if (!typeCheckOnly) { //向容器標(biāo)記指定的Bean已經(jīng)被創(chuàng)建 markBeanAsCreated(beanName); }
typeCheckOnly
是doGetBean(final String name, @Nullable final Class
接著追蹤markBeanAsCreated()
方法:
protected void markBeanAsCreated(String beanName) { // 沒有創(chuàng)建 if (!this.alreadyCreated.contains(beanName)) { synchronized (this.mergedBeanDefinitions) { // 再次檢查一次:DCL 雙重校驗(yàn) if (!this.alreadyCreated.contains(beanName)) { clearMergedBeanDefinition(beanName); // 添加到已創(chuàng)建 bean 集合中 this.alreadyCreated.add(beanName); } } } }
這里用到了單例模式中耳熟能詳?shù)碾p重校驗(yàn)
對應(yīng)代碼如下:
//從容器中獲取 beanName 相應(yīng)的 GenericBeanDefinition 對象,并將其轉(zhuǎn)換為 RootBeanDefinition 對象 //主要解決Bean繼承時子類合并父類公共屬性問題 final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); // 檢查給定的合并的 BeanDefinition (是否為抽象類) checkMergedBeanDefinition(mbd, beanName, args);
這段代碼注釋很詳細(xì),就不多解釋了。
對應(yīng)代碼如下:
// Guarantee initialization of beans that the current bean depends on. // 處理所依賴的 bean @DependsOn() //獲取當(dāng)前Bean所有依賴Bean的名稱 <1> String[] dependsOn = mbd.getDependsOn(); //如果有依賴 if (dependsOn != null) { for (String dep : dependsOn) { //校驗(yàn)該依賴是否已經(jīng)注冊過給當(dāng)前 Bean <2> if (isDependent(beanName, dep)) { //已注冊,拋出異常 throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'"); } //沒有,則先注冊依賴的bean <3> registerDependentBean(dep, beanName); //遞歸調(diào)用getBean(),先生成依賴的bean <4> getBean(dep); } }
在spring中有一個@DependsOn
注解,它的作用是依賴加載,比如A對象要在B對象加載之后才能加載,那么可以在A上面加@DependsOn(value = "B")
注解,就可以達(dá)到我們的要求。
其實(shí)@DependsOn
實(shí)現(xiàn)的原理就是上面這段代碼。
<1>、通過我們前面從IoC容器中拿到的BeanDefinition
,調(diào)用mbd.getDependsOn()
方法,獲取當(dāng)前bean所有的依賴。
<2>、遍歷這些依賴,判斷此依賴是否已注冊給當(dāng)前的Bean
<3>、沒有,則先注冊依賴的Bean
<4>、遞歸調(diào)用getBean(),先生成依賴的bean
代碼:
// 保存的是bean與其依賴的映射關(guān)系:B - > A private final Map> dependentBeanMap = new ConcurrentHashMap<>(64); //保存的是bean與其依賴的映射關(guān)系:A - > B private final Map > dependenciesForBeanMap = new ConcurrentHashMap<>(64); private boolean isDependent(String beanName, String dependentBeanName, @Nullable Set alreadySeen) { if (alreadySeen != null && alreadySeen.contains(beanName)) { return false; } // 獲取當(dāng)前原始 beanName String canonicalName = canonicalName(beanName); // 獲取該bean依賴的其他bean集合 Set dependentBeans = this.dependentBeanMap.get(canonicalName); if (dependentBeans == null) { return false; } // 存在,則證明該依賴已經(jīng)注冊到bean中 if (dependentBeans.contains(dependentBeanName)) { return true; } // 遞歸檢測依賴 for (String transitiveDependency : dependentBeans) { if (alreadySeen == null) { alreadySeen = new HashSet<>(); } alreadySeen.add(beanName); if (isDependent(transitiveDependency, dependentBeanName, alreadySeen)) { return true; } } return false; }
這段代碼很簡單,主要就是通過dependentBeanMap
獲取當(dāng)前bean對應(yīng)的所有依賴dependentBeans
,然后判斷是否已注冊,接著遞歸檢查依賴的Bean有沒有依賴,如果有,就遞歸調(diào)用isDependent()
檢查
如果沒有注冊依賴的Bean到該 Bean,則執(zhí)行注冊registerDependentBean(dep, beanName)
:
// 保存的是bean與其依賴的映射關(guān)系:B - > A private final Map> dependentBeanMap = new ConcurrentHashMap<>(64); //保存的是bean與其依賴的映射關(guān)系:A - > B private final Map > dependenciesForBeanMap = new ConcurrentHashMap<>(64); //為指定的Bean注入依賴的Bean public void registerDependentBean(String beanName, String dependentBeanName) { // A quick check for an existing entry upfront, avoiding synchronization... //獲取原始beanName String canonicalName = canonicalName(beanName); Set dependentBeans = this.dependentBeanMap.get(canonicalName); if (dependentBeans != null && dependentBeans.contains(dependentBeanName)) { return; } // No entry yet -> fully synchronized manipulation of the dependentBeans Set //先從容器中:bean名稱-->全部依賴Bean名稱集合找查找給定名稱Bean的依賴Bean synchronized (this.dependentBeanMap) { //獲取給定名稱Bean的所有依賴Bean名稱 dependentBeans = this.dependentBeanMap.get(canonicalName); if (dependentBeans == null) { //為Bean設(shè)置依賴Bean信息 dependentBeans = new LinkedHashSet<>(8); this.dependentBeanMap.put(canonicalName, dependentBeans); } //把映射關(guān)系存入集合 dependentBeans.add(dependentBeanName); } //從容器中:bean名稱-->指定名稱Bean的依賴Bean集合找查找給定名稱Bean的依賴Bean synchronized (this.dependenciesForBeanMap) { Set dependenciesForBean = this.dependenciesForBeanMap.get(dependentBeanName); if (dependenciesForBean == null) { dependenciesForBean = new LinkedHashSet<>(8); this.dependenciesForBeanMap.put(dependentBeanName, dependenciesForBean); } //把映射關(guān)系存入集合 dependenciesForBean.add(canonicalName); } }
套用上面的例子,如果 A@DependsOn(value = "B")
,也就是說A依賴于B,那么該方法registerDependentBean(dep, beanName)
中,參數(shù) dep 就是B,beanName 就是A。
這段代碼中其實(shí)就是把bean之間的依賴關(guān)系注冊到兩個map中。
dependentBeanMap 存入(B,A)
dependenciesForBeanMap 存入(A,B)
到了這一步,遞歸調(diào)用getBean(beanName)方法也就是doGetBean(beanName)重走當(dāng)前流程,來先實(shí)例化依賴的Bean。等依賴的Bean實(shí)例化之后,當(dāng)前bean再接著往下執(zhí)行。
代碼:
// Create bean instance. //創(chuàng)建單例Bean if (mbd.isSingleton()) { //這里使用了一個匿名內(nèi)部類,創(chuàng)建Bean實(shí)例對象,并且注冊給所依賴的對象 sharedInstance = getSingleton(beanName, () -> { try { //創(chuàng)建一個指定Bean實(shí)例對象,如果有父級繼承,則合并子類和父類的定義 return createBean(beanName, mbd, args); } catch (BeansException ex) { // Explicitly remove instance from singleton cache: It might have been put there // eagerly by the creation process, to allow for circular reference resolution. // Also remove any beans that received a temporary reference to the bean. //顯式地從容器單例模式Bean緩存中清除實(shí)例對象 destroySingleton(beanName); throw ex; } }); //獲取給定Bean的實(shí)例對象 bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd); } //創(chuàng)建多例Bean else if (mbd.isPrototype()) { //原型模式(Prototype)是每次都會創(chuàng)建一個新的對象 Object prototypeInstance = null; try { //加載前置處理,默認(rèn)的功能是注冊當(dāng)前創(chuàng)建的原型對象 beforePrototypeCreation(beanName); //創(chuàng)建指定Bean對象實(shí)例 prototypeInstance = createBean(beanName, mbd, args); } finally { //加載后置處理,默認(rèn)的功能告訴IOC容器指定Bean的原型對象不再創(chuàng)建 afterPrototypeCreation(beanName); } //獲取給定Bean的實(shí)例對象 bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd); } //要創(chuàng)建的Bean既不是Singleton也不是Prototype //如:request、session、application等生命周期 else { String scopeName = mbd.getScope(); final Scope scope = this.scopes.get(scopeName); //Bean定義資源中沒有配置生命周期范圍,則Bean定義不合法 if (scope == null) { throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'"); } try { //這里又使用了一個匿名內(nèi)部類,獲取一個指定生命周期范圍的實(shí)例 Object scopedInstance = scope.get(beanName, () -> { //前置處理 beforePrototypeCreation(beanName); try { return createBean(beanName, mbd, args); } finally { //后置處理 afterPrototypeCreation(beanName); } }); //獲取給定Bean的實(shí)例對象 bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd); } catch (IllegalStateException ex) { throw new BeanCreationException(beanName, "Scope '" + scopeName + "' is not active for the current thread; consider " + "defining a scoped proxy for this bean if you intend to refer to it from a singleton", ex); } }
這段代碼很明顯,分成了3個部分:
singleton Bean實(shí)例化
Prototype Bean實(shí)例化
其他類型 Bean 實(shí)例化(session,request等)
我們先來看singleton Bean實(shí)例化:
if (mbd.isSingleton()) { //這里使用了一個匿名內(nèi)部類,創(chuàng)建Bean實(shí)例對象,并且注冊給所依賴的對象 sharedInstance = getSingleton(beanName, () -> { try { //創(chuàng)建一個指定Bean實(shí)例對象,如果有父級繼承,則合并子類和父類的定義 return createBean(beanName, mbd, args); } catch (BeansException ex) { // Explicitly remove instance from singleton cache: It might have been put there // eagerly by the creation process, to allow for circular reference resolution. // Also remove any beans that received a temporary reference to the bean. //顯式地從容器單例模式Bean緩存中清除實(shí)例對象 destroySingleton(beanName); throw ex; } }); //獲取給定Bean的實(shí)例對象 bean = getObjectForBeanInstance(sharedInstance, name,beanName, mbd); }
Spring Bean 的作用域默認(rèn)為 singleton 。還有其他作用域,如 prototype、request、session 等。
不同的作用域會有不同的初始化策略。
詳見Spring Ioc 之 Bean的加載(三):各個 scope 的 Bean 創(chuàng)建。
代碼:
// Check if required type matches the type of the actual bean instance. //對創(chuàng)建的Bean實(shí)例對象進(jìn)行類型檢查 if (requiredType != null && !requiredType.isInstance(bean)) { try { //執(zhí)行轉(zhuǎn)換 T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType); // 轉(zhuǎn)換失敗,拋異常 if (convertedBean == null) { throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } return convertedBean; } catch (TypeMismatchException ex) { if (logger.isDebugEnabled()) { logger.debug("Failed to convert bean '" + name + "' to required type '" + ClassUtils.getQualifiedName(requiredType) + "'", ex); } throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } } return (T) bean;
requiredType
是 getBean()方法可傳入的一個參數(shù),即可以根據(jù)指定的 beanName 和 requiredType 來獲取Bean。
但是一般情況下是不需要類型檢查的,requiredType
一般為null,如getBean(beanName)
當(dāng)requiredType
不為null的時候走這段邏輯。
關(guān)于Spring Ioc中怎么加載Bean就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。