Spring IOC - 依赖注入(概述 getBean)

1.写在前面的废话

前面几次关于 IOC 的分享完成了对 Spring 对资源的加载的一个概述。读者可以对 Spring 加载资源有一个大致的了解。今天我们将开始了解当资源加载到 Spring 容器中后,如何进行依赖注入的。

 

学习 Spring,有一个非常重要的概念就是控制反转,控制反转就是要把资源装配工作丢给 Spring 容器,业务只需要知道自己需要什么样的服务就可以了。Spring 装配 Bean 的依赖是一个比较复杂的过程,基本上是横看成岭侧成峰,远近高低各不同的状态。今天我们从一个基本了解的角度大概了解一下依赖注入的整个过程。

 

2.依赖注入

2.1依赖注入从何处开始

我们知道 Spring 可以是懒加载的,就是当真正使用到 Bean 的时候才实例化 Bean。当然也不全是这样,例如配置 Bean lazy-init 属性,可以控制 Spring 的加载时机。现在机器的性能、内存等都比较高,基本上也不使用懒加载,在容器启动时候来加载bean,启动时间稍微长一点儿,这样在实际获取 bean 供业务使用时,就可以减轻不少负担,这个后面再做分析。 我们使用到 Bean 的时候,最直接的方式就是从 Factroy 中获取,这个就是加载 Bean 实例的源头。

 

从了解 AbstractBeanFactory 开始:

 

  • AbstractBeanFactory 的位置


 AbstractBeanFactory 是大部分 BeanFactory 的基类。他有两方面的能力,一方面是 Bean 注册的能力,另一个方面是 BeanFactory 的固有服务的能力。前者可以判断注册的 Bean 是否是单例、别名注册等;后者让客户端可以获取 Bean 实例。

 

  • AbstractBeanFactory 中的 getBean 方法

 

// 我们经常使用的方法,通过 Bean 名称获取 Bean 实例
public Object getBean(String name) throws BeansException {
	// 交给 doGetBean 处理
	return doGetBean(name, null, null, false);
}

// 通过指定的 Bean 名称和获取 Bean 的类型,获取 Bean 实例
public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
	// 交给 doGetBean 处理
	return doGetBean(name, requiredType, null, false);
}

// 通过 Bean 名称和指定构造 Bean 实例的工厂方法参数来获取 Bean 实例
public Object getBean(String name, Object... args) throws BeansException {
	// 交给 doGetBean 处理
	return doGetBean(name, null, args, false);
}

/**
 * Return an instance, which may be shared or independent, of the specified bean.
 * @param name the name of the bean to retrieve
 * @param requiredType the required type of the bean to retrieve
 * @param args arguments to use if creating a prototype using explicit arguments to a
 * static factory method. It is invalid to use a non-null args value in any other case.
 * @return an instance of the bean
 * @throws BeansException if the bean could not be created
 */
public <T> T getBean(String name, Class<T> requiredType, Object... args) throws BeansException {
	// 交给 doGetBean 处理
	return doGetBean(name, requiredType, args, false);
}

/**
 * Return an instance, which may be shared or independent, of the specified bean.
 * @param name the name of the bean to retrieve
 * @param requiredType the required type of the bean to retrieve
 * @param args arguments to use if creating a prototype using explicit arguments to a
 * static factory method. It is invalid to use a non-null args value in any other case.
 * @param typeCheckOnly whether the instance is obtained for a type check,
 * not for actual use
 * @return an instance of the bean
 * @throws BeansException if the bean could not be created
 */
@SuppressWarnings("unchecked")
protected <T> T doGetBean(
	final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly)
	throws BeansException {
	// 处理别名,找出真正的 Bean 名称
	final String beanName = transformedBeanName(name);
	Object bean;

	// Eagerly check singleton cache for manually registered singletons.
	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 + "'");
			}
		}
		// 处理 FactoryBean 的方式的实例
		bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
	}

	else {
		// Fail if we're already creating this bean instance:
		// We're assumably within a circular reference.
		// 如果正在创建 Bean 跳出处理
		if (isPrototypeCurrentlyInCreation(beanName)) {
			throw new BeanCurrentlyInCreationException(beanName);
		}

		// Check if bean definition exists in this factory.
		// 如果父容器不为空,并且本容器中没有对应的 BeanDefinition 时,交给父容器递归处理。
		BeanFactory parentBeanFactory = getParentBeanFactory();
		if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
			// Not found -> check parent.
			String nameToLookup = originalBeanName(name);
			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);
			}
		}

		if (!typeCheckOnly) {
			markBeanAsCreated(beanName);
		}

		// 获取 BeanDefinition 定义
		final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
		checkMergedBeanDefinition(mbd, beanName, args);

		// Guarantee initialization of beans that the current bean depends on.
		// 查找 BeanDefinition 中依赖,递归处理实例化依赖的 Bean。
		String[] dependsOn = mbd.getDependsOn();
		if (dependsOn != null) {
			for (String dependsOnBean : dependsOn) {
				getBean(dependsOnBean);
				registerDependentBean(dependsOnBean, beanName);
			}
		}

		// Create bean instance.
		// 默认就是单例模式
		if (mbd.isSingleton()) {
			sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() {
				public Object getObject() throws BeansException {
					try {
						// createBean 是虚方法,等待子类实现
						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;
					}
				}
			});
			// 处理 FactoryBean 
			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();
			final Scope scope = this.scopes.get(scopeName);
			if (scope == null) {
				throw new IllegalStateException("No Scope registered for scope '" + scopeName + "'");
			}
			try {
				Object scopedInstance = scope.get(beanName, new ObjectFactory<Object>() {
					public Object getObject() throws BeansException {
						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);
			}
		}
	}

	// Check if required type matches the type of the actual bean instance.
	if (requiredType != null && bean != null && !requiredType.isAssignableFrom(bean.getClass())) {
		try {
			return getTypeConverter().convertIfNecessary(bean, requiredType);
		}
		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;
}

 

2.2 DefaultSingletonBeanRegistry 中的 getSingleton 方法

2.1 中我们知道在 getBean 中有调用 getSingleton 方法,从而来回调来调用 createBean 方法。

 

下面是 getSingleton 的源码:

public Object getSingleton(String beanName, ObjectFactory singletonFactory) {
	Assert.notNull(beanName, "'beanName' must not be null");
	// 同步 singletonObjects 集合
	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 the 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 recordSuppressedExceptions = (this.suppressedExceptions == null);
			if (recordSuppressedExceptions) {
				this.suppressedExceptions = new LinkedHashSet<Exception>();
			}
			try {
				// 回调创建单例实例的策略方法创建 Bean 实例
				singletonObject = singletonFactory.getObject();
			}
			catch (BeanCreationException ex) {
				if (recordSuppressedExceptions) {
					for (Exception suppressedException : this.suppressedExceptions) {
						ex.addRelatedCause(suppressedException);
					}
				}
				throw ex;
			}
			finally {
				if (recordSuppressedExceptions) {
					this.suppressedExceptions = null;
				}
				afterSingletonCreation(beanName);
			}
			addSingleton(beanName, singletonObject);
		}
		return (singletonObject != NULL_OBJECT ? singletonObject : null);
	}
}

 

2.3创建 Bean 实例

创建 Bean 实例,我们必须讲到AbstractAutowireCapableBeanFactory。他继承于 AbstractBeanFactory,子类有 DefaultListableBeanFactory。基本上所有的 ApplicationContext 实例都会实现 DefaultListableBeanFactory,所以这个方法应该是容器里面核心方法。

 

源码如下:

/**
 * Central method of this class: creates a bean instance,
 * populates the bean instance, applies post-processors, etc.
 * @see #doCreateBean
 */
@Override
protected Object createBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)
		throws BeanCreationException {

	if (logger.isDebugEnabled()) {
		logger.debug("Creating instance of bean '" + beanName + "'");
	}
	// Make sure bean class is actually resolved at this point.
	// 确保 BeanClass 真正存在
	resolveBeanClass(mbd, beanName);

	// Prepare method overrides.
	try {
		mbd.prepareMethodOverrides();
	}
	catch (BeanDefinitionValidationException ex) {
		throw new BeanDefinitionStoreException(mbd.getResourceDescription(),
				beanName, "Validation of method overrides failed", ex);
	}

	try {
		// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
		// 定义自己 BeanPostProceccor 来创建 Bean 实例的代理实现,如果创建成功直接返回 
		Object bean = resolveBeforeInstantiation(beanName, mbd);
		if (bean != null) {
			return bean;
		}
	}
	catch (Throwable ex) {
		throw new BeanCreationException(mbd.getResourceDescription(), beanName,
				"BeanPostProcessor before instantiation of bean failed", ex);
	}

	// 如果没有用户自定义的创建 Bean 实例的代理,那么执行通用的创建 Bean 实例的方法
	Object beanInstance = doCreateBean(beanName, mbd, args);
	if (logger.isDebugEnabled()) {
		logger.debug("Finished creating instance of bean '" + beanName + "'");
	}
	return beanInstance;
}

/**
 * Actually create the specified bean. Pre-creation processing has already happened
 * at this point, e.g. checking <code>postProcessBeforeInstantiation</code> callbacks.
 * <p>Differentiates between default bean instantiation, use of a
 * factory method, and autowiring a constructor.
 * @param beanName the name of the bean
 * @param mbd the merged bean definition for the bean
 * @param args arguments to use if creating a prototype using explicit arguments to a
 * static factory method. This parameter must be <code>null</code> except in this case.
 * @return a new instance of the bean
 * @throws BeanCreationException if the bean could not be created
 * @see #instantiateBean
 * @see #instantiateUsingFactoryMethod
 * @see #autowireConstructor
 */
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) {
	// Instantiate the bean.
	BeanWrapper instanceWrapper = null;
	if (mbd.isSingleton()) {
		instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
	}
	if (instanceWrapper == null) {
		instanceWrapper = createBeanInstance(beanName, mbd, args);
	}
	final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null);
	Class beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null);

	// Allow post-processors to modify the merged bean definition.
	synchronized (mbd.postProcessingLock) {
		if (!mbd.postProcessed) {
			applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
			mbd.postProcessed = true;
		}
	}

	// Eagerly cache singletons to be able to resolve circular references
	// even when triggered by lifecycle interfaces like BeanFactoryAware.
	// 关于在创建 Bean 实例前需要的定义执行的 BeanPostProcessor 处理
	boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
			isSingletonCurrentlyInCreation(beanName));
	if (earlySingletonExposure) {
		if (logger.isDebugEnabled()) {
			logger.debug("Eagerly caching bean '" + beanName +
					"' to allow for resolving potential circular references");
		}
		addSingletonFactory(beanName, new ObjectFactory() {
			public Object getObject() throws BeansException {
				return getEarlyBeanReference(beanName, mbd, bean);
			}
		});
	}

	// Initialize the bean instance.
	Object exposedObject = bean;
	try {
		// 组装 Bean
		populateBean(beanName, mbd, instanceWrapper);
		if (exposedObject != null) {
			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);
		}
	}

	// 二次验证在 Bean 创建前处理的 BeanPostProcessor 是否正确
	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<String>(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 " +
							"'getBeanNamesOfType' 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;
}

 

3.小结

本次分享主要了解了依赖注入的入口,以及依赖注入的大概过程。Bean 在做依赖注入时,通过我们常用的 getBean 方法开始,最终到AbstractAutowireCapableBeanFactory.doCreateBean 方法,整个模板方法还是相对比较简单,功能也比较内聚。过程中我们注意到有处理 FactroyBean 的入口、处理单例多例模式的入口、扩展点(BeanPostProcessor)的入口、组装 Bean 实例的入口等,这些都可能我们调查问题、扩展框架的源头。在创建完 Bean 以后,这个 bean 实例就可以真正的被业务所使用。依赖注入的过程是一个相对比较复杂过程,也是 Spring 的核心,很难在一次分享中就能介绍的全面,所以下面的几次分享中,我们将从以下几个方面分角度了解:

 

  • Bean 实例化过程
  • Bean 自动装载的过程
  • Bean 中扩展点(BeanPostProcessor)的处理过程
  • Bean scope处理过程(特别是单例的处理过程)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值