转载

Spring Ioc 之 Bean的加载(二)

在上篇文章中 Spring Ioc 之 Bean的加载(一) ,我们分析了Spring Ioc中Bean的加载 doGetBean() 方法的 2.2从缓存中获取单例bean2.3获取最终的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类型转换

2.4、原型模式依赖检查(Prototype)和从 parentBeanFactory 获取 Bean

原型模式依赖检查,对应代码如下:

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(...) 方法

2.5、标记bean为已创建或即将创建

对应代码如下:

//创建的Bean是否需要进行类型验证,一般不需要
			if (!typeCheckOnly) {
				//向容器标记指定的Bean已经被创建
				markBeanAsCreated(beanName);
			}
复制代码

typeCheckOnly 是doGetBean(final String name, @Nullable final Class 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);
				}
			}
		}
	}
复制代码

这里用到了单例模式中耳熟能详的双重校验

2.6、获取BeanDefinition

对应代码如下:

//从容器中获取 beanName 相应的 GenericBeanDefinition 对象,并将其转换为 RootBeanDefinition 对象
	//主要解决Bean继承时子类合并父类公共属性问题
	final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
	// 检查给定的合并的 BeanDefinition (是否为抽象类)
	checkMergedBeanDefinition(mbd, beanName, args);
复制代码

这段代码注释很详细,就不多解释了。

2.7、bean依赖处理

对应代码如下:

// 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

<2>、遍历这些依赖,判断此依赖是否已注册给当前的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() 检查

<3>、没有,则先注册依赖的Bean

如果没有注册依赖的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)

<4>、递归调用getBean(dep),先生成依赖的bean

到了这一步,递归调用getBean(beanName)方法也就是doGetBean(beanName)重走当前流程,来先实例化依赖的Bean。等依赖的Bean实例化之后,当前bean再接着往下执行。

2.8、不同作用域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各个scope的bean创建

2.9、类型转换

代码:

// 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;
复制代码

requiredTypegetBean() 方法可传入的一个参数,即可以根据指定的 beanName 和 requiredType 来获取Bean。

但是一般情况下是不需要类型检查的, requiredType 一般为null,如 getBean(beanName)

requiredType 不为null的时候走这段逻辑。

总结:

至此,spring加载Bean也就是 getBean() 我们大致分析完了,之后会再写几篇文章对其中有些步骤进行详细介绍。

参考:

芋道源码

原文  https://juejin.im/post/5d70c0c6e51d4561a705bb76
正文到此结束
Loading...