您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Spring Ioc中怎么加載Bean,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
//真正實現向IOC容器獲取Bean的功能,也是觸發依賴注入功能的地方 protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType, @Nullable final Object[] args, boolean typeCheckOnly) throws BeansException { //根據指定的名稱獲取被管理Bean的名稱,剝離指定名稱中對容器的相關依賴 // 如果指定的是別名,將別名轉換為規范的Bean名稱 <1> final String beanName = transformedBeanName(name); Object bean; // Eagerly check singleton cache for manually registered singletons. // 從緩存中獲取已被創建過的單例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是創建創建對象的工廠Bean,兩者之間有區別 //獲取給定Bean的實例對象,該對象要么是 bean 實例本身,要么就是 FactoryBean 創建的 Bean 對象 //(為什么要再次獲取呢,因為上面獲取的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. // 因為 Spring 只解決單例模式下的循環依賴,在原型模式下如果存在循環依賴則會拋出異常。 <4> if (isPrototypeCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException(beanName); } // Check if bean definition exists in this factory. //對IOC容器中是否存在指定名稱的BeanDefinition進行檢查,首先檢查是否 //能在當前的BeanFactory中獲取的所需要的Bean,如果不能則委托當前容器 //的父級容器去查找,如果還是找不到則沿著容器的繼承體系向父級容器查找 BeanFactory parentBeanFactory = getParentBeanFactory(); //當前容器的父級容器存在,且當前容器中不存在指定名稱的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. //委派父級容器根據指定名稱和顯式的參數查找 return (T) parentBeanFactory.getBean(nameToLookup, args); } else { // No args -> delegate to standard getBean method. //委派父級容器根據指定名稱和類型查找 return parentBeanFactory.getBean(nameToLookup, requiredType); } } // 創建的Bean是否需要進行類型驗證,一般不需要 <5> if (!typeCheckOnly) { //向容器標記指定的Bean已經被創建 markBeanAsCreated(beanName); } try { //從容器中獲取 beanName 相應的 GenericBeanDefinition 對象,并將其轉換為 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() // 獲取當前Bean所有依賴Bean的名稱 <7> String[] dependsOn = mbd.getDependsOn(); //如果有依賴 if (dependsOn != null) { for (String dep : dependsOn) { //校驗該依賴是否已經注冊過給當前 Bean if (isDependent(beanName, dep)) { //已注冊,拋出異常 throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'"); } //沒有,則先注冊依賴的bean registerDependentBean(dep, beanName); //遞歸調用getBean(),先生成依賴的bean getBean(dep); } } // Create bean instance. //創建單例Bean <8> if (mbd.isSingleton()) { //這里使用了一個匿名內部類,創建Bean實例對象,并且注冊給所依賴的對象 sharedInstance = getSingleton(beanName, () -> { try { //創建一個指定Bean實例對象,如果有父級繼承,則合并子類和父類的定義 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緩存中清除實例對象 destroySingleton(beanName); throw ex; } }); //獲取給定Bean的實例對象 bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd); } //創建多例Bean else if (mbd.isPrototype()) { //原型模式(Prototype)是每次都會創建一個新的對象 Object prototypeInstance = null; try { //加載前置處理,默認的功能是注冊當前創建的原型對象 beforePrototypeCreation(beanName); //創建指定Bean對象實例 prototypeInstance = createBean(beanName, mbd, args); } finally { //加載后置處理,默認的功能告訴IOC容器指定Bean的原型對象不再創建 afterPrototypeCreation(beanName); } //獲取給定Bean的實例對象 bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd); } //要創建的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 { //這里又使用了一個匿名內部類,獲取一個指定生命周期范圍的實例 Object scopedInstance = scope.get(beanName, () -> { //前置處理 beforePrototypeCreation(beanName); try { return createBean(beanName, mbd, args); } finally { //后置處理 afterPrototypeCreation(beanName); } }); //獲取給定Bean的實例對象 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. //對創建的Bean實例對象進行類型檢查 <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實例對象
<4>處: 具體分析,見2.4原型模式依賴檢查(Prototype)和從 parentBeanFactory 獲取 Bean
<5>處: 具體分析,見2.5標記bean為已創建或即將創建
<6>處: 具體分析,見2.6獲取BeanDefinition
<7>處: 具體分析,見2.7bean依賴處理
<8>處: 具體分析,見2.8不同作用域bean的實例化
<9>處: 具體分析,見2.9類型轉換
原型模式依賴檢查,對應代碼如下:
if (isPrototypeCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException(beanName); }
跟蹤進去:
/** Names of beans that are currently in creation */ private final ThreadLocal<Object> prototypesCurrentlyInCreation = new NamedThreadLocal<>("Prototype beans currently in creation"); protected boolean isPrototypeCurrentlyInCreation(String beanName) { //從ThreadLocal中取出正在創建的prototype Object curVal = this.prototypesCurrentlyInCreation.get(); return (curVal != null && (curVal.equals(beanName) || (curVal instanceof Set && ((Set<?>) curVal).contains(beanName)))); }
Spring 只處理單例模式下得循環依賴,對于原型模式的循環依賴直接拋出異常。
Spring會把正在創建的原型模式Bean存入ThreadLoacl
,在這里通過ThreadLoacl來判斷當前Bean是否已經創建。
從 parentBeanFactory 獲取 Bean,對應代碼如下:
// Check if bean definition exists in this factory. //對IOC容器中是否存在指定名稱的BeanDefinition進行檢查,首先檢查是否 //能在當前的BeanFactory中獲取的所需要的Bean,如果不能則委托當前容器 //的父級容器去查找,如果還是找不到則沿著容器的繼承體系向父級容器查找 BeanFactory parentBeanFactory = getParentBeanFactory(); //當前容器的父級容器存在,且當前容器中不存在指定名稱的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. //委派父級容器根據指定名稱和顯式的參數查找 return (T) parentBeanFactory.getBean(nameToLookup, args); } else { // No args -> delegate to standard getBean method. //委派父級容器根據指定名稱和類型查找 return parentBeanFactory.getBean(nameToLookup, requiredType); } }
如果當前容器緩存中沒有相對應的 BeanDefinition 對象,則會嘗試從父類工廠(parentBeanFactory)中加載,然后再去遞歸調用 getBean(...) 方法
對應代碼如下:
//創建的Bean是否需要進行類型驗證,一般不需要 if (!typeCheckOnly) { //向容器標記指定的Bean已經被創建 markBeanAsCreated(beanName); }
typeCheckOnly
是doGetBean(final String name, @Nullable final Class<T> requiredType,@Nullable final Object[] args, boolean typeCheckOnly)方法中的一個參數,一般這個參數傳的都是false
接著追蹤markBeanAsCreated()
方法:
protected void markBeanAsCreated(String beanName) { // 沒有創建 if (!this.alreadyCreated.contains(beanName)) { synchronized (this.mergedBeanDefinitions) { // 再次檢查一次:DCL 雙重校驗 if (!this.alreadyCreated.contains(beanName)) { clearMergedBeanDefinition(beanName); // 添加到已創建 bean 集合中 this.alreadyCreated.add(beanName); } } } }
這里用到了單例模式中耳熟能詳的雙重校驗
對應代碼如下:
//從容器中獲取 beanName 相應的 GenericBeanDefinition 對象,并將其轉換為 RootBeanDefinition 對象 //主要解決Bean繼承時子類合并父類公共屬性問題 final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); // 檢查給定的合并的 BeanDefinition (是否為抽象類) checkMergedBeanDefinition(mbd, beanName, args);
這段代碼注釋很詳細,就不多解釋了。
對應代碼如下:
// Guarantee initialization of beans that the current bean depends on. // 處理所依賴的 bean @DependsOn() //獲取當前Bean所有依賴Bean的名稱 <1> String[] dependsOn = mbd.getDependsOn(); //如果有依賴 if (dependsOn != null) { for (String dep : dependsOn) { //校驗該依賴是否已經注冊過給當前 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); //遞歸調用getBean(),先生成依賴的bean <4> getBean(dep); } }
在spring中有一個@DependsOn
注解,它的作用是依賴加載,比如A對象要在B對象加載之后才能加載,那么可以在A上面加@DependsOn(value = "B")
注解,就可以達到我們的要求。
其實@DependsOn
實現的原理就是上面這段代碼。
<1>、通過我們前面從IoC容器中拿到的BeanDefinition
,調用mbd.getDependsOn()
方法,獲取當前bean所有的依賴。
<2>、遍歷這些依賴,判斷此依賴是否已注冊給當前的Bean
<3>、沒有,則先注冊依賴的Bean
<4>、遞歸調用getBean(),先生成依賴的bean
代碼:
// 保存的是bean與其依賴的映射關系:B - > A private final Map<String, Set<String>> dependentBeanMap = new ConcurrentHashMap<>(64); //保存的是bean與其依賴的映射關系:A - > B private final Map<String, Set<String>> dependenciesForBeanMap = new ConcurrentHashMap<>(64); private boolean isDependent(String beanName, String dependentBeanName, @Nullable Set<String> alreadySeen) { if (alreadySeen != null && alreadySeen.contains(beanName)) { return false; } // 獲取當前原始 beanName String canonicalName = canonicalName(beanName); // 獲取該bean依賴的其他bean集合 Set<String> dependentBeans = this.dependentBeanMap.get(canonicalName); if (dependentBeans == null) { return false; } // 存在,則證明該依賴已經注冊到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
獲取當前bean對應的所有依賴dependentBeans
,然后判斷是否已注冊,接著遞歸檢查依賴的Bean有沒有依賴,如果有,就遞歸調用isDependent()
檢查
如果沒有注冊依賴的Bean到該 Bean,則執行注冊registerDependentBean(dep, beanName)
:
// 保存的是bean與其依賴的映射關系:B - > A private final Map<String, Set<String>> dependentBeanMap = new ConcurrentHashMap<>(64); //保存的是bean與其依賴的映射關系:A - > B private final Map<String, Set<String>> 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<String> 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設置依賴Bean信息 dependentBeans = new LinkedHashSet<>(8); this.dependentBeanMap.put(canonicalName, dependentBeans); } //把映射關系存入集合 dependentBeans.add(dependentBeanName); } //從容器中:bean名稱-->指定名稱Bean的依賴Bean集合找查找給定名稱Bean的依賴Bean synchronized (this.dependenciesForBeanMap) { Set<String> dependenciesForBean = this.dependenciesForBeanMap.get(dependentBeanName); if (dependenciesForBean == null) { dependenciesForBean = new LinkedHashSet<>(8); this.dependenciesForBeanMap.put(dependentBeanName, dependenciesForBean); } //把映射關系存入集合 dependenciesForBean.add(canonicalName); } }
套用上面的例子,如果 A @DependsOn(value = "B")
,也就是說A依賴于B,那么該方法registerDependentBean(dep, beanName)
中,參數 dep 就是B,beanName 就是A。
這段代碼中其實就是把bean之間的依賴關系注冊到兩個map中。
dependentBeanMap 存入(B,A)
dependenciesForBeanMap 存入(A,B)
到了這一步,遞歸調用getBean(beanName)方法也就是doGetBean(beanName)重走當前流程,來先實例化依賴的Bean。等依賴的Bean實例化之后,當前bean再接著往下執行。
代碼:
// Create bean instance. //創建單例Bean if (mbd.isSingleton()) { //這里使用了一個匿名內部類,創建Bean實例對象,并且注冊給所依賴的對象 sharedInstance = getSingleton(beanName, () -> { try { //創建一個指定Bean實例對象,如果有父級繼承,則合并子類和父類的定義 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緩存中清除實例對象 destroySingleton(beanName); throw ex; } }); //獲取給定Bean的實例對象 bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd); } //創建多例Bean else if (mbd.isPrototype()) { //原型模式(Prototype)是每次都會創建一個新的對象 Object prototypeInstance = null; try { //加載前置處理,默認的功能是注冊當前創建的原型對象 beforePrototypeCreation(beanName); //創建指定Bean對象實例 prototypeInstance = createBean(beanName, mbd, args); } finally { //加載后置處理,默認的功能告訴IOC容器指定Bean的原型對象不再創建 afterPrototypeCreation(beanName); } //獲取給定Bean的實例對象 bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd); } //要創建的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 { //這里又使用了一個匿名內部類,獲取一個指定生命周期范圍的實例 Object scopedInstance = scope.get(beanName, () -> { //前置處理 beforePrototypeCreation(beanName); try { return createBean(beanName, mbd, args); } finally { //后置處理 afterPrototypeCreation(beanName); } }); //獲取給定Bean的實例對象 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實例化
Prototype Bean實例化
其他類型 Bean 實例化(session,request等)
我們先來看singleton Bean實例化:
if (mbd.isSingleton()) { //這里使用了一個匿名內部類,創建Bean實例對象,并且注冊給所依賴的對象 sharedInstance = getSingleton(beanName, () -> { try { //創建一個指定Bean實例對象,如果有父級繼承,則合并子類和父類的定義 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緩存中清除實例對象 destroySingleton(beanName); throw ex; } }); //獲取給定Bean的實例對象 bean = getObjectForBeanInstance(sharedInstance, name,beanName, mbd); }
Spring Bean 的作用域默認為 singleton 。還有其他作用域,如 prototype、request、session 等。
不同的作用域會有不同的初始化策略。
詳見Spring Ioc 之 Bean的加載(三):各個 scope 的 Bean 創建。
代碼:
// Check if required type matches the type of the actual bean instance. //對創建的Bean實例對象進行類型檢查 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;
requiredType
是 getBean() 方法可傳入的一個參數,即可以根據指定的 beanName 和 requiredType 來獲取Bean。
但是一般情況下是不需要類型檢查的,requiredType
一般為null,如getBean(beanName)
當requiredType
不為null的時候走這段邏輯。
關于Spring Ioc中怎么加載Bean就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。