1. 程式人生 > >spring原始碼分析六 bean的載入第三步-單例的建立 上篇

spring原始碼分析六 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); }

整個邏輯比較簡單,

  1. getSingleton 獲取到物件,方法裡面傳遞ObjectFactory物件
  2. 呼叫createBean(beanName, mbd, args)的方法
  3. 建立bean失敗的話就銷燬bean,呼叫destroySingleton(beanName);方法
    這裡先解是為什麼建立失敗要銷燬bean?這裡要顯示的從快取中移除掉建立失敗的bean,這是因為在建立過程中,可能已經將該bean建立了放到了快取中,為了解決迴圈依賴的問題。這裡不僅僅要移除該bean,並且將依賴該bean的其他的所有的bean都要移除。具體原始碼的分析這裡就略去了。

下面分析 getSingleton 的原始碼:

在這裡插入圖片描述

下面分析createBean(beanName, mbd, args)方法

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.java
在這裡插入圖片描述

先看如下的方法:mbdToUse.prepareMethodOverrides();

public void prepareMethodOverrides() throws BeanDefinitionValidationException {
	// Check that lookup methods exists.
if (hasMethodOverrides()) { Set<MethodOverride> overrides = getMethodOverrides().getOverrides(); synchronized (overrides) { for (MethodOverride mo : overrides) { prepareMethodOverride(mo); } } } } protected void prepareMethodOverride(MethodOverride mo) throws BeanDefinitionValidationException { int count = ClassUtils.getMethodCountForName(getBeanClass(), mo.getMethodName()); if (count == 0) { throw new BeanDefinitionValidationException( "Invalid method override: no method with name '" + mo.getMethodName() + "' on class [" + getBeanClassName() + "]"); } else if (count == 1) { // Mark override as not overloaded, to avoid the overhead of arg type checking. mo.setOverloaded(false); } }

再看Object bean = resolveBeforeInstantiation(beanName, mbdToUse);

protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
	Object bean = null;
	if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
		// Make sure bean class is actually resolved at this point.
		if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
			Class<?> targetType = determineTargetType(beanName, mbd);
			if (targetType != null) {
				bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
				if (bean != null) {
					bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
				}
			}
		}
		mbd.beforeInstantiationResolved = (bean != null);
	}
	return bean;
}

protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {
	for (BeanPostProcessor bp : getBeanPostProcessors()) {
		if (bp instanceof InstantiationAwareBeanPostProcessor) {
			InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
			Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);
			if (result != null) {
				return result;
			}
		}
	}
	return null;
}

public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
		throws BeansException {

	Object result = existingBean;
	for (BeanPostProcessor processor : getBeanPostProcessors()) {
		Object current = processor.postProcessAfterInitialization(result, beanName);
		if (current == null) {
			return result;
		}
		result = current;
	}
	return result;
}

最後看 Object beanInstance = doCreateBean(beanName, mbdToUse, args); 這裡是常規bean的建立,邏輯最複雜,難度最大。