Spring Bean的实例化之属性注入源码剖析过程


Posted in Java/Android onJune 13, 2021

前言

这一章节我们来讨论创建Bean过程中的属性注入,在Spring的IOC容器启动过程中,会把定义的Bean封装成BeanDefinition注册到一个ConcurrentHashMap中,Bean注册完成后,就会对单利的且lazy-init=false 的Bean进行实例化。创建Bean的代码在 AbstractAutowireCapableBeanFactory#doCreateBean 中,当Bean创建成功之后,会调用AbstractAutowireCapableBeanFactory#populateBean 方法进行属性注入。本篇文章主要就是分析该方法是如何实现Bean的属性注入的。

这里先上一个属性注入的流程图,待会儿可以根据这个图来看代码

Spring Bean的实例化之属性注入源码剖析过程

属性注入:AbstractAutowireCapableBeanFactory#populateBean

AbstractAutowireCapableBeanFactory#populateBean 方法的主要功能就是属性填充,源码如下

//使用 bean 定义中的属性值填充给定 BeanWrapper 中的 bean 实例。
	@SuppressWarnings("deprecation")  // for postProcessPropertyValues
	protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {
		if (bw == null) {
			//判断是否有property属性
			if (mbd.hasPropertyValues()) {
				throw new BeanCreationException(
						mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance");
			}
			else {
				//没有任何属性可以填充
				// Skip property population phase for null instance.
				return;
			}
		}

		// Give any InstantiationAwareBeanPostProcessors the opportunity to modify the
		// state of the bean before properties are set. This can be used, for example,
		// to support styles of field injection.
		//让 InstantiationAwareBeanPostProcessors 也在属性注入之前改变Bean的状态
		if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
			for (BeanPostProcessor bp : getBeanPostProcessors()) {
				if (bp instanceof InstantiationAwareBeanPostProcessor) {
					InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
					if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
						return;
					}
				}
			}
		}
		//从 RootBeanDefinition 获取所有的PropertyValues
		PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);

		int resolvedAutowireMode = mbd.getResolvedAutowireMode();
		//根据名字获取根据type注入
		if (resolvedAutowireMode == AUTOWIRE_BY_NAME || resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
			MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
			// Add property values based on autowire by name if applicable.
			//如果适用,根据名称添加基于自动装配的属性值。
			if (resolvedAutowireMode == AUTOWIRE_BY_NAME) {
				autowireByName(beanName, mbd, bw, newPvs);
			}
			// Add property values based on autowire by type if applicable.
			//如果适用,根据类型添加基于自动装配的属性值
			if (resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
				autowireByType(beanName, mbd, bw, newPvs);
			}
			pvs = newPvs;
		}
		//后置处理器是否已经注册,初始化好了
		boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
		//是否要检查依赖,默认false
		boolean needsDepCheck = (mbd.getDependencyCheck() != AbstractBeanDefinition.DEPENDENCY_CHECK_NONE);

		PropertyDescriptor[] filteredPds = null;
		if (hasInstAwareBpps) {
			if (pvs == null) {
				pvs = mbd.getPropertyValues();
			}
			//获取后置处理器
			for (BeanPostProcessor bp : getBeanPostProcessors()) {
				//如果是Bean实例化后置处理器
				if (bp instanceof InstantiationAwareBeanPostProcessor) {
					InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
					//
					PropertyValues pvsToUse = ibp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName);
					if (pvsToUse == null) {
						if (filteredPds == null) {
							filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
						}
						//对需要依赖检查的属性进行后置处理
						pvsToUse = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
						if (pvsToUse == null) {
							return;
						}
					}
					pvs = pvsToUse;
				}
			}
		}
		if (needsDepCheck) {
			if (filteredPds == null) {
				filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
			}
			checkDependencies(beanName, mbd, filteredPds, pvs);
		}

		if (pvs != null) {
			//依赖注入入口,讲属性应用到Bean中
			applyPropertyValues(beanName, mbd, bw, pvs);
		}
	}

方法中的重要代码

  • autowireByName : 根据属性名进行注入
  • autowireByType:根据类型注入Bean
  • InstantiationAwareBeanPostProcessor.postProcessPropertyValues :该方法是在工厂将给定的属性值应用于给定的 bean 之前对给定的属性值进行处理,比如:RequiredAnnotationBeanPostProcessor类中对属性的验证。
  • applyPropertyValues:属性的填充

autowireByName是根据名字注入,源码如下

protected void autowireByName(
			String beanName, AbstractBeanDefinition mbd, BeanWrapper bw, MutablePropertyValues pvs) {
		//寻找BeanWrapper中需要依赖的属性
		String[] propertyNames = unsatisfiedNonSimpleProperties(mbd, bw);
		for (String propertyName : propertyNames) {
			if (containsBean(propertyName)) {
				//递归实例化的Bean
				Object bean = getBean(propertyName);
				pvs.add(propertyName, bean);
				//注册依赖的Bean,加入 dependentBeanMap 中
				registerDependentBean(propertyName, beanName);
				if (logger.isTraceEnabled()) {
					logger.trace("Added autowiring by name from bean name '" + beanName +
							"' via property '" + propertyName + "' to bean named '" + propertyName + "'");
				}
			}
			else {
				if (logger.isTraceEnabled()) {
					logger.trace("Not autowiring property '" + propertyName + "' of bean '" + beanName +
							"' by name: no matching bean found");
				}
			}
		}
	}

这个方法很简单,就是先找到依赖的Bean,递归初始化,然后加入 pvs中

//定义“按类型自动装配”(按类型的 bean 属性)行为的抽象方法
	protected void autowireByType(
			String beanName, AbstractBeanDefinition mbd, BeanWrapper bw, MutablePropertyValues pvs) {
		//类型转换器
		TypeConverter converter = getCustomTypeConverter();
		if (converter == null) {
			converter = bw;
		}

		Set<String> autowiredBeanNames = new LinkedHashSet<>(4);
		//找到需要注入的属性
		String[] propertyNames = unsatisfiedNonSimpleProperties(mbd, bw);
		for (String propertyName : propertyNames) {
			try {
				//属性描述
				PropertyDescriptor pd = bw.getPropertyDescriptor(propertyName);
				// Don't try autowiring by type for type Object: never makes sense,
				// even if it technically is a unsatisfied, non-simple property.
				if (Object.class != pd.getPropertyType()) {
					//获取对象的set方法
					MethodParameter methodParam = BeanUtils.getWriteMethodParameter(pd);
					// Do not allow eager init for type matching in case of a prioritized post-processor.

					boolean eager = !(bw.getWrappedInstance() instanceof PriorityOrdered);
					//依赖描述
					DependencyDescriptor desc = new AutowireByTypeDependencyDescriptor(methodParam, eager);
					//【重要】得到依赖的属性的值,存储到 autowiredBeanNames 集合中
					//提供了对集合如:@Autowired private List<A> as; 支持,根据类型走到所有的Bean注入其中
					Object autowiredArgument = resolveDependency(desc, beanName, autowiredBeanNames, converter);
					if (autowiredArgument != null) {
						//添加到pvs
						pvs.add(propertyName, autowiredArgument);
					}
					for (String autowiredBeanName : autowiredBeanNames) {
						//注入依赖的Bean
						registerDependentBean(autowiredBeanName, beanName);
						if (logger.isTraceEnabled()) {
							logger.trace("Autowiring by type from bean name '" + beanName + "' via property '" +
									propertyName + "' to bean named '" + autowiredBeanName + "'");
						}
					}
					//清理掉依赖
					autowiredBeanNames.clear();
				}
			}
			catch (BeansException ex) {
				throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName, propertyName, ex);
			}
		}
	}

看到这,我们大概清楚了,其实在populateBean 方法中会先从RootBeanDefinition 中获取 Bean的属性(PropertyValues),同时也会根据RootBeanDefinition的autowireMode自动注入模式来根据name或者type寻主Bean的依赖的属性。

根据类型注入和根据名字注入都是先从BeanWrapper 中找到Bean的依赖的属性,然后根据属性类型找到匹配的Bean,实现依赖注入。还提供了对集合如:@Autowired private List<A> as; 集合注入的支持。

属性寻找好之后都会封装成 PropertyValues,然后传给applyPropertyValues应用到Bean身上。

AbstractAutowireCapableBeanFactory#applyPropertyValues

我们可以认为前面的代码是在为当前Bean寻找依赖的的属性,封装到 PropertyValues中,在applyPropertyValues中才是把属性应用到当前Bean。

//处理对象之间的引用,使用深拷贝
	protected void applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrapper bw, PropertyValues pvs) {
		if (pvs.isEmpty()) {
			return;
		}

		if (System.getSecurityManager() != null && bw instanceof BeanWrapperImpl) {
			((BeanWrapperImpl) bw).setSecurityContext(getAccessControlContext());
		}

		MutablePropertyValues mpvs = null;
		List<PropertyValue> original;

		if (pvs instanceof MutablePropertyValues) {
			mpvs = (MutablePropertyValues) pvs;
			//判断mpvs中的值是否已经转成了对应的类型,已经转了就可以直接设置值到 BeanWrapper了
			if (mpvs.isConverted()) {
				// Shortcut: use the pre-converted values as-is.
				try {
					//为实例化对象设置属性
					bw.setPropertyValues(mpvs);
					return;
				}
				catch (BeansException ex) {
					throw new BeanCreationException(
							mbd.getResourceDescription(), beanName, "Error setting property values", ex);
				}
			}
			//获取属性值的原始类型
			original = mpvs.getPropertyValueList();
		}
		else {
			//如果类型不是MutablePropertyValues , 就使用原生属性获取方法
			original = Arrays.asList(pvs.getPropertyValues());
		}
		//获取用户自定义的型转换器
		TypeConverter converter = getCustomTypeConverter();
		if (converter == null) {
			converter = bw;
		}
		//解析器:用于 bean 工厂实现的 Helper 类,将 bean 定义对象中包含的值,解析为应用于目标 bean 实例的实际值。
		BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(this, beanName, mbd, converter);

		// Create a deep copy, resolving any references for values.
		//存放类型转换过的属性,把Bean的属性解析值新建拷贝,把拷贝的数据注入到对象
		List<PropertyValue> deepCopy = new ArrayList<>(original.size());
		boolean resolveNecessary = false;
		//类型转换,把属性转换为对应的类型
		for (PropertyValue pv : original) {
			if (pv.isConverted()) {
			//属性值不需要转
				deepCopy.add(pv);
			}
			else {
				//属性名
				String propertyName = pv.getName();
				//原始属性值,它的类型是一个 如: RuntimeBeanReference<otherBean> 引用类型
				Object originalValue = pv.getValue();
				//转换属性值,将引用转换为 IOC 容器中实例化对象引用  OtherBean
				Object resolvedValue = valueResolver.resolveValueIfNecessary(pv, originalValue);
				Object convertedValue = resolvedValue;
				
				boolean convertible = bw.isWritableProperty(propertyName) &&
						!PropertyAccessorUtils.isNestedOrIndexedProperty(propertyName);
				if (convertible) {
					//使用用户自定义的转换器转换
					convertedValue = convertForProperty(resolvedValue, propertyName, bw, converter);
				}
				// Possibly store converted value in merged bean definition,
				// in order to avoid re-conversion for every created bean instance.
				if (resolvedValue == originalValue) {
					if (convertible) {
						//设置转换之后的值到PV ,把依赖的Bean设置给PropertyValue
						pv.setConvertedValue(convertedValue);
					}
					deepCopy.add(pv);
				}
				else if (convertible && originalValue instanceof TypedStringValue &&
						!((TypedStringValue) originalValue).isDynamic() &&
						!(convertedValue instanceof Collection || ObjectUtils.isArray(convertedValue))) {
					pv.setConvertedValue(convertedValue);
					deepCopy.add(pv);
				}
				else {
					resolveNecessary = true;
					//转换好的依赖的属性最终放到一个ArrayList中
					deepCopy.add(new PropertyValue(pv, convertedValue));
				}
			}
		}
		if (mpvs != null && !resolveNecessary) {
			mpvs.setConverted();
		}

		// Set our (possibly massaged) deep copy.
		try {
			//把解析好的属性 设置到 BeanWrapper 中
			bw.setPropertyValues(new MutablePropertyValues(deepCopy));
		}
		catch (BeansException ex) {
			throw new BeanCreationException(
					mbd.getResourceDescription(), beanName, "Error setting property values", ex);
		}
	}

这里主要进行属性转换,然后应用到Bean身上,这里的属性转换比如: 在BeanDefinition中属性可能是用字符串类型来描述的,需要把属性转成真实的原始属性类型。

  • 首先判断属性是否需要转换类型,如果不需要转直接应用于Bean。比如:<property name="otherBean" ref="otherBean" /> 这种属性值其实是个字符串“otherBean” ,需要解析成容器中的OtherBean实例的引用。
  • 如果属性值需要类型转换,比如:属性值是容器中的另外一个Bean,则需要根据属性值解析出引用的对象然后注入到对象的属性上,应用到Bean。

通过 BeanDefinitionValueResolver 类中的 resolveValueIfNecessary()方法中进行属性值的解析, 对属性值的注入是通过 bw.setPropertyValues()方法完成

解析: BeanDefinitionValueResolver#resolveValueIfNecessary

给定一个 PropertyValue根据属性值进行类型解析,必要时解析对工厂中其他 bean 的引用

@Nullable
	public Object resolveValueIfNecessary(Object argName, @Nullable Object value) {
		// We must check each value to see whether it requires a runtime reference
		// to another bean to be resolved.
		//对属性值是引用类型的解析
		if (value instanceof RuntimeBeanReference) {
			//比如:<property name="xx" ref="xxBean" 就是引用类型,会走这里
			RuntimeBeanReference ref = (RuntimeBeanReference) value;
			//对引用类型属性进行解析
			return resolveReference(argName, ref);
		}
		///对属性值是引用容器中另一个 Bean 名称的解析
		else if (value instanceof RuntimeBeanNameReference) {
			String refName = ((RuntimeBeanNameReference) value).getBeanName();
			refName = String.valueOf(doEvaluate(refName));
			//判断容器中是否有这个Bean
			if (!this.beanFactory.containsBean(refName)) {
				throw new BeanDefinitionStoreException(
						"Invalid bean name '" + refName + "' in bean reference for " + argName);
			}
			return refName;
		}
		else if (value instanceof BeanDefinitionHolder) {
			// Resolve BeanDefinitionHolder: contains BeanDefinition with name and aliases.
			//解析 BeanDefinitionHolder:包含带有名称和别名的 BeanDefinition
			BeanDefinitionHolder bdHolder = (BeanDefinitionHolder) value;
			//解析内部 Bean
			return resolveInnerBean(argName, bdHolder.getBeanName(), bdHolder.getBeanDefinition());
		}
		else if (value instanceof BeanDefinition) {
			// Resolve plain BeanDefinition, without contained name: use dummy name.
			//解析纯 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) {
			// May need to resolve contained runtime references.
			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) {
						// Improve the message by showing the context.
						throw new BeanCreationException(
								this.beanDefinition.getResourceDescription(), this.beanName,
								"Error resolving array type for " + argName, ex);
					}
				}
				else {
					elementType = Object.class;
				}
			}
			return resolveManagedArray(argName, (List<?>) value, elementType);
		}
		//对集合类型解析
		else if (value instanceof ManagedList) {
			// May need to resolve contained runtime references.
			return resolveManagedList(argName, (List<?>) value);
		}
		//对Set类型解析
		else if (value instanceof ManagedSet) {
			// May need to resolve contained runtime references.
			return resolveManagedSet(argName, (Set<?>) value);
		}
		//对Map类型解析
		else if (value instanceof ManagedMap) {
			// May need to resolve contained runtime references.
			return resolveManagedMap(argName, (Map<?, ?>) value);
		}
		//对Properties解析
		else if (value instanceof ManagedProperties) {
			Properties original = (Properties) value;
			Properties copy = new Properties();
			original.forEach((propKey, propValue) -> {
				if (propKey instanceof TypedStringValue) {
					propKey = evaluate((TypedStringValue) propKey);
				}
				if (propValue instanceof TypedStringValue) {
					propValue = evaluate((TypedStringValue) propValue);
				}
				if (propKey == null || propValue == null) {
					throw new BeanCreationException(
							this.beanDefinition.getResourceDescription(), this.beanName,
							"Error converting Properties key/value pair for " + argName + ": resolved to null");
				}
				copy.put(propKey, propValue);
			});
			return copy;
		}
		//解析字符串类型的属性值
		else if (value instanceof TypedStringValue) {
			// Convert value to target type here.
			TypedStringValue typedStringValue = (TypedStringValue) value;
			Object valueObject = evaluate(typedStringValue);
			try {
				//目标类型
				Class<?> resolvedTargetType = resolveTargetType(typedStringValue);
				if (resolvedTargetType != null) {
					//目标类型进行解析
					return this.typeConverter.convertIfNecessary(valueObject, resolvedTargetType);
				}
				else {
					//类型没获取到,就返回Object类型
					return valueObject;
				}
			}
			catch (Throwable ex) {
				// Improve the message by showing the context.
				throw new BeanCreationException(
						this.beanDefinition.getResourceDescription(), this.beanName,
						"Error converting typed String value for " + argName, ex);
			}
		}
		else if (value instanceof NullBean) {
			return null;
		}
		else {
			return evaluate(value);
		}
	}

这个方法中就是根据属性的值的类型进行解析,如:String,Array,List,Set,Map的类型,比较复杂的就是属性值依赖的是一个Bean,那么就需要根据依赖的Bean的名字找到容器中的Bean的实例,查找如下:

/**
	 * Resolve a reference to another bean in the factory.
	 */
	 //关联对象的解析
	@Nullable
	private Object resolveReference(Object argName, RuntimeBeanReference ref) {
		try {
			Object 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");
				}
				//如果对象在父容器中,从父容器获取
				bean = this.beanFactory.getParentBeanFactory().getBean(refName);
			}
			else {
				//[重要]根据依赖的Bean的名字,从当前容器中获取Bean
				bean = this.beanFactory.getBean(refName);
				//把依赖的Bean的实例和当前对象建议依赖关系,使用 dependentBeanMap 去维护关系
				this.beanFactory.registerDependentBean(refName, this.beanName);
			}
			if (bean instanceof NullBean) {
				bean = null;
			}
			return bean;
		}
		catch (BeansException ex) {
			throw new BeanCreationException(
					this.beanDefinition.getResourceDescription(), this.beanName,
					"Cannot resolve reference to bean '" + ref.getBeanName() + "' while setting " + argName, ex);
		}
	}


 * For each element in the managed array, resolve reference if necessary.
	 */
	 //解析数组
	private Object resolveManagedArray(Object argName, List<?> ml, Class<?> elementType) {
		Object resolved = Array.newInstance(elementType, ml.size());
		for (int i = 0; i < ml.size(); i++) {
			Array.set(resolved, i, resolveValueIfNecessary(new KeyedArgName(argName, i), ml.get(i)));
		}
		return resolved;
	}

	/**
	 * For each element in the managed list, resolve reference if necessary.
	 */
	 //解析List
	private List<?> resolveManagedList(Object argName, List<?> ml) {
		List<Object> resolved = new ArrayList<>(ml.size());
		for (int i = 0; i < ml.size(); i++) {
			resolved.add(resolveValueIfNecessary(new KeyedArgName(argName, i), ml.get(i)));
		}
		return resolved;
	}

	/**
	 * For each element in the managed set, resolve reference if necessary.
	 */
	 //解析set
	private Set<?> resolveManagedSet(Object argName, Set<?> ms) {
		Set<Object> resolved = new LinkedHashSet<>(ms.size());
		int i = 0;
		for (Object m : ms) {
			resolved.add(resolveValueIfNecessary(new KeyedArgName(argName, i), m));
			i++;
		}
		return resolved;
	}

	/**
	 * For each element in the managed map, resolve reference if necessary.
	 */
	 //解析Map
	private Map<?, ?> resolveManagedMap(Object argName, Map<?, ?> mm) {
		Map<Object, Object> resolved = new LinkedHashMap<>(mm.size());
		mm.forEach((key, value) -> {
			Object resolvedKey = resolveValueIfNecessary(argName, key);
			Object resolvedValue = resolveValueIfNecessary(new KeyedArgName(argName, key), value);
			resolved.put(resolvedKey, resolvedValue);
		});
		return resolved;
	}

属性值解析完成之后 是封装成一个 MutablePropertyValues,通过 BeanWrapperImpl.setPropertyValues()方法完成值的注入,BeanWrapperImpl中注入方法又是由AbstractPropertyAccessor#setPropertyValue(java.lang.String, java.lang.Object)去完成。

AbstractPropertyAccessor#setPropertyValue

@Override
	public void setPropertyValues(PropertyValues pvs, boolean ignoreUnknown, boolean ignoreInvalid)
			throws BeansException {

		List<PropertyAccessException> propertyAccessExceptions = null;
		//拿到所有的属性列表
		List<PropertyValue> propertyValues = (pvs instanceof MutablePropertyValues ?
				((MutablePropertyValues) pvs).getPropertyValueList() : Arrays.asList(pvs.getPropertyValues()));
		for (PropertyValue pv : propertyValues) {
			try {
				// This method may throw any BeansException, which won't be caught
				// here, if there is a critical failure such as no matching field.
				// We can attempt to deal only with less serious exceptions.
				//设置属性值
				setPropertyValue(pv);
			}
			...省略...

@Override
	public void setPropertyValue(String propertyName, @Nullable Object value) throws BeansException {
		//属性访问器
		AbstractNestablePropertyAccessor nestedPa;
		try {
			nestedPa = getPropertyAccessorForPropertyPath(propertyName);
		}
		catch (NotReadablePropertyException ex) {
			throw new NotWritablePropertyException(getRootClass(), this.nestedPath + propertyName,
					"Nested property in path '" + propertyName + "' does not exist", ex);
		}
		//属性助手
		PropertyTokenHolder tokens = getPropertyNameTokens(getFinalPath(nestedPa, propertyName));
		//通过属性访问器为属性设置值
		nestedPa.setPropertyValue(tokens, new PropertyValue(propertyName, value));
	}

这里看到,属性的注入交给了 AbstractNestablePropertyAccessor 属性访问器去完成

protected void setPropertyValue(PropertyTokenHolder tokens, PropertyValue pv) throws BeansException {
		if (tokens.keys != null) {
			processKeyedProperty(tokens, pv);
		}
		else {
			//走这里
			processLocalProperty(tokens, pv);
		}
	}

private void processLocalProperty(PropertyTokenHolder tokens, PropertyValue pv) {
		//属性处理器
		PropertyHandler ph = getLocalPropertyHandler(tokens.actualName);
		if (ph == null || !ph.isWritable()) {
			if (pv.isOptional()) {
				if (logger.isDebugEnabled()) {
					logger.debug("Ignoring optional value for property '" + tokens.actualName +
							"' - property not found on bean class [" + getRootClass().getName() + "]");
				}
				return;
			}
			else {
				throw createNotWritablePropertyException(tokens.canonicalName);
			}
		}

		Object oldValue = null;
		try {
			//原生值
			Object originalValue = pv.getValue();
			Object valueToApply = originalValue;
			...省略...
			//这是重点,通过 PropertyHandler 把属性值设置给对象
			ph.setValue(valueToApply);
		}
		catch (TypeMismatchException ex) {
			throw ex;
		}

这里的属性值通过 PropertyHandler去设置

@Override
	public void setValue(@Nullable Object value) throws Exception {
		//得到属性的set方法
		Method writeMethod = (this.pd instanceof GenericTypeAwarePropertyDescriptor ?
				((GenericTypeAwarePropertyDescriptor) this.pd).getWriteMethodForActualAccess() :
				this.pd.getWriteMethod());
		if (System.getSecurityManager() != null) {
			AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
				ReflectionUtils.makeAccessible(writeMethod);
				return null;
			});
			try {
				AccessController.doPrivileged((PrivilegedExceptionAction<Object>)
						() -> writeMethod.invoke(getWrappedInstance(), value), acc);
			}
			catch (PrivilegedActionException ex) {
				throw ex.getException();
			}
		}
		else {
			//设置访问权限
			ReflectionUtils.makeAccessible(writeMethod);
			//调用set方法把属性值设置进去
			writeMethod.invoke(getWrappedInstance(), value);
		}
	}

这里就是得到属性的set方法,然后调用set方法把值注入进去。

构造器注入参数

在之前分析Bean的创建的时候我们就说到,在 AbstractAutowireCapableBeanFactory#createBeanInstance 中会通过反射获取到Bean的构造器,如果是有参构造就会走autowireConstructor 方法,通过有参构造创建实例

protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
		// Make sure bean class is actually resolved at this point.
		Class<?> beanClass = resolveBeanClass(mbd, beanName);

		...省略...
		// Need to determine the constructor...
		Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
		if (ctors != null ||
				mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR ||
				mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args))  {
				//【重要】构造器注入参数
			return autowireConstructor(beanName, mbd, ctors, args);
		}

		// No special handling: simply use no-arg constructor.
		return instantiateBean(beanName, mbd);
	}

在autowireConstructor方法中会通过 ConstructorResolver 对构造器参数进行解析

protected BeanWrapper autowireConstructor(
			String beanName, RootBeanDefinition mbd, @Nullable Constructor<?>[] ctors, @Nullable Object[] explicitArgs) {
		//构造器解析器,注入
		return new ConstructorResolver(this).autowireConstructor(beanName, mbd, ctors, explicitArgs);
	}


public BeanWrapper autowireConstructor(final String beanName, final RootBeanDefinition mbd,
			@Nullable Constructor<?>[] chosenCtors, @Nullable final Object[] explicitArgs) {

		...省略...
			else {
				//得到构造器参数
				ConstructorArgumentValues cargs = mbd.getConstructorArgumentValues();
				//得到构造器参数值
				resolvedValues = new ConstructorArgumentValues();
				//解析参数值【重要】
				minNrOfArgs = resolveConstructorArguments(beanName, mbd, bw, cargs, resolvedValues);
			}
		...省略...
try {
			//实例化策略
			final InstantiationStrategy strategy = beanFactory.getInstantiationStrategy();
			Object beanInstance;

			if (System.getSecurityManager() != null) {
				final Constructor<?> ctorToUse = constructorToUse;
				final Object[] argumentsToUse = argsToUse;
				beanInstance = AccessController.doPrivileged((PrivilegedAction<Object>) () ->
						strategy.instantiate(mbd, beanName, beanFactory, ctorToUse, argumentsToUse),
						beanFactory.getAccessControlContext());
			}
			else {
				//实例化对象,根据有参构造器,使用反射创建实例
				beanInstance = strategy.instantiate(mbd, beanName, this.beanFactory, constructorToUse, argsToUse);
			}

			bw.setBeanInstance(beanInstance);
			return bw;
		}

resolveConstructorArguments 方法中又通过 BeanDefinitionValueResolver来解析属性值,有参数的值了,就会走反射,根据有参构造器创建实例返回。

private int resolveConstructorArguments(String beanName, RootBeanDefinition mbd, BeanWrapper bw,
			ConstructorArgumentValues cargs, ConstructorArgumentValues resolvedValues) {

		TypeConverter customConverter = this.beanFactory.getCustomTypeConverter();
		TypeConverter converter = (customConverter != null ? customConverter : bw);
		//属性值的解析器
		BeanDefinitionValueResolver valueResolver =
				new BeanDefinitionValueResolver(this.beanFactory, beanName, mbd, converter);

		...省略...
		for (ConstructorArgumentValues.ValueHolder valueHolder : cargs.getGenericArgumentValues()) {
			if (valueHolder.isConverted()) {
				resolvedValues.addGenericArgumentValue(valueHolder);
			}
			else {
			//为构造器参数值做解析,这里和之前分析的set注入的属性值解析就一样了
				Object resolvedValue =
						valueResolver.resolveValueIfNecessary("constructor argument", valueHolder.getValue());
				ConstructorArgumentValues.ValueHolder resolvedValueHolder = new ConstructorArgumentValues.ValueHolder(
						resolvedValue, valueHolder.getType(), valueHolder.getName());
				resolvedValueHolder.setSource(valueHolder);
				resolvedValues.addGenericArgumentValue(resolvedValueHolder);
			}
		}

		return minNrOfArgs;
	}

因为之前 resolveValueIfNecessary方法已经分析过了,这里就不多说了。

总结

到这里属性注入的流程就分析完了,总结都在这个图上

Spring Bean的实例化之属性注入源码剖析过程

喜欢的话就给个好评吧,你的肯定是我坚持写作最大的动力,来吧兄弟们,给我一点动力

以上就是Spring Bean的实例化之属性注入源码剖析过程的详细内容,更多关于Spring Bean 实例化属性注入的资料请关注三水点靠木其它相关文章!

Java/Android 相关文章推荐
解决Maven项目中 Invalid bound statement 无效的绑定问题
Jun 15 Java/Android
SpringBoot生成License的实现示例
Jun 16 Java/Android
Java数据结构之链表相关知识总结
Jun 18 Java/Android
SpringBoot集成Redis,并自定义对象序列化操作
Jun 22 Java/Android
dubbo集成zipkin获取Traceid的实现
Jul 26 Java/Android
聊聊SpringBoot自动装配的魔力
Nov 17 Java/Android
使用Java去实现超市会员管理系统
Mar 18 Java/Android
Java Spring Boot 正确读取配置文件中的属性的值
Apr 20 Java/Android
mybatis 获取更新记录的id
May 20 Java/Android
Mybatis-plus配置分页插件返回统一结果集
Jun 21 Java/Android
Java 多线程并发FutureTask
Jun 28 Java/Android
SpringBoot深入分析讲解监听器模式下
Jul 15 Java/Android
Java输出Hello World完美过程解析
Java方法重载和方法重写的区别到底在哪?
Jun 11 #Java/Android
Java 将PPT幻灯片转为HTML文件的实现思路
springboot中一些比较常用的注解总结
Jun 11 #Java/Android
分享一些Java的常用工具
浅谈Java实现分布式事务的三种方案
源码解读Spring-Integration执行过程
You might like
DC《小丑》11项提名领跑奥斯卡 Netflix成第92届奥斯卡提名最大赢家
2020/04/09 欧美动漫
PHP 循环列出目录内容的函数代码
2010/05/26 PHP
Extjs中DisplayField的日期或者数字格式化扩展
2010/09/03 Javascript
Js 回车换行处理的办法及replace方法应用
2013/01/24 Javascript
Extjs实现进度条的两种便捷方式
2013/09/26 Javascript
JS计算网页停留时间代码
2014/04/28 Javascript
js数值计算时使用parseInt进行数据类型转换(jquery)
2014/10/07 Javascript
Node.js 文件夹目录结构创建实例代码
2016/07/08 Javascript
jQuery实现表格行和列的动态添加与删除方法【测试可用】
2016/08/01 Javascript
Bootstrap作品展示站点实战项目2
2016/10/14 Javascript
Vue系列:通过vue-router如何传递参数示例
2017/01/16 Javascript
JavaScript组件开发之输入框加候选框
2017/03/10 Javascript
表格展示利器 Bootstrap Table实例代码
2017/09/06 Javascript
angularjs实现简单的购物车功能
2017/09/21 Javascript
使用DataTable插件实现异步加载数据
2017/11/19 Javascript
vue2.0+ 从插件开发到npm发布的示例代码
2018/04/28 Javascript
node.js中事件触发器events的使用方法实例分析
2019/11/23 Javascript
[07:47]DOTA2国际邀请赛采访专栏:探访Valve总部
2013/08/08 DOTA
python求素数示例分享
2014/02/16 Python
零基础写python爬虫之使用Scrapy框架编写爬虫
2014/11/07 Python
Python脚本实现代码行数统计代码分享
2015/03/10 Python
利用Python绘制数据的瀑布图的教程
2015/04/07 Python
Python ValueError: invalid literal for int() with base 10 实用解决方法
2015/06/21 Python
Python中py文件引用另一个py文件变量的方法
2018/04/29 Python
Pycharm更换python解释器的方法
2018/10/29 Python
波兰最大的儿童服装连锁店之一:5.10.15.
2018/02/11 全球购物
Unineed中文官网:高端护肤美妆与时尚配饰,英国直邮
2020/07/23 全球购物
逃课上网检讨书
2014/02/20 职场文书
研究生求职自荐书
2014/06/23 职场文书
师德师风自我评价范文
2014/09/11 职场文书
安全生产工作汇报
2014/10/28 职场文书
支行行长竞聘报告
2014/11/06 职场文书
前台岗位职责
2015/02/13 职场文书
禁毒主题班会教案
2015/08/14 职场文书
红领巾广播站广播稿
2015/08/19 职场文书
Qt自定义Plot实现曲线绘制的详细过程
2021/11/02 Python