上一篇博客中有单例创建的如下代码:

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 的源码:

spring源码分析六 bean的加载第三步-单例的创建 上篇

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

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.java
spring源码分析六 bean的加载第三步-单例的创建 上篇

先看如下的方法: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的创建,逻辑最复杂,难度最大。

相关文章:

  • 2022-02-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-31
  • 2022-12-23
  • 2021-08-06
  • 2021-11-30
猜你喜欢
  • 2021-04-05
  • 2021-06-27
  • 2021-10-26
  • 2021-06-27
  • 2022-12-23
  • 2021-07-24
  • 2021-10-14
相关资源
相似解决方案