Spring 5.x Source trip twenty-five getBean Detailed ten-arg constructor automatic assembly autowireConstructor

The method of automatic assembly configured autowireConstructor

Also calls internal ConstructorResolvermethods.

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

		return new ConstructorResolver(this).autowireConstructor(beanName, mbd, ctors, explicitArgs);
	}

ConstructorResolver的autowireConstructor

This and Recall factory method to instantiate instantiateUsingFactoryMethodlike, there are a few places are not the same.

  • Factory method will traverse all the way to finish, and then find the smallest difference method parameter types, and automatic assembly construction method to find a satisfying the condition stopped.
  • Parameter Type differencing algorithm is not the same, the factory method is a strict method getAssignabilityWeight, and a method of automatic assembly is loose getTypeDifferenceWeight.

ConstructorResolver的getTypeDifferenceWeight

Gets the parameter type and parameter types of the original difference, but still with the original type of priority, also because of differences in value-1024.

//获取类型差异值
		public int getTypeDifferenceWeight(Class<?>[] paramTypes) {
			
			//找出参数类型,和原始参数类型的差异,选最小的
			int typeDiffWeight = MethodInvoker.getTypeDifferenceWeight(paramTypes, this.arguments);
			int rawTypeDiffWeight = MethodInvoker.getTypeDifferenceWeight(paramTypes, this.rawArguments) - 1024;
			return Math.min(rawTypeDiffWeight, typeDiffWeight);
		}

MethodInvoker的getTypeDifferenceWeight

Here is the parameter type defined types and methods of comparing the incoming parameters, if not inherited, directly returns the largest difference, if it is inherited, acquired the parent incoming parameters, if the parent class type is the parameter type, the difference +2, otherwise determine whether the type of parent class is a subclass of the type parameter, then the difference is +2, and then continue to look for the parent of the parent class, until no parent so far, the last parameter is the interface type if you find the difference +1, and then return the difference.

public static int getTypeDifferenceWeight(Class<?>[] paramTypes, Object[] args) {
		int result = 0;
		for (int i = 0; i < paramTypes.length; i++) {
			if (!ClassUtils.isAssignableValue(paramTypes[i], args[i])) {
				return Integer.MAX_VALUE;//有参数类型不匹配直接返回最大差异
			}
			if (args[i] != null) {
				Class<?> paramType = paramTypes[i];
				Class<?> superClass = args[i].getClass().getSuperclass();//获得传入参数的父类来比较
				while (superClass != null) {
					if (paramType.equals(superClass)) {//参数类型等于父类型的,差异+2
						result = result + 2;
						superClass = null;
					}
					else if (ClassUtils.isAssignable(paramType, superClass)) {//superClass是paramType的子类类型,差异+2,可能还有paramType的子类类型,再尝试获取
						result = result + 2;
						superClass = superClass.getSuperclass();
					}
					else {
						superClass = null;
					}
				}
				if (paramType.isInterface()) {//参数类型是接口类型,差异+1
					result = result + 1;
				}
			}
		}
		return result;
	}

ClassUtils of isAssignable whether the comparison is of the same type, or subclass

Comparison of two main classes Class is not the same or subclass, i.e., rhsTypeis not lhsTypea sub-class or similar.

public static boolean isAssignable(Class<?> lhsType, Class<?> rhsType) {
		Assert.notNull(lhsType, "Left-hand side type must not be null");
		Assert.notNull(rhsType, "Right-hand side type must not be null");
		if (lhsType.isAssignableFrom(rhsType)) {
			return true;
		}
		if (lhsType.isPrimitive()) {
			Class<?> resolvedPrimitive = primitiveWrapperTypeMap.get(rhsType);
			if (lhsType == resolvedPrimitive) {
				return true;
			}
		}
		else {
			Class<?> resolvedWrapper = primitiveTypeToWrapperMap.get(rhsType);
			if (resolvedWrapper != null && lhsType.isAssignableFrom(resolvedWrapper)) {
				return true;
			}
		}
		return false;
	}
Class的isAssignableFrom

a.isAssignableFrom(b), That bis not aof the same kind or subclass.
Here Insert Picture Description
Well, here today, we hope to help study and understand, do not spray the Great God see, understand only their own learning, limited capacity, please excuse.

Published 235 original articles · won praise 74 · views 30000 +

Guess you like

Origin blog.csdn.net/wangwei19871103/article/details/105117687