天天看點

Spring源碼分析之getBean流程分析(循環依賴)前言一、getBean(String beanName)二、循環依賴

文章目錄

  • 前言
  • 一、getBean(String beanName)
    • 1.1 getSingleton(beanName)
      • 1.1.1 三級緩存
    • 1.2 單例Bean的建立
      • 1.2.1 singletonFactory.getObject()
      • 1.2.2 createBeanInstance(beanName, mbd, args)
  • 二、循環依賴

前言

當Spring的資源解析器把解析好的bean加載到IOC容器後,我們要向擷取某個bean執行個體,隻需getBean(beanName)即可,這篇文章就是分析這個方法的流程,而且詳細講解了Spring是如何解決循環依賴的

這篇文章的思路是:

  1. 先說清楚getBean()的流程,即怎麼初始化一個bean的
  2. 再說Spring是如何解決循環依賴的

一、getBean(String beanName)

話不多說直接上源碼, 一步步往裡追源碼

AbstractApplicationContext.java

@Override
	public Object getBean(String name) throws BeansException {
		assertBeanFactoryActive();
		return getBeanFactory().getBean(name);
	}
           

繼續往裡追

getBean(name)

AbstractBeanFactory.java
@Override
	public Object getBean(String name) throws BeansException {
		return doGetBean(name, null, null, false);
	}
繼續追doGetBean(name, null, null, false)

說明: 不重要的方法我會略過,或者直接在代碼中标注方法含義,重要的會單獨列出來說
protected <T> T doGetBean(
			String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly)
			throws BeansException {
		//取出真正的beanName
		String beanName = transformedBeanName(name);
		Object bean;

		//重要,單獨列出來說
		Object sharedInstance = getSingleton(beanName);
		if (sharedInstance != null && args == null) {
			if (logger.isTraceEnabled()) {
				if (isSingletonCurrentlyInCreation(beanName)) {
					logger.trace("Returning eagerly cached instance of singleton bean '" + beanName +
							"' that is not fully initialized yet - a consequence of a circular reference");
				}
				else {
					logger.trace("Returning cached instance of singleton bean '" + beanName + "'");
				}
			}
			bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
		}
		....此處為了友善閱讀, 先省略下面的代碼, 後面會說	

           

别被這麼長的方法吓到了,跟着我一步步來,很簡單的,我們逐行拆解

1.1 getSingleton(beanName)

@Override
	@Nullable
	public Object getSingleton(String beanName) {
		return getSingleton(beanName, true);
	}

繼續往裡追
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
		// Quick check for existing instance without full singleton lock
		Object singletonObject = this.singletonObjects.get(beanName);
		if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
			singletonObject = this.earlySingletonObjects.get(beanName);
			if (singletonObject == null && allowEarlyReference) {
				synchronized (this.singletonObjects) {
					// Consistent creation of early reference within full singleton lock
					singletonObject = this.singletonObjects.get(beanName);
					if (singletonObject == null) {
						singletonObject = this.earlySingletonObjects.get(beanName);
						if (singletonObject == null) {
							ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
							if (singletonFactory != null) {
								singletonObject = singletonFactory.getObject();
								this.earlySingletonObjects.put(beanName, singletonObject);
								this.singletonFactories.remove(beanName);
							}
						}
					}
				}
			}
		}
		return singletonObject;
	}
           

1.1.1 三級緩存

這個方法的主要含義就是從

singletonObjects,earlySingletonObjects,singletonFactories

這三個map中取資料,那麼這三個map是幹嘛的呢?

/** Cache of singleton objects: bean name to bean instance. */
一級緩存: 存放的資料  beanName -> 完全執行個體化的bean
	private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);
三級緩存: beanName -> 對象工廠,會在下面單獨說一下這個對象工廠
	/** Cache of singleton factories: bean name to ObjectFactory. */
	private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<>(16);
二級緩存: beanName -> 半初始化的bean執行個體(其實就是隻反射調用預設構造器生成的對象執行個體)
	/** Cache of early singleton objects: bean name to bean instance. */
	private final Map<String, Object> earlySingletonObjects = new ConcurrentHashMap<>(16);
           

這裡的ObjectFactory<?>是什麼呢?

  • 它是一個函數式接口,裡面隻有一個方法getObject(),即取對象執行個體的.對于普通的依賴注入,例如@autowired的屬性注入, 這個三級緩存中存的就是半初始化的bean,這點後面的代碼中會展現,不急

知道了這三個map的含義,那麼這段代碼的意思就很明顯了,就是先從一級緩存中取資料,沒有去取二級緩存,以此類推,但是有個細節要注意:

  • 如果一級緩存中沒有資料,還要判斷

    isSingletonCurrentlyInCreation(beanName)

    ,這又是什麼
public boolean isSingletonCurrentlyInCreation(String beanName) {
		return this.singletonsCurrentlyInCreation.contains(beanName);
	}
	
private final Set<String> singletonsCurrentlyInCreation =
			Collections.newSetFromMap(new ConcurrentHashMap<>(16));
           

這個

singletonsCurrentlyInCreation

是一個set集合,裡面存放的是正在加載的beanName,那麼是什麼時候放進去的呢? 提前劇透下,是在走

createBean(beanName, mbd, args)

之前放進去的,後面會講到. 那麼這個集合的作用是什麼呢?

  • 作用:存放目前正在加載的beanName

至此

getSingleton(String beanName, boolean allowEarlyReference)

的代碼算是說完了, 不要小看這個方法,這個方法可是解決循環依賴很重要的其中一步,後面說循環依賴的時候還會再來說

很明顯,當啟動項目,初始化容器時,緩存都是空的, 而且

isSingletonCurrentlyInCreation(String beanName)

傳回的是false, 剛啟動項目還沒放進singletonsCurrentlyInCreation集合呢,是以此時

getSingleton()

傳回的是null

回到doGetBean(), 因為

getSingleton()

傳回的null,此處代碼不會走

if (sharedInstance != null && args == null) {
			if (logger.isTraceEnabled()) {
				if (isSingletonCurrentlyInCreation(beanName)) {
					logger.trace("Returning eagerly cached instance of singleton bean '" + beanName +
							"' that is not fully initialized yet - a consequence of a circular reference");
				}
				else {
					logger.trace("Returning cached instance of singleton bean '" + beanName + "'");
				}
			}
			bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
		}
           

那麼會走

else

else {
			// Spring不解決bean作用域為Protoy的循環依賴,抛出異常
			if (isPrototypeCurrentlyInCreation(beanName)) {
				throw new BeanCurrentlyInCreationException(beanName);
			}

			// Check if bean definition exists in this factory.
			//沒有父BeanFactory, 這段代碼也不會走
			BeanFactory parentBeanFactory = getParentBeanFactory();
			if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
				// Not found -> check parent.
				String nameToLookup = originalBeanName(name);
				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 if (requiredType != null) {
					// No args -> delegate to standard getBean method.
					return parentBeanFactory.getBean(nameToLookup, requiredType);
				}
				else {
					return (T) parentBeanFactory.getBean(nameToLookup);
				}
			}
			
			//typeCheckOnly傳的是false, 如果不僅僅是檢查而是建立bean,這裡記錄一下,
			//這個方法不是重點,略
			if (!typeCheckOnly) {
				markBeanAsCreated(beanName);
			}
			
			//如果有标注@DependsOn注解,一般也不會走,略
			try {
				RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
				checkMergedBeanDefinition(mbd, beanName, args);

				// Guarantee initialization of beans that the current bean depends on.
				String[] dependsOn = mbd.getDependsOn();
				if (dependsOn != null) {
					for (String dep : dependsOn) {
						if (isDependent(beanName, dep)) {
							throw new BeanCreationException(mbd.getResourceDescription(), beanName,
						"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
						}
						registerDependentBean(dep, beanName);
						try {
							getBean(dep);
						}
						catch (NoSuchBeanDefinitionException ex) {
							throw new BeanCreationException(mbd.getResourceDescription(), beanName,
									"'" + beanName + "' depends on missing bean '" + dep + "'", ex);
						}
					}
				}

				// Create bean instance.
				//如果bean的作用域是單例的, 到這就是重點啦,重中之重,單獨說
				if (mbd.isSingleton()) {
					sharedInstance = getSingleton(beanName, () -> {
						try {
							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.
							destroySingleton(beanName);
							throw ex;
						}
					});
					bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
				}
				
				//如果不是單例的, 不是重點,略
				else if (mbd.isPrototype()) {
					// It's a prototype -> create a new instance.
					Object prototypeInstance = null;
					try {
						beforePrototypeCreation(beanName);
						prototypeInstance = createBean(beanName, mbd, args);
					}
					finally {
						afterPrototypeCreation(beanName);
					}
					bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
				}
				
				//其他情況, 基本不會走, 略
				else {
					String scopeName = mbd.getScope();
					if (!StringUtils.hasLength(scopeName)) {
						throw new IllegalStateException("No scope name defined for bean ´" + beanName + "'");
					}
					Scope scope = this.scopes.get(scopeName);
					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 = 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;
			}
		}
           

1.2 單例Bean的建立

if (mbd.isSingleton()) {
					sharedInstance = getSingleton(beanName, () -> {
						try {
							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.
							destroySingleton(beanName);
							throw ex;
						}
					});
					bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
				}
           

如果bean的作用域是單例的,會走

getSingleton(String beanName, ObjectFactory<?> singletonFactory)

public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
		Assert.notNull(beanName, "Bean name must not be null");
		synchronized (this.singletonObjects) {
		//再從緩存中取一次,如果沒有
			Object singletonObject = this.singletonObjects.get(beanName);
			if (singletonObject == null) {
				if (this.singletonsCurrentlyInDestruction) {
					throw new BeanCreationNotAllowedException(beanName,
							"Singleton bean creation not allowed while singletons of this factory are in destruction " +
							"(Do not request a bean from a BeanFactory in a destroy method implementation!)");
				}
				if (logger.isDebugEnabled()) {
					logger.debug("Creating shared instance of singleton bean '" + beanName + "'");
				}
//這裡就是将beanName放入了set集合,辨別正在執行執行個體化,這樣當發生循環依賴時,
//前面的那個方法`getSingleton(beanName)`就能繼續往下走,取二級緩存,這個我們放在後面循環依賴說,不急. 接着往下看吧
				beforeSingletonCreation(beanName);
				boolean newSingleton = false;
				boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
				if (recordSuppressedExceptions) {
					this.suppressedExceptions = new LinkedHashSet<>();
				}
//重點來了, 這個singletonFactory是個啥, 他就是我們前面說的三級緩存中放的函數式接口,那麼這個getObject()是什麼呢?
//答案就在我們調getSingleton()時的傳參裡,因為它是個函數式接口,是以可以傳入一個lambda表達式
//下面我們單獨說說這個lambda表達式
				try {
					singletonObject = singletonFactory.getObject();
					newSingleton = true;
				}
				catch (IllegalStateException ex) {
					// Has the singleton object implicitly appeared in the meantime ->
					// if yes, proceed with it since the exception indicates that state.
					singletonObject = this.singletonObjects.get(beanName);
					if (singletonObject == null) {
						throw ex;
					}
				}
				catch (BeanCreationException ex) {
					if (recordSuppressedExceptions) {
						for (Exception suppressedException : this.suppressedExceptions) {
							ex.addRelatedCause(suppressedException);
						}
					}
					throw ex;
				}
				finally {
					if (recordSuppressedExceptions) {
						this.suppressedExceptions = null;
					}
					afterSingletonCreation(beanName);
				}
				if (newSingleton) {
					addSingleton(beanName, singletonObject);
				}
			}
			return singletonObject;
		}
	}
           

1.2.1 singletonFactory.getObject()

//往回找,在doGetBean()中
				sharedInstance = getSingleton(beanName, () -> {
						try {
							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.
							destroySingleton(beanName);
							throw ex;
						}
					});
           

看到這就明白了,原來當走到

singletonObject = singletonFactory.getObject()

時其實走的是

createBean(beanName, mbd, args)

,這個方法可是最重要的了, 打起精神,我們繼續往裡追

@Override
	protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
			throws BeanCreationException {

		...	省略不重要代碼
		
		try {
		//重點在這,我們看這個方法
			Object beanInstance = doCreateBean(beanName, mbdToUse, args);
			if (logger.isTraceEnabled()) {
				logger.trace("Finished creating instance of bean '" + beanName + "'");
			}
			return beanInstance;
		}
		...省略
	}
           
protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
			throws BeanCreationException {

		// Instantiate the bean.
		BeanWrapper instanceWrapper = null;
		if (mbd.isSingleton()) {
			instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
		}
		//重要方法: 執行個體化bean,并放到wrapper對象中 ,我們單獨出來說
		//使用合适的執行個體化政策來建立新的執行個體:工廠方法、構造函數自動注入、簡單初始化
		if (instanceWrapper == null) {
			instanceWrapper = createBeanInstance(beanName, mbd, args);
		}
		Object bean = instanceWrapper.getWrappedInstance();
		Class<?> beanType = instanceWrapper.getWrappedClass();
		if (beanType != NullBean.class) {
			mbd.resolvedTargetType = beanType;
		}

		// Allow post-processors to modify the merged bean definition.
		synchronized (mbd.postProcessingLock) {
			if (!mbd.postProcessed) {
				try {
					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
		// even when triggered by lifecycle interfaces like BeanFactoryAware.
		boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
				isSingletonCurrentlyInCreation(beanName));
		if (earlySingletonExposure) {
			if (logger.isTraceEnabled()) {
				logger.trace("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 {
			populateBean(beanName, mbd, instanceWrapper);
			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<String> 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 " +
								"'getBeanNamesForType' 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;
	}
           

1.2.2 createBeanInstance(beanName, mbd, args)

protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
		// Make sure bean class is actually resolved at this point.
		//解析出Class
		Class<?> beanClass = resolveBeanClass(mbd, beanName);

		if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed()) {
			throw new BeanCreationException(mbd.getResourceDescription(), beanName,
					"Bean class isn't public, and non-public access not allowed: " + beanClass.getName());
		}
//如果存在 Supplier 回調,則使用給定的回調方法初始化政策
		Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
		if (instanceSupplier != null) {
			return obtainFromSupplier(instanceSupplier, beanName);
		}
//使用 FactoryBean 的 factory-method 來建立,支援靜态工廠和執行個體工廠
		if (mbd.getFactoryMethodName() != null) {
			return instantiateUsingFactoryMethod(beanName, mbd, args);
		}

		// Shortcut when re-creating the same bean...
//從緩存中取出構造函數
		boolean resolved = false;
		boolean autowireNecessary = false;
		if (args == null) {
			synchronized (mbd.constructorArgumentLock) {
				if (mbd.resolvedConstructorOrFactoryMethod != null) {
					resolved = true;
					autowireNecessary = mbd.constructorArgumentsResolved;
				}
			}
		}
		//如果解析到了,就使用該構造函數執行個體化
		if (resolved) {
			if (autowireNecessary) {
				return autowireConstructor(beanName, mbd, null, null);
			}
			else {
				return instantiateBean(beanName, mbd);
			}
		}

//如果是構造函數的注入方式,會走到這裡,然後走  autowireConstructor(beanName, mbd, ctors, args)
		// Candidate constructors for autowiring?
		Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
		if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||
				mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
//這個方法對應着構造函數注入的執行個體化,會先看構造函數中的參數有沒有初始化,
//如果參數沒有初始化,那麼會去初始化參數, 即遞歸調用getBean(beanName),最後反射調用構造函數執行個體化
//這也解釋了為什麼Spring隻能解決屬性注入的循環依賴,因為如果是構造函數注入,那麼構造函數中的參數一定要先執行個體化,
//而該參數又依賴了這個要執行個體化的bean,

//舉個例子來說A依賴了B,B依賴了A,且都采用構造函數的方式注入時,
//要執行個體化A,必須要先執行個體化B,而因為B是采用構造函數的方式注入A,是以會先執行個體化A,而A又依賴了B,即陷入了死循環,
//可見: 要想能解決循環依賴最重要的是B不能采用構造函數的方式注入A,至于在A中注入B構造函數和屬性注入都可以

//這個方法沒有單獨說,不是因為這個方法不重要,而是我們平常中常用屬性注入的方式,但從Spring4.x以後吧,都推薦構造函數注入了,
//但我們平時還是用屬性注入的比較多,是以這裡就重點說預設構造函數執行個體化,
			return autowireConstructor(beanName, mbd, ctors, args);
		}

		// Preferred constructors for default construction?
		ctors = mbd.getPreferredConstructors();
		if (ctors != null) {
			return autowireConstructor(beanName, mbd, ctors, null);
		}
//預設構造函數執行個體化,即空參構造器, 當我們采用@autowired屬性注入時,就會走到這裡,我們單獨說下
		// No special handling: simply use no-arg constructor.
		return instantiateBean(beanName, mbd);
	}
           
protected BeanWrapper instantiateBean(String beanName, RootBeanDefinition mbd) {
		try {
			Object beanInstance;
			if (System.getSecurityManager() != null) {
				beanInstance = AccessController.doPrivileged(
						(PrivilegedAction<Object>) () -> getInstantiationStrategy().instantiate(mbd, beanName, this),
						getAccessControlContext());
			}
			else {
			//會走到這裡,單獨說
				beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, this);
			}
			BeanWrapper bw = new BeanWrapperImpl(beanInstance);
			initBeanWrapper(bw);
			return bw;
		}
		catch (Throwable ex) {
			throw new BeanCreationException(
					mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);
		}
	}
           
//instantiate(mbd, beanName, this)

@Override
	public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {
		// Don't override the class with CGLIB if no overrides.
		if (!bd.hasMethodOverrides()) {
			Constructor<?> constructorToUse;
			synchronized (bd.constructorArgumentLock) {
				constructorToUse = (Constructor<?>) bd.resolvedConstructorOrFactoryMethod;
				if (constructorToUse == null) {
					final Class<?> clazz = bd.getBeanClass();
					if (clazz.isInterface()) {
						throw new BeanInstantiationException(clazz, "Specified class is an interface");
					}
					try {
						if (System.getSecurityManager() != null) {
							constructorToUse = AccessController.doPrivileged(
									(PrivilegedExceptionAction<Constructor<?>>) clazz::getDeclaredConstructor);
						}
						else {
							constructorToUse = clazz.getDeclaredConstructor();
						}
						bd.resolvedConstructorOrFactoryMethod = constructorToUse;
					}
					catch (Throwable ex) {
						throw new BeanInstantiationException(clazz, "No default constructor found", ex);
					}
				}
			}
			//會走到這裡,繼續追
			return BeanUtils.instantiateClass(constructorToUse);
		}
		else {
			// Must generate CGLIB subclass.
			return instantiateWithMethodInjection(bd, beanName, owner);
		}
	}
           
public static <T> T instantiateClass(Constructor<T> ctor, Object... args) throws BeanInstantiationException {
		Assert.notNull(ctor, "Constructor must not be null");
		try {
			ReflectionUtils.makeAccessible(ctor);
			if (KotlinDetector.isKotlinReflectPresent() && KotlinDetector.isKotlinType(ctor.getDeclaringClass())) {
				return KotlinDelegate.instantiateClass(ctor, args);
			}
			else {
				Class<?>[] parameterTypes = ctor.getParameterTypes();
				Assert.isTrue(args.length <= parameterTypes.length, "Can't specify more arguments than constructor parameters");
				Object[] argsWithDefaultValues = new Object[args.length];
				//入參args是一個空的, args.length=0
				for (int i = 0 ; i < args.length; i++) {
					if (args[i] == null) {
						Class<?> parameterType = parameterTypes[i];
						argsWithDefaultValues[i] = (parameterType.isPrimitive() ? DEFAULT_TYPE_VALUES.get(parameterType) : null);
					}
					else {
						argsWithDefaultValues[i] = args[i];
					}
				}
				//看到了吧,反射調用空參構造器
				return ctor.newInstance(argsWithDefaultValues);
			}
		}
		...友善閱讀,省略
	}
           

至此,

doCreateBean 中的 createBeanInstance

方法走完了,接着往下看

protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
			throws BeanCreationException {

		// Instantiate the bean.
		BeanWrapper instanceWrapper = null;
		if (mbd.isSingleton()) {
			instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
		}
		if (instanceWrapper == null) {
		//這個方法走完後,就有了一個半初始化的對象執行個體
			instanceWrapper = createBeanInstance(beanName, mbd, args);
		}
		//就是它,半初始化的對象執行個體
		Object bean = instanceWrapper.getWrappedInstance();
		Class<?> beanType = instanceWrapper.getWrappedClass();
		if (beanType != NullBean.class) {
			mbd.resolvedTargetType = beanType;
		}

		// Allow post-processors to modify the merged bean definition.
		synchronized (mbd.postProcessingLock) {
			if (!mbd.postProcessed) {
				try {
					applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
				}
				catch (Throwable ex) {
					throw new BeanCreationException(mbd.getResourceDescription(), beanName,
							"Post-processing of merged bean definition failed", ex);
				}
				mbd.postProcessed = true;
			}
		}

//提前曝光bean,解決循環依賴很重要的一步
//如果是單例的,且允許循環依賴,且目前bean正在建立中,就會把該bean加入到三級緩存中

		// Eagerly cache singletons to be able to resolve circular references
		// even when triggered by lifecycle interfaces like BeanFactoryAware.
		boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
				isSingletonCurrentlyInCreation(beanName));
		if (earlySingletonExposure) {
			if (logger.isTraceEnabled()) {
				logger.trace("Eagerly caching bean '" + beanName +
						"' to allow for resolving potential circular references");
			}
			//把該bean加入到三級緩存中,下方單獨列出了,不懂的看下方,這個方法比較簡單,一筆帶過了
			addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
		}

		// Initialize the bean instance.
		Object exposedObject = bean;
		try {
		//屬性填充,将bean中屬性注入的bean初始化,即遞歸調用getBean()
			populateBean(beanName, mbd, instanceWrapper);
		//調用初始化方法,主要是對bean的一些增強操作,然後将這個bean傳回
			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) {
//到這裡又會走getSingleton(beanName, false),是不是很熟悉,沒錯就是我們進doGetBean時一開始會走的從緩存中取bean的方法
//不過之前傳的是true,這裡傳的是false
//先提前說明下這個方法的流程,舉例說明

//對于循環依賴: A依賴B, B依賴A,且屬性注入(構造函數注入無法解決嘛)
//初始化A, 走到填充屬性時,會去初始化B, 由于B中又依賴的A, 又去getBean(A), 此時就會走  getSingleton(beanName, true)
//而此時容器中一,二級緩存中沒有A執行個體的,隻有三級緩存中有,就會把三級緩存中那個半初始化的beanA取出(注意:此時會把A放在的二級緩存中,從三級緩存中移除)
//然後B的屬性填充完畢, 然後走初始化完成, 當B走到下面這段代碼時, 即走到getSingleton(beanName, false)時, 一二級緩存均為空,
//但是傳參是false,是以Object earlySingletonReference 為null,那麼傳回的就是初始化完成的exposedObject 
//即exposedObject = initializeBean(beanName, exposedObject, mbd);
// 最後後會把初始化好的B放入一級緩存中
//然後繼續走之前的A屬性填充(留意一下,A填充好了),初始化,但是當A走到這段代碼時,因為之前往二級緩存中放了A,
//是以此時取二級緩存不為空,會傳回緩存中的這個引用,而且如果這個引用和增強後的bean是一個對象執行個體,那麼會傳回這個引用

//此時有人可能會有疑問: 緩存中的這個bean不是半初始化的麼,怎麼傳回使用了呢?
//這是因為,二級緩存中存放的是A的引用,在之前這個引用指向的對象執行個體被填充了啊,這就是之前讓你留意一下的原因

//不過讀到這裡我有個疑問: 為什麼Spring要再去緩存中取一下這個bean呢?我直接使用初始化傳回的那個bean嘛,就是exposedObject,
//為什麼要現在這樣設計呢?還查下緩存,然後再比較bean有沒有變,變了傳回exposedObject, 沒變傳回緩存中的bean
//我看有的部落格說的是此處代碼的作用是: 循環依賴檢查,判斷是否需要抛出異常
//存疑?

			Object earlySingletonReference = getSingleton(beanName, false);
			if (earlySingletonReference != null) {
				if (exposedObject == bean) {
					exposedObject = earlySingletonReference;
				}
				else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
					String[] dependentBeans = getDependentBeans(beanName);
					Set<String> 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 " +
								"'getBeanNamesForType' 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;
	}
           
protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) {
		Assert.notNull(singletonFactory, "Singleton factory must not be null");
		synchronized (this.singletonObjects) {
			if (!this.singletonObjects.containsKey(beanName)) {
			//放入三級緩存() -> getEarlyBeanReference(beanName, mbd, bean)
				this.singletonFactories.put(beanName, singletonFactory);
			//此時二級緩存中是沒有資料的,這個方法此時其實沒什麼用,傳回的也是null,
			//那麼二級緩存中什麼時候會有值呢,這也跟循環依賴有關,我們放到循環依賴篇說
				this.earlySingletonObjects.remove(beanName);
				this.registeredSingletons.add(beanName);
			}
		}
	}
           

讀到這裡,其實

createBean(beanName, mbd, args)

已經走完了,我們接着往下看

public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
		Assert.notNull(beanName, "Bean name must not be null");
		synchronized (this.singletonObjects) {
			Object singletonObject = this.singletonObjects.get(beanName);
			if (singletonObject == null) {
				if (this.singletonsCurrentlyInDestruction) {
					throw new BeanCreationNotAllowedException(beanName,
							"Singleton bean creation not allowed while singletons of this factory are in destruction " +
							"(Do not request a bean from a BeanFactory in a destroy method implementation!)");
				}
				if (logger.isDebugEnabled()) {
					logger.debug("Creating shared instance of singleton bean '" + beanName + "'");
				}
				beforeSingletonCreation(beanName);
				boolean newSingleton = false;
				boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
				if (recordSuppressedExceptions) {
					this.suppressedExceptions = new LinkedHashSet<>();
				}
				try {
				//即這行代碼對應的createBean走完了
					singletonObject = singletonFactory.getObject();
					newSingleton = true;
				}
				catch (IllegalStateException ex) {
					// Has the singleton object implicitly appeared in the meantime ->
					// if yes, proceed with it since the exception indicates that state.
					singletonObject = this.singletonObjects.get(beanName);
					if (singletonObject == null) {
						throw ex;
					}
				}
				catch (BeanCreationException ex) {
					if (recordSuppressedExceptions) {
						for (Exception suppressedException : this.suppressedExceptions) {
							ex.addRelatedCause(suppressedException);
						}
					}
					throw ex;
				}
				finally {
					if (recordSuppressedExceptions) {
						this.suppressedExceptions = null;
					}
					//從存放目前正在建立的bean的set集合中删除該bean
					afterSingletonCreation(beanName);
				}
				if (newSingleton) {
				//加入一級緩存,删除二三級緩存
					addSingleton(beanName, singletonObject);
				}
			}
			return singletonObject;
		}
	}
           

至此getBean的流程已經分析完了,總結下來就是下面這張圖

Spring源碼分析之getBean流程分析(循環依賴)前言一、getBean(String beanName)二、循環依賴

二、循環依賴

當弄明白了bean的初始化流程後,再去了解循環依賴就很容易了,因為Spring解決循環依賴的方法伴随着bean的初始化的.

由于文章到這裡已經很長了,放在下篇中講解

繼續閱讀