14、Spring之Bean生命周期~推断构造方法

推断构造方法

  spring在创建Bean对象的会调用createBeanInstance()方法进行实例化,接下来我们将从源码层面分析一下createBeanInstance()方法,废话不多说,上代码:

/**
 * Create a new instance for the specified bean, using an appropriate instantiation strategy:
 * factory method, constructor autowiring, or simple instantiation.
 *
 * @param beanName the name of the bean
 * @param mbd      the bean definition for the bean
 * @param args     explicit arguments to use for constructor or factory method invocation
 * @return a BeanWrapper for the new instance
 * @see #obtainFromSupplier
 * @see #instantiateUsingFactoryMethod
 * @see #autowireConstructor
 * @see #instantiateBean
 */
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);

	if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed()) {
		throw new BeanCreationException(mbd.getResourceDescription(), beanName,
				"Bean class isn't public, and non-public access not allowed: " + beanClass.getName());
	}

	// BeanDefinition中添加了Supplier,则调用Supplier来得到对象
	Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
	if (instanceSupplier != null) {
		return obtainFromSupplier(instanceSupplier, beanName);
	}

	// @Bean对应的BeanDefinition
	if (mbd.getFactoryMethodName() != null) {
		return instantiateUsingFactoryMethod(beanName, mbd, args);
	}

	// Shortcut when re-creating the same bean...
	// 一个原型BeanDefinition,会多次来创建Bean,那么就可以把该BeanDefinition所要使用的构造方法缓存起来,避免每次都进行构造方法推断
	boolean resolved = false;
	boolean autowireNecessary = false;
	// 如果构造器方法参数 等于 空
	if (args == null) {
		synchronized (mbd.constructorArgumentLock) {
			if (mbd.resolvedConstructorOrFactoryMethod != null) {
				resolved = true;
				// autowireNecessary表示有没有必要要进行注入,比如当前BeanDefinition用的是无参构造方法,那么autowireNecessary为false,否则为true,表示需要给构造方法参数注入值
				autowireNecessary = mbd.constructorArgumentsResolved;
			}
		}
	}
	if (resolved) {
		// 如果确定了当前BeanDefinition的构造方法,那么看是否需要进行对构造方法进行参数的依赖注入(构造方法注入)
		if (autowireNecessary) {
			// 方法内会拿到缓存好的构造方法的入参
			return autowireConstructor(beanName, mbd, null, null);
		} else {
			// 构造方法已经找到了,但是没有参数,那就表示是无参,直接进行实例化
			return instantiateBean(beanName, mbd);
		}
	}

//		logger.info("=====推断构造方法=====");

	// 如果没有找过构造方法,那么就开始找了

	// Candidate constructors for autowiring?
	// 提供一个扩展点,可以利用SmartInstantiationAwareBeanPostProcessor来控制用beanClass中的哪些构造方法
	// 比如AutowiredAnnotationBeanPostProcessor会把加了@Autowired注解的构造方法找出来,具体看代码实现会更复杂一点
	Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);

	// 如果推断出来了构造方法,则需要给构造方法赋值,也就是给构造方法参数赋值,也就是构造方法注入
	// 如果没有推断出来构造方法,但是autowiremode为AUTOWIRE_CONSTRUCTOR,则也可能需要给构造方法赋值,因为不确定是用无参的还是有参的构造方法
	// 如果通过BeanDefinition指定了构造方法参数值,那肯定就是要进行构造方法注入了
	// 如果调用getBean的时候传入了构造方法参数值,那肯定就是要进行构造方法注入了
	if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||
			mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
		return autowireConstructor(beanName, mbd, ctors, args);
	}

	// Preferred constructors for default construction?
	ctors = mbd.getPreferredConstructors();
	if (ctors != null) {
		return autowireConstructor(beanName, mbd, ctors, null);
	}

	// No special handling: simply use no-arg constructor.
	// 不匹配以上情况,则直接使用无参构造方法
	return instantiateBean(beanName, mbd);
}

通过上述代码我们可以看到:

  1. 首先判断,如果BeanDefinition设置啦Supplier,通过Supplier接口的get方法获取对象,(Supplier接口使用不多,这里不展开详细讲解);
  2. 如果是@Bean调用instantiateUsingFactoryMethod()方法
  3. 再往下,如果调用createBeanInstance()时没有指定构造器方法参数,判断BeanDefinition中是否有构造方法的缓存;
  4. 如果BeanDefinition中有构造方法的缓存,再判断缓存的构造方法是否需要参数;
  5. 如果缓存的构造方法需要参数,调用autowireConstructor()方法
  6. 如果缓存的构造方法不需要参数,调用instantiateBean()方法;
  7. 如果没有缓存,调用determineConstructorsFromBeanPostProcessors()方法,查找添加@Autowired注解的构造方法;
  8. 再往下这个判断,如果有@Autowired注解的构造方法,或者调用createBeanInstance()时有指定构造器方法参数,等(其他两判断没研究明白),调用autowireConstructor()方法
  9. 如果没有@Autowired注解的构造方法,且调用createBeanInstance()时没有指定构造器方法参数,调用instantiateBean()方法尝试使用无参构造器;

determineConstructorsFromBeanPostProcessors()方法

determineConstructorsFromBeanPostProcessors()方法详解

/**
 * Determine candidate constructors to use for the given bean, checking all registered
 * {@link SmartInstantiationAwareBeanPostProcessor SmartInstantiationAwareBeanPostProcessors}.
 *
 * @param beanClass the raw class of the bean
 * @param beanName  the name of the bean
 * @return the candidate constructors, or {@code null} if none specified
 * @throws org.springframework.beans.BeansException in case of errors
 * @see org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor#determineCandidateConstructors
 */
@Nullable
protected Constructor<?>[] determineConstructorsFromBeanPostProcessors(@Nullable Class<?> beanClass, String beanName)
		throws BeansException {

	if (beanClass != null && hasInstantiationAwareBeanPostProcessors()) {
		for (SmartInstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().smartInstantiationAware) {
			Constructor<?>[] ctors = bp.determineCandidateConstructors(beanClass, beanName);
			if (ctors != null) {
				return ctors;
			}
		}
	}
	return null;
}

这里我们可以看到,这里是拿到所有实现SmartInstantiationAwareBeanPostProcessor接口的Bean对象,调用determineCandidateConstructors()方法,这里调用的其实是AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors()方法,

@Override
@Nullable
public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, final String beanName)
		throws BeanCreationException {

	// Let's check for lookup methods here...
	if (!this.lookupMethodsChecked.contains(beanName)) {
		// 判断beanClass是不是java.开头的类,比如String
		if (AnnotationUtils.isCandidateClass(beanClass, Lookup.class)) {
			try {
				Class<?> targetClass = beanClass;
				do {
					// 遍历targetClass中的method,查看是否写了@Lookup方法
					ReflectionUtils.doWithLocalMethods(targetClass, method -> {
						Lookup lookup = method.getAnnotation(Lookup.class);
						if (lookup != null) {
							Assert.state(this.beanFactory != null, "No BeanFactory available");

							// 将当前method封装成LookupOverride并设置到BeanDefinition的methodOverrides中
							LookupOverride override = new LookupOverride(method, lookup.value());
							try {
								RootBeanDefinition mbd = (RootBeanDefinition)
										this.beanFactory.getMergedBeanDefinition(beanName);
								mbd.getMethodOverrides().addOverride(override);
							} catch (NoSuchBeanDefinitionException ex) {
								throw new BeanCreationException(beanName,
										"Cannot apply @Lookup to beans without corresponding bean definition");
							}
						}
					});
					targetClass = targetClass.getSuperclass();
				}
				while (targetClass != null && targetClass != Object.class);

			} catch (IllegalStateException ex) {
				throw new BeanCreationException(beanName, "Lookup method resolution failed", ex);
			}
		}
		this.lookupMethodsChecked.add(beanName);
	}

	// Quick check on the concurrent map first, with minimal locking.
	Constructor<?>[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);
	if (candidateConstructors == null) {
		// Fully synchronized resolution now...
		synchronized (this.candidateConstructorsCache) {
			candidateConstructors = this.candidateConstructorsCache.get(beanClass);
			if (candidateConstructors == null) {
				Constructor<?>[] rawCandidates;
				try {
					// 拿到所有的构造方法
					rawCandidates = beanClass.getDeclaredConstructors();
				} catch (Throwable ex) {
					throw new BeanCreationException(beanName,
							"Resolution of declared constructors on bean Class [" + beanClass.getName() +
									"] from ClassLoader [" + beanClass.getClassLoader() + "] failed", ex);
				}
				List<Constructor<?>> candidates = new ArrayList<>(rawCandidates.length);

				// 用来记录required为true的构造方法,一个类中只能有一个required为true的构造方法
				Constructor<?> requiredConstructor = null;
				// 用来记录默认无参的构造方法
				Constructor<?> defaultConstructor = null;
				// kotlin相关,不用管
				Constructor<?> primaryConstructor = BeanUtils.findPrimaryConstructor(beanClass);
				int nonSyntheticConstructors = 0;

				// 遍历每个构造方法
				for (Constructor<?> candidate : rawCandidates) {
					if (!candidate.isSynthetic()) {
						// 记录一下普通的构造方法
						nonSyntheticConstructors++;
					} else if (primaryConstructor != null) {
						continue;
					}

					// 当前遍历的构造方法是否写了@Autowired
					MergedAnnotation<?> ann = findAutowiredAnnotation(candidate);
					if (ann == null) {
						// 如果beanClass是代理类,则得到被代理的类的类型
						Class<?> userClass = ClassUtils.getUserClass(beanClass);
						if (userClass != beanClass) {
							try {
								Constructor<?> superCtor =
										userClass.getDeclaredConstructor(candidate.getParameterTypes());
								// 继续查找被代理类的构造方法是否有@Autowired,@Value注解;
								ann = findAutowiredAnnotation(superCtor);
							} catch (NoSuchMethodException ex) {
								// Simply proceed, no equivalent superclass constructor found...
							}
						}
					}

					// 当前构造方法上加了@Autowired
					if (ann != null) {
						// 整个类中如果有一个required为true的构造方法,那就不能有其他的加了@Autowired的构造方法
						if (requiredConstructor != null) {
							throw new BeanCreationException(beanName,
									"Invalid autowire-marked constructor: " + candidate +
											". Found constructor with 'required' Autowired annotation already: " +
											requiredConstructor);
						}

						boolean required = determineRequiredStatus(ann);
						if (required) {
							if (!candidates.isEmpty()) {
								throw new BeanCreationException(beanName,
										"Invalid autowire-marked constructors: " + candidates +
												". Found constructor with 'required' Autowired annotation: " +
												candidate);
							}
							// 记录唯一一个required为true的构造方法
							requiredConstructor = candidate;
						}
						// 记录所有加了@Autowired的构造方法,不管required是true还是false
						// 如果默认无参的构造方法上也加了@Autowired,那么也会加到candidates中
						candidates.add(candidate);

						// 从上面代码可以得到一个结论,在一个类中,要么只能有一个required为true的构造方法,要么只能有一个或多个required为false的方法
					} else if (candidate.getParameterCount() == 0) {
						// 记录唯一一个无参的构造方法
						defaultConstructor = candidate;
					}

					// 有可能存在有参、并且没有添加@Autowired的构造方法
				}


				if (!candidates.isEmpty()) {
					// Add default constructor to list of optional constructors, as fallback.
					// 如果不存在一个required为true的构造方法,则所有required为false的构造方法和无参构造方法都是合格的
					if (requiredConstructor == null) {
						if (defaultConstructor != null) {
							candidates.add(defaultConstructor);
						} else if (candidates.size() == 1 && logger.isInfoEnabled()) {
							logger.info("Inconsistent constructor declaration on bean with name '" + beanName +
									"': single autowire-marked constructor flagged as optional - " +
									"this constructor is effectively required since there is no " +
									"default constructor to fall back to: " + candidates.get(0));
						}
					}
					// 如果只存在一个required为true的构造方法,那就只有这一个是合格的
					candidateConstructors = candidates.toArray(new Constructor<?>[0]);
				}
				// 没有添加了@Autowired注解的构造方法,并且类中只有一个构造方法,并且是有参的
				else if (rawCandidates.length == 1 && rawCandidates[0].getParameterCount() > 0) {
					candidateConstructors = new Constructor<?>[]{rawCandidates[0]};
				}
				// primaryConstructor不用管
				else if (nonSyntheticConstructors == 2 && primaryConstructor != null &&
						defaultConstructor != null && !primaryConstructor.equals(defaultConstructor)) {
					candidateConstructors = new Constructor<?>[]{primaryConstructor, defaultConstructor};
				}
				// primaryConstructor不用管
				else if (nonSyntheticConstructors == 1 && primaryConstructor != null) {
					candidateConstructors = new Constructor<?>[]{primaryConstructor};
				} else {
					// 如果有多个有参构造方法、并且没有添加@Autowired的构造方法,是会返回空的
					candidateConstructors = new Constructor<?>[0];
				}
				this.candidateConstructorsCache.put(beanClass, candidateConstructors);
			}
		}
	}
	return (candidateConstructors.length > 0 ? candidateConstructors : null);
}

通过上述代码,我们可以看到:

  1. 首先判断@Lookup注解,
  2. 将当前method封装成LookupOverride并设置到BeanDefinition的methodOverrides中;
  3. 再往下,拿到所有的构造方法进行遍历;
  4. 通过findAutowiredAnnotation()方法,判断构造器是否包含@Autowired注解;
  5. 如果当前构造方法没有添加@Autowired注解,判断当前类是否是代理类,如果是代理类,继续查找被代理类的构造方法是否有@Autowired注解;
  6. 如果当前构造方法有添加@Autowired注解,判断这个类是否存在@Autowired(required = true)的构造方法,存在报错;如果当前这个构造方法的注解的是@Autowired(required = true),判断是否有其他有@Autowired注解的构造器,有报错;(这个判断的大体意思是:一个类中有@Autowired(required = true)注解的构造方法,其他构造方法就不能有@Autowired注解;但是允许存在多个@Autowired(required = flase)注解的构造方法);
  7. 如果构造器没有@Autowired注解,且构造方法参数是0,记录无参构造器标记;
  8. 如果不存在@Autowired(required = true)的构造方法,则所有required为false的构造方法和无参构造方法都是合格的;
  9. 如果存在@Autowired(required = true)的构造方法,那就只有这一个是合格的;
  10. 如果所有构造方法都没有@Autowired,且 类中只有一个有参的构造方法;
  11. 最后如果有多个有参构造方法、并且没有添加@Autowired的构造方法,是会返回空的;

返回createBeanInstance()方法

autowireConstructor()方法

autowireConstructor()方法详解

/**
 * "autowire constructor" (with constructor arguments by type) behavior.
 * Also applied if explicit constructor argument values are specified,
 * matching all remaining arguments with beans from the bean factory.
 * <p>This corresponds to constructor injection: In this mode, a Spring
 * bean factory is able to host components that expect constructor-based
 * dependency resolution.
 *
 * @param beanName     the name of the bean
 * @param mbd          the bean definition for the bean
 * @param ctors        the chosen candidate constructors
 * @param explicitArgs argument values passed in programmatically via the getBean method,
 *                     or {@code null} if none (-> use constructor argument values from bean definition)
 * @return a BeanWrapper for the new instance
 */
protected BeanWrapper autowireConstructor(
		String beanName, RootBeanDefinition mbd, @Nullable Constructor<?>[] ctors, @Nullable Object[] explicitArgs) {

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

通过上述代码,我们可以看到,这里会调用ConstructorResolver类的autowireConstructor()方法,接下来,我们详细分析ConstructorResolver类的autowireConstructor()方法

/**
 * "autowire constructor" (with constructor arguments by type) behavior.
 * Also applied if explicit constructor argument values are specified,
 * matching all remaining arguments with beans from the bean factory.
 * <p>This corresponds to constructor injection: In this mode, a Spring
 * bean factory is able to host components that expect constructor-based
 * dependency resolution.
 * @param beanName the name of the bean
 * @param mbd the merged bean definition for the bean
 * @param chosenCtors chosen candidate constructors (or {@code null} if none)
 * @param explicitArgs argument values passed in programmatically via the getBean method,
 * or {@code null} if none (-> use constructor argument values from bean definition)
 * @return a BeanWrapper for the new instance
 */
public BeanWrapper autowireConstructor(String beanName, RootBeanDefinition mbd,
		@Nullable Constructor<?>[] chosenCtors, @Nullable Object[] explicitArgs) {

	BeanWrapperImpl bw = new BeanWrapperImpl();
	this.beanFactory.initBeanWrapper(bw);

	Constructor<?> constructorToUse = null;
	ArgumentsHolder argsHolderToUse = null;
	Object[] argsToUse = null;

	// 如果getBean()传入了args,那构造方法要用的入参就直接确定好了
	if (explicitArgs != null) {
		argsToUse = explicitArgs;
	}
	else {
		Object[] argsToResolve = null;
		synchronized (mbd.constructorArgumentLock) {
			constructorToUse = (Constructor<?>) mbd.resolvedConstructorOrFactoryMethod;
			if (constructorToUse != null && mbd.constructorArgumentsResolved) {
				// Found a cached constructor...
				argsToUse = mbd.resolvedConstructorArguments;
				if (argsToUse == null) {
					argsToResolve = mbd.preparedConstructorArguments;
				}
			}
		}
		if (argsToResolve != null) {
			argsToUse = resolvePreparedArguments(beanName, mbd, bw, constructorToUse, argsToResolve);
		}
	}

	// 如果没有确定要使用的构造方法,或者确定了构造方法但是所要传入的参数值没有确定
	if (constructorToUse == null || argsToUse == null) {

		// Take specified constructors, if any.
		// 如果没有指定构造方法,那就获取beanClass中的所有构造方法所谓候选者
		Constructor<?>[] candidates = chosenCtors;
		if (candidates == null) {
			Class<?> beanClass = mbd.getBeanClass();
			try {
				candidates = (mbd.isNonPublicAccessAllowed() ?
						beanClass.getDeclaredConstructors() : beanClass.getConstructors());
			}
			catch (Throwable ex) {
				throw new BeanCreationException(mbd.getResourceDescription(), beanName,
						"Resolution of declared constructors on bean Class [" + beanClass.getName() +
						"] from ClassLoader [" + beanClass.getClassLoader() + "] failed", ex);
			}
		}

		// 如果只有一个候选构造方法,并且没有指定所要使用的构造方法参数值,并且该构造方法是无参的,那就直接用这个无参构造方法进行实例化了
		if (candidates.length == 1 && explicitArgs == null && !mbd.hasConstructorArgumentValues()) {
			Constructor<?> uniqueCandidate = candidates[0];
			if (uniqueCandidate.getParameterCount() == 0) {
				synchronized (mbd.constructorArgumentLock) {
					mbd.resolvedConstructorOrFactoryMethod = uniqueCandidate;
					mbd.constructorArgumentsResolved = true;
					mbd.resolvedConstructorArguments = EMPTY_ARGS;
				}
				bw.setBeanInstance(instantiate(beanName, mbd, uniqueCandidate, EMPTY_ARGS));
				return bw;
			}
		}

		// Need to resolve the constructor.
		boolean autowiring = (chosenCtors != null ||
				mbd.getResolvedAutowireMode() == AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR);
		ConstructorArgumentValues resolvedValues = null;

		// 确定要选择的构造方法的参数个数的最小值,后续判断候选构造方法的参数个数如果小于minNrOfArgs,则直接pass掉
		int minNrOfArgs;
		if (explicitArgs != null) {
			// 如果直接传了构造方法参数值,那么所用的构造方法的参数个数肯定不能少于
			minNrOfArgs = explicitArgs.length;
		}
		else {
			// 如果通过BeanDefinition传了构造方法参数值,因为有可能是通过下标指定了,比如0位置的值,2位置的值,虽然只指定了2个值,但是构造方法的参数个数至少得是3个
			ConstructorArgumentValues cargs = mbd.getConstructorArgumentValues();
			resolvedValues = new ConstructorArgumentValues();
			// 处理RuntimeBeanReference
			minNrOfArgs = resolveConstructorArguments(beanName, mbd, bw, cargs, resolvedValues);
		}

		// 对候选构造方法进行排序,public的方法排在最前面,都是public的情况下参数个数越多越靠前
		AutowireUtils.sortConstructors(candidates);
		int minTypeDiffWeight = Integer.MAX_VALUE;
		Set<Constructor<?>> ambiguousConstructors = null;
		Deque<UnsatisfiedDependencyException> causes = null;

		// 遍历每个构造方法,进行筛选
		for (Constructor<?> candidate : candidates) {
			// 参数个数
			int parameterCount = candidate.getParameterCount();

			// 本次遍历时,之前已经选出来了所要用的构造方法和入参对象,并且入参对象个数比当前遍历到的这个构造方法的参数个数多,则不用再遍历,退出循环
			if (constructorToUse != null && argsToUse != null && argsToUse.length > parameterCount) {
				// Already found greedy constructor that can be satisfied ->
				// do not look any further, there are only less greedy constructors left.
				break;
			}
			// 如果参数个数小于所要求的参数个数,则遍历下一个,这里考虑的是同时存在public和非public的构造方法
			if (parameterCount < minNrOfArgs) {
				continue;
			}

			ArgumentsHolder argsHolder;
			Class<?>[] paramTypes = candidate.getParameterTypes();
			// 没有通过getBean()指定构造方法参数值
			if (resolvedValues != null) {
				try {
					// 如果在构造方法上使用了@ConstructorProperties,那么就直接取定义的值作为构造方法的参数名
					String[] paramNames = ConstructorPropertiesChecker.evaluate(candidate, parameterCount);

					// 获取构造方法参数名
					if (paramNames == null) {
						ParameterNameDiscoverer pnd = this.beanFactory.getParameterNameDiscoverer();
						if (pnd != null) {
							paramNames = pnd.getParameterNames(candidate);
						}
					}

					// 根据参数类型、参数名找到对应的bean对象
					argsHolder = createArgumentArray(beanName, mbd, resolvedValues, bw, paramTypes, paramNames,
							getUserDeclaredConstructor(candidate), autowiring, candidates.length == 1);
				}
				catch (UnsatisfiedDependencyException ex) {
					// 当前正在遍历的构造方法找不到可用的入参对象,记录一下
					if (logger.isTraceEnabled()) {
						logger.trace("Ignoring constructor [" + candidate + "] of bean '" + beanName + "': " + ex);
					}
					// Swallow and try next constructor.
					if (causes == null) {
						causes = new ArrayDeque<>(1);
					}
					causes.add(ex);
					continue;
				}
			}
			else {
				// Explicit arguments given -> arguments length must match exactly.
				// 在调getBean方法时传入了参数值,那就表示只能用对应参数个数的构造方法
				if (parameterCount != explicitArgs.length) {
					continue;
				}
				// 不用再去BeanFactory中查找bean对象了,已经有了,同时当前正在遍历的构造方法就是可用的构造方法
				argsHolder = new ArgumentsHolder(explicitArgs);
			}

			// 当前遍历的构造方法所需要的入参对象都找到了,根据参数类型和找到的参数对象计算出来一个匹配值,值越小越匹配
			// Lenient表示宽松模式
			int typeDiffWeight = (mbd.isLenientConstructorResolution() ?
					argsHolder.getTypeDifferenceWeight(paramTypes) : argsHolder.getAssignabilityWeight(paramTypes));
			// Choose this constructor if it represents the closest match.
			// 值越小越匹配
			if (typeDiffWeight < minTypeDiffWeight) {
				constructorToUse = candidate;
				argsHolderToUse = argsHolder;
				argsToUse = argsHolder.arguments;
				minTypeDiffWeight = typeDiffWeight;
				ambiguousConstructors = null;
			}
			// 值相等的情况下,记录一下匹配值相同的构造方法
			else if (constructorToUse != null && typeDiffWeight == minTypeDiffWeight) {
				if (ambiguousConstructors == null) {
					ambiguousConstructors = new LinkedHashSet<>();
					ambiguousConstructors.add(constructorToUse);
				}
				ambiguousConstructors.add(candidate);
			}
		}
		// 遍历结束   x

		// 如果没有可用的构造方法,就取记录的最后一个异常并抛出
		if (constructorToUse == null) {
			if (causes != null) {
				UnsatisfiedDependencyException ex = causes.removeLast();
				for (Exception cause : causes) {
					this.beanFactory.onSuppressedException(cause);
				}
				throw ex;
			}
			throw new BeanCreationException(mbd.getResourceDescription(), beanName,
					"Could not resolve matching constructor on bean class [" + mbd.getBeanClassName() + "] " +
					"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)");
		}
		// 如果有可用的构造方法,但是有多个
		else if (ambiguousConstructors != null && !mbd.isLenientConstructorResolution()) {
			throw new BeanCreationException(mbd.getResourceDescription(), beanName,
					"Ambiguous constructor matches found on bean class [" + mbd.getBeanClassName() + "] " +
					"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities): " +
					ambiguousConstructors);
		}

		// 如果没有通过getBean方法传入参数,并且找到了构造方法以及要用的入参对象则缓存
		if (explicitArgs == null && argsHolderToUse != null) {
			argsHolderToUse.storeCache(mbd, constructorToUse);
		}
	}

	Assert.state(argsToUse != null, "Unresolved constructor arguments");
	bw.setBeanInstance(instantiate(beanName, mbd, constructorToUse, argsToUse));
	return bw;
}

通过上述代码,我们可以看到:

  1. 首先判断,如果createBeanInstance()方法指定啦构造器参数;
  2. 再判断如果有构造方法缓存;
  3. 我们具体要分析的是,如果createBeanInstance()方法没有指定构造器参数,且 没有缓存构造方法,进入下面 if 代码块;
  4. 判断如果determineConstructorsFromBeanPostProcessors()方法有没有找到带@Autowired注解的构造方法,没有没有带@Autowired注解的方法,会获取所有的构造方法;
  5. 再往下判断,如果只有一个候选构造方法,并且没有指定所要使用的构造方法参数值,并且该构造方法是无参的,那就直接用这个无参构造方法进行实例化了;
  6. 再往下,查找构造方法参数的个数;
  7. 如果createBeanInstance()方法指定啦构造器参数,指定参数的个数就是要查找构造方法参数的个数;
  8. 如果createBeanInstance()方法没有指定构造器参数,尝试从BeanDefinition中获取构造器的参数的个数(怎么给BeanDefinitio设置构造器参数自行查找);
  9. 确定啦构造参数的个数后;
  10. 对候选构造器排序,public的方法排在最前面,都是public的情况下参数个数越多越靠前;
  11. 遍历排序好的构造器;
  12. 首先第一个判断是,如果遍历过程中找到啦要用的构造方法和入参对象,并且入参对象个数比当前遍历到的这个构造方法的参数个数多,则不用再遍历,退出循环;
  13. 如果参数个数小于所要求的参数个数,则遍历下一个,这里考虑的是同时存在public和非public的构造方法;
  14. 再往下如果在构造方法上使用了@ConstructorProperties,那么就直接取定义的值作为构造方法的参数名;
  15. 如果createBeanInstance()方法没有指定啦构造器参数;
  16. 如果没有@ConstructorProperties注解,直接获取构造方法的参数和参数类型;
  17. 调用createArgumentArray()方法根据参数类型、参数名找到对应的bean对象;
  18. 如果createBeanInstance()方法指定啦构造器参数,那就表示只能用对应参数个数的构造方法
  19. 当前遍历的构造方法所需要的入参对象都找到了,根据参数类型和找到的参数对象计算出来一个匹配值,值越小越匹配;
  20. 如果没有可用的构造方法,就取记录的最后一个异常并抛出;
  21. 如果有可用的构造方法,但是有多个,也会报错;
  22. 如果能找到构造方法就会调用instantiate()方法进行实例化;

返回createBeanInstance()方法

instantiateBean()方法

instantiateBean()方法详解

/**
 * Instantiate the given bean using its default constructor.
 *
 * @param beanName the name of the bean
 * @param mbd      the bean definition for the bean
 * @return a BeanWrapper for the new instance
 */
protected BeanWrapper instantiateBean(String beanName, RootBeanDefinition mbd) {
	try {
		Object beanInstance;
		if (System.getSecurityManager() != null) {
			beanInstance = AccessController.doPrivileged(
					(PrivilegedAction<Object>) () -> getInstantiationStrategy().instantiate(mbd, beanName, this),
					getAccessControlContext());
		} else {
			// 默认是CglibSubclassingInstantiationStrategy
			beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, this);
		}
		BeanWrapper bw = new BeanWrapperImpl(beanInstance);
		initBeanWrapper(bw);
		return bw;
	} catch (Throwable ex) {
		throw new BeanCreationException(
				mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);
	}
}

通过上述代码,我们可以看到,这里最有是调用InstantiationStrategy接口的instantiate()方法,SimpleInstantiationStrategy类实现啦InstantiationStrategy接口的instantiate()方法,我看下具体实现:

/**
 * 此方法有重载,此方法是用来调用无参构造方法来实例化的
 * <p>
 * 默认构造器创建对象
 */
@Override
public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {
	// Don't override the class with CGLIB if no overrides.
	// 判断当前BeanDefinition对应的beanClass中是否存在@Lookup的方法
	if (!bd.hasMethodOverrides()) {
		Constructor<?> constructorToUse;
		synchronized (bd.constructorArgumentLock) {
			constructorToUse = (Constructor<?>) bd.resolvedConstructorOrFactoryMethod;
			if (constructorToUse == null) {
				final Class<?> clazz = bd.getBeanClass();
				// 判断Class类型是否是接口或注解类型
				if (clazz.isInterface()) {
					throw new BeanInstantiationException(clazz, "Specified class is an interface");
				}
				try {
					if (System.getSecurityManager() != null) {
						constructorToUse = AccessController.doPrivileged(
								(PrivilegedExceptionAction<Constructor<?>>) clazz::getDeclaredConstructor);
					} else {
						// 获取显示的无参构造器
						constructorToUse = clazz.getDeclaredConstructor();
					}
					bd.resolvedConstructorOrFactoryMethod = constructorToUse;
				} catch (Throwable ex) {
					throw new BeanInstantiationException(clazz, "No default constructor found", ex);
				}
			}
		}
		// 构造器反射创建对象
		return BeanUtils.instantiateClass(constructorToUse);
	} else {
		// Must generate CGLIB subclass.
		// 如果存在@Lookup,则会生成一个代理对象
		return instantiateWithMethodInjection(bd, beanName, owner);
	}
}

通过上述代码我们可以看到:

  1. 首先判断是否有@Lookup注解对应的方法;如果有调用cglib生成代理对象;
  2. 如果没有@Lookup注解对应的方法,
  3. 判断如果是接口,报错;
  4. 调用getDeclaredConstructor()方法获取无法构造器,获取不到,报错;
  5. 最后通过无参构造器实例化对象;

返回createBeanInstance()方法

instantiateUsingFactoryMethod()方法

instantiateUsingFactoryMethod()方法详解

/**
 * Instantiate the bean using a named factory method. The method may be static, if the
 * mbd parameter specifies a class, rather than a factoryBean, or an instance variable
 * on a factory object itself configured using Dependency Injection.
 *
 * @param beanName     the name of the bean
 * @param mbd          the bean definition for the bean
 * @param explicitArgs argument values passed in programmatically via the getBean method,
 *                     or {@code null} if none (-> use constructor argument values from bean definition)
 * @return a BeanWrapper for the new instance
 * @see #getBean(String, Object[])
 */
protected BeanWrapper instantiateUsingFactoryMethod(
		String beanName, RootBeanDefinition mbd, @Nullable Object[] explicitArgs) {

	// 使用factoryBean来实例化对象
	return new ConstructorResolver(this).instantiateUsingFactoryMethod(beanName, mbd, explicitArgs);
}

这里我们可以看到,最终会调用ConstructorResolver类的instantiateUsingFactoryMethod()方法,我们详细分析一下ConstructorResolver类的instantiateUsingFactoryMethod()方法

/**
 * Instantiate the bean using a named factory method. The method may be static, if the
 * bean definition parameter specifies a class, rather than a "factory-bean", or
 * an instance variable on a factory object itself configured using Dependency Injection.
 * <p>Implementation requires iterating over the static or instance methods with the
 * name specified in the RootBeanDefinition (the method may be overloaded) and trying
 * to match with the parameters. We don't have the types attached to constructor args,
 * so trial and error is the only way to go here. The explicitArgs array may contain
 * argument values passed in programmatically via the corresponding getBean method.
 * @param beanName the name of the bean
 * @param mbd the merged bean definition for the bean
 * @param explicitArgs argument values passed in programmatically via the getBean
 * method, or {@code null} if none (-> use constructor argument values from bean definition)
 * @return a BeanWrapper for the new instance
 */
public BeanWrapper instantiateUsingFactoryMethod(
		String beanName, RootBeanDefinition mbd, @Nullable Object[] explicitArgs) {

	BeanWrapperImpl bw = new BeanWrapperImpl();
	this.beanFactory.initBeanWrapper(bw);

	Object factoryBean;
	Class<?> factoryClass;
	boolean isStatic;

	// 注意,这里拿到的是factoryBeanName,而不是factoryMethodName,比如AppConfig对象
	String factoryBeanName = mbd.getFactoryBeanName();
	if (factoryBeanName != null) {
		if (factoryBeanName.equals(beanName)) {
			throw new BeanDefinitionStoreException(mbd.getResourceDescription(), beanName,
					"factory-bean reference points back to the same bean definition");
		}
		factoryBean = this.beanFactory.getBean(factoryBeanName);

		// 该单例已经创建好了?
		if (mbd.isSingleton() && this.beanFactory.containsSingleton(beanName)) {
			throw new ImplicitlyAppearedSingletonException();
		}
		this.beanFactory.registerDependentBean(factoryBeanName, beanName);
		factoryClass = factoryBean.getClass();
		isStatic = false;
	}
	else {
		// It's a static factory method on the bean class.
		// static的@Bean方法
		if (!mbd.hasBeanClass()) {
			throw new BeanDefinitionStoreException(mbd.getResourceDescription(), beanName,
					"bean definition declares neither a bean class nor a factory-bean reference");
		}
		factoryBean = null;
		factoryClass = mbd.getBeanClass();
		isStatic = true;
	}

	Method factoryMethodToUse = null;
	ArgumentsHolder argsHolderToUse = null;
	Object[] argsToUse = null;

	if (explicitArgs != null) {
		argsToUse = explicitArgs;
	}
	else {
		Object[] argsToResolve = null;
		synchronized (mbd.constructorArgumentLock) {
			factoryMethodToUse = (Method) mbd.resolvedConstructorOrFactoryMethod;
			if (factoryMethodToUse != null && mbd.constructorArgumentsResolved) {
				// Found a cached factory method...
				argsToUse = mbd.resolvedConstructorArguments;
				if (argsToUse == null) {
					argsToResolve = mbd.preparedConstructorArguments;
				}
			}
		}
		if (argsToResolve != null) {
			argsToUse = resolvePreparedArguments(beanName, mbd, bw, factoryMethodToUse, argsToResolve);
		}
	}

	if (factoryMethodToUse == null || argsToUse == null) {
		// Need to determine the factory method...
		// Try all methods with this name to see if they match the given arguments.
		factoryClass = ClassUtils.getUserClass(factoryClass);

		List<Method> candidates = null;
		if (mbd.isFactoryMethodUnique) {
			if (factoryMethodToUse == null) {
				factoryMethodToUse = mbd.getResolvedFactoryMethod();
			}
			if (factoryMethodToUse != null) {
				candidates = Collections.singletonList(factoryMethodToUse);
			}
		}

		// 找到对应的@Bean方法,由于可能参数重载,所以有可能会有多个
		if (candidates == null) {
			candidates = new ArrayList<>();
			Method[] rawCandidates = getCandidateMethods(factoryClass, mbd);
			for (Method candidate : rawCandidates) {
				if (Modifier.isStatic(candidate.getModifiers()) == isStatic && mbd.isFactoryMethod(candidate)) {
					candidates.add(candidate);
				}
			}
		}

		if (candidates.size() == 1 && explicitArgs == null && !mbd.hasConstructorArgumentValues()) {
			Method uniqueCandidate = candidates.get(0);
			if (uniqueCandidate.getParameterCount() == 0) {
				mbd.factoryMethodToIntrospect = uniqueCandidate;
				synchronized (mbd.constructorArgumentLock) {
					mbd.resolvedConstructorOrFactoryMethod = uniqueCandidate;
					mbd.constructorArgumentsResolved = true;
					mbd.resolvedConstructorArguments = EMPTY_ARGS;
				}
				bw.setBeanInstance(instantiate(beanName, mbd, factoryBean, uniqueCandidate, EMPTY_ARGS));
				return bw;
			}
		}

		if (candidates.size() > 1) {  // explicitly skip immutable singletonList
			candidates.sort(AutowireUtils.EXECUTABLE_COMPARATOR);
		}

		ConstructorArgumentValues resolvedValues = null;
		boolean autowiring = (mbd.getResolvedAutowireMode() == AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR);
		int minTypeDiffWeight = Integer.MAX_VALUE;
		Set<Method> ambiguousFactoryMethods = null;

		int minNrOfArgs;
		if (explicitArgs != null) {
			minNrOfArgs = explicitArgs.length;
		}
		else {
			// We don't have arguments passed in programmatically, so we need to resolve the
			// arguments specified in the constructor arguments held in the bean definition.
			if (mbd.hasConstructorArgumentValues()) {
				ConstructorArgumentValues cargs = mbd.getConstructorArgumentValues();
				resolvedValues = new ConstructorArgumentValues();
				minNrOfArgs = resolveConstructorArguments(beanName, mbd, bw, cargs, resolvedValues);
			}
			else {
				minNrOfArgs = 0;
			}
		}

		Deque<UnsatisfiedDependencyException> causes = null;

		for (Method candidate : candidates) {
			int parameterCount = candidate.getParameterCount();

			if (parameterCount >= minNrOfArgs) {
				ArgumentsHolder argsHolder;

				Class<?>[] paramTypes = candidate.getParameterTypes();
				if (explicitArgs != null) {
					// Explicit arguments given -> arguments length must match exactly.
					if (paramTypes.length != explicitArgs.length) {
						continue;
					}
					argsHolder = new ArgumentsHolder(explicitArgs);
				}
				else {
					// Resolved constructor arguments: type conversion and/or autowiring necessary.
					try {

						// 根据参数类型和参数名找Bean
						String[] paramNames = null;
						ParameterNameDiscoverer pnd = this.beanFactory.getParameterNameDiscoverer();
						if (pnd != null) {
							paramNames = pnd.getParameterNames(candidate);
						}
						argsHolder = createArgumentArray(beanName, mbd, resolvedValues, bw,
								paramTypes, paramNames, candidate, autowiring, candidates.size() == 1);
					}
					catch (UnsatisfiedDependencyException ex) {
						if (logger.isTraceEnabled()) {
							logger.trace("Ignoring factory method [" + candidate + "] of bean '" + beanName + "': " + ex);
						}
						// Swallow and try next overloaded factory method.
						if (causes == null) {
							causes = new ArrayDeque<>(1);
						}
						causes.add(ex);
						continue;
					}
				}

				int typeDiffWeight = (mbd.isLenientConstructorResolution() ?
						argsHolder.getTypeDifferenceWeight(paramTypes) : argsHolder.getAssignabilityWeight(paramTypes));
				// Choose this factory method if it represents the closest match.
				if (typeDiffWeight < minTypeDiffWeight) {
					factoryMethodToUse = candidate;
					argsHolderToUse = argsHolder;
					argsToUse = argsHolder.arguments;
					minTypeDiffWeight = typeDiffWeight;
					ambiguousFactoryMethods = null;
				}
				// Find out about ambiguity: In case of the same type difference weight
				// for methods with the same number of parameters, collect such candidates
				// and eventually raise an ambiguity exception.
				// However, only perform that check in non-lenient constructor resolution mode,
				// and explicitly ignore overridden methods (with the same parameter signature).
				else if (factoryMethodToUse != null && typeDiffWeight == minTypeDiffWeight &&
						!mbd.isLenientConstructorResolution() &&
						paramTypes.length == factoryMethodToUse.getParameterCount() &&
						!Arrays.equals(paramTypes, factoryMethodToUse.getParameterTypes())) {
					if (ambiguousFactoryMethods == null) {
						ambiguousFactoryMethods = new LinkedHashSet<>();
						ambiguousFactoryMethods.add(factoryMethodToUse);
					}
					ambiguousFactoryMethods.add(candidate);
				}
			}
		}

		if (factoryMethodToUse == null || argsToUse == null) {
			if (causes != null) {
				UnsatisfiedDependencyException ex = causes.removeLast();
				for (Exception cause : causes) {
					this.beanFactory.onSuppressedException(cause);
				}
				throw ex;
			}
			List<String> argTypes = new ArrayList<>(minNrOfArgs);
			if (explicitArgs != null) {
				for (Object arg : explicitArgs) {
					argTypes.add(arg != null ? arg.getClass().getSimpleName() : "null");
				}
			}
			else if (resolvedValues != null) {
				Set<ValueHolder> valueHolders = new LinkedHashSet<>(resolvedValues.getArgumentCount());
				valueHolders.addAll(resolvedValues.getIndexedArgumentValues().values());
				valueHolders.addAll(resolvedValues.getGenericArgumentValues());
				for (ValueHolder value : valueHolders) {
					String argType = (value.getType() != null ? ClassUtils.getShortName(value.getType()) :
							(value.getValue() != null ? value.getValue().getClass().getSimpleName() : "null"));
					argTypes.add(argType);
				}
			}
			String argDesc = StringUtils.collectionToCommaDelimitedString(argTypes);
			throw new BeanCreationException(mbd.getResourceDescription(), beanName,
					"No matching factory method found on class [" + factoryClass.getName() + "]: " +
					(mbd.getFactoryBeanName() != null ?
						"factory bean '" + mbd.getFactoryBeanName() + "'; " : "") +
					"factory method '" + mbd.getFactoryMethodName() + "(" + argDesc + ")'. " +
					"Check that a method with the specified name " +
					(minNrOfArgs > 0 ? "and arguments " : "") +
					"exists and that it is " +
					(isStatic ? "static" : "non-static") + ".");
		}
		else if (void.class == factoryMethodToUse.getReturnType()) {
			throw new BeanCreationException(mbd.getResourceDescription(), beanName,
					"Invalid factory method '" + mbd.getFactoryMethodName() + "' on class [" +
					factoryClass.getName() + "]: needs to have a non-void return type!");
		}
		else if (ambiguousFactoryMethods != null) {
			throw new BeanCreationException(mbd.getResourceDescription(), beanName,
					"Ambiguous factory method matches found on class [" + factoryClass.getName() + "] " +
					"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities): " +
					ambiguousFactoryMethods);
		}

		if (explicitArgs == null && argsHolderToUse != null) {
			mbd.factoryMethodToIntrospect = factoryMethodToUse;
			argsHolderToUse.storeCache(mbd, factoryMethodToUse);
		}
	}

	bw.setBeanInstance(instantiate(beanName, mbd, factoryBean, factoryMethodToUse, argsToUse));
	return bw;
}

待补充!!!
返回createBeanInstance()方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值