10、IoC之依赖注入(3)-解析注入的属性

对于属性注入的间接引用,具体的解析过程是在BeanDefinitionValueResolver类中的resolveValueIfNecessary方法中实现的,主要解析Array,Set,Map,props和字符串类型的间接引用。具体实现源码如下:

	// BeanDefinitionValueResolver
	// 给定一个PropertyValue,返回一个值,必要时解析对工厂中其他bean的引用。
	public Object resolveValueIfNecessary(Object argName, Object value) {
		// 对直接引用bean解析
		if (value instanceof RuntimeBeanReference) {
			RuntimeBeanReference ref = (RuntimeBeanReference) value;
			// 在工厂中解析对另一个bean的引用。
			return resolveReference(argName, ref);
		}
		// 属性值引用的是容器中另外一个bean的名称进行解析
		else if (value instanceof RuntimeBeanNameReference) {
			// 
			String refName = ((RuntimeBeanNameReference) value).getBeanName();
			refName = String.valueOf(doEvaluate(refName));
			if (!this.beanFactory.containsBean(refName)) {
				throw new BeanDefinitionStoreException(
						"Invalid bean name '" + refName + "' in bean reference for " + argName);
			}
			return refName;
		}
		else if (value instanceof BeanDefinitionHolder) {
			// 解析BeanDefinitionHolder:包含具有名称和别名的BeanDefinition。
			BeanDefinitionHolder bdHolder = (BeanDefinitionHolder) value;
			return resolveInnerBean(argName, bdHolder.getBeanName(), bdHolder.getBeanDefinition());
		}
		else if (value instanceof BeanDefinition) {
			//解析纯BeanDefinition,不包含名称:使用虚拟名称。
			BeanDefinition bd = (BeanDefinition) value;
			String innerBeanName = "(inner bean)" + BeanFactoryUtils.GENERATED_BEAN_NAME_SEPARATOR +
					ObjectUtils.getIdentityHexString(bd);
			return resolveInnerBean(argName, innerBeanName, bd);
		}
		else if (value instanceof ManagedArray) {
			// 可能需要解析的运行时数组引用。
			ManagedArray array = (ManagedArray) value;
			Class elementType = array.resolvedElementType;
			if (elementType == null) {
				String elementTypeName = array.getElementTypeName();
				if (StringUtils.hasText(elementTypeName)) {
					try {
						// 通过反射获取数组类型
						elementType = ClassUtils.forName(elementTypeName, this.beanFactory.getBeanClassLoader());
						array.resolvedElementType = elementType;
					}
					catch (Throwable ex) {
						throw new BeanCreationException(
								this.beanDefinition.getResourceDescription(), this.beanName,
								"Error resolving array type for " + argName, ex);
					}
				}
				else {
					// 没有获取到数组类型
					// 也没有获取到数组元素的类型
					// 直接设置为Object类型
					elementType = Object.class;
				}
			}
			// 获取到的数组类型
			return resolveManagedArray(argName, (List) value, elementType);
		}
		else if (value instanceof ManagedList) {
			// 解析List引用。
			return resolveManagedList(argName, (List) value);
		}
		else if (value instanceof ManagedSet) {
			// 解析Set引用。
			return resolveManagedSet(argName, (Set) value);
		}
		else if (value instanceof ManagedMap) {
			// 解析Map引用。
			return resolveManagedMap(argName, (Map) value);
		}
		else if (value instanceof ManagedProperties) {
			// 解析props类型
			// 可以把它看成解析key和value都是String的map
			Properties original = (Properties) value;
			Properties copy = new Properties();
			for (Map.Entry propEntry : original.entrySet()) {
				Object propKey = propEntry.getKey();
				Object propValue = propEntry.getValue();
				if (propKey instanceof TypedStringValue) {
					propKey = evaluate((TypedStringValue) propKey);
				}
				if (propValue instanceof TypedStringValue) {
					propValue = evaluate((TypedStringValue) propValue);
				}
				copy.put(propKey, propValue);
			}
			return copy;
		}
		else if (value instanceof TypedStringValue) {
			// 解析字符串类型
			TypedStringValue typedStringValue = (TypedStringValue) value;
			Object valueObject = evaluate(typedStringValue);
			try {
				Class resolvedTargetType = resolveTargetType(typedStringValue);
				if (resolvedTargetType != null) {
					// 递归调用,对目标对象的属性值进行解析
					return this.typeConverter.convertIfNecessary(valueObject, resolvedTargetType);
				}
				// 没有目标对象的类型,返回Object
				else {
					return valueObject;
				}
			}
			catch (Throwable ex) {
				throw new BeanCreationException(
						this.beanDefinition.getResourceDescription(), this.beanName,
						"Error converting typed String value for " + argName, ex);
			}
		}
		else {
			return evaluate(value);
		}
	}
	
	
	// BeanDefinitionValueResolver
	// 对引用类型的解析
	private Object resolveReference(Object argName, RuntimeBeanReference ref) {
		try {
			// 得到bean的名称
			String refName = ref.getBeanName();
			refName = String.valueOf(doEvaluate(refName));
			// 如果引用在父容器内,则在父容器中获取指定的对象
			if (ref.isToParent()) {
				if (this.beanFactory.getParentBeanFactory() == null) {
					// 父容器为空 抛出异常
					throw new BeanCreationException(
							this.beanDefinition.getResourceDescription(), this.beanName,
							"Can't resolve reference to bean '" + refName +
							"' in parent factory: no parent factory available");
				}
				return this.beanFactory.getParentBeanFactory().getBean(refName);
			}
			else {
				// 引用在当前容器内
				// 可能会出发递归的引用依赖注入
				Object bean = this.beanFactory.getBean(refName);
				this.beanFactory.registerDependentBean(refName, this.beanName);
				return bean;
			}
		}
		catch (BeansException ex) {
			throw new BeanCreationException(
					this.beanDefinition.getResourceDescription(), this.beanName,
					"Cannot resolve reference to bean '" + ref.getBeanName() + "' while setting " + argName, ex);
		}
	}
	
	
	
	// 解析Array引用。
	private Object resolveManagedArray(Object argName, List ml, Class elementType) {
		Object resolved = Array.newInstance(elementType, ml.size());
		for (int i = 0; i < ml.size(); i++) {
			// 遍历List中每个节点递归解析
			Array.set(resolved, i,
					resolveValueIfNecessary(new KeyedArgName(argName, i), ml.get(i)));
		}
		return resolved;
	}

	// 解析List引用。
	private List resolveManagedList(Object argName, List ml) {
		List resolved = new ArrayList(ml.size());
		for (int i = 0; i < ml.size(); i++) {
			// 遍历List中每个节点递归解析
			// 和Array很类似
			resolved.add(
					resolveValueIfNecessary(new KeyedArgName(argName, i), ml.get(i)));
		}
		return resolved;
	}

	// 解析Set
	private Set resolveManagedSet(Object argName, Set ms) {
		Set resolved = new LinkedHashSet(ms.size());
		int i = 0;
		for (Object m : ms) {
			// 遍历Set中每个节点进行递归解析
			resolved.add(resolveValueIfNecessary(new KeyedArgName(argName, i), m));
			i++;
		}
		return resolved;
	}

	// 解析Map
	private Map resolveManagedMap(Object argName, Map mm) {
		Map resolved = new LinkedHashMap(mm.size());
		// 递归解析每个元素的key和value
		for (Map.Entry entry : mm.entrySet()) {
			Object resolvedKey = resolveValueIfNecessary(argName, entry.getKey());
			Object resolvedValue = resolveValueIfNecessary(
					new KeyedArgName(argName, entry.getKey()), entry.getValue());
			resolved.put(resolvedKey, resolvedValue);
		}
		return resolved;
	}
	 
  

你可能感兴趣的:(java)