【spring】推断构造方法

推断构造方法简单介绍

  • 本文源码基于spring-framework-5.3.10。
  • 本文从源码层面解释spring如何选择构造方法。

推断构造方法的核心思路

  • 一个构造方法的时候,使用这一个构造方法,默认无参或者自定义的有参。
  • 程序员指定了构造方法入参值,通过getBean或者BeanDefinition去传入参数,使用匹配参数的的构造。
  • 完全自动选择构造:autowire=“constructor”
  • 被@Autowired标记的构造方法优先使用。

推断构造方法的源码思路

  • AbstractAutowireCapableBeanFactory类中的createBeanInstance()方法会去创建一个Bean实例
  • 根据BeanDefinition加载类得到Class对象。
  • 如果BeanDefinition绑定了一个Supplier,那就调用Supplier的get方法得到一个对象并直接返回。
  • 如果BeanDefinition中存在factoryMethodName,那么就调用该工厂方法得到一个bean对象并返回。
  • 如果BeanDefinition已经自动构造过了,那就调用autowireConstructor()自动构造一个对象。
  • 调用SmartInstantiationAwareBeanPostProcessor的determineCandidateConstructors()方法得到哪些构造方法是可以用的。
  • 如果存在可用得构造方法,或者当前BeanDefinition的autowired是AUTOWIRE_CONSTRUCTOR,或者- BeanDefinition中指定了构造方法参数值,或者创建Bean的时候指定了构造方法参数值,那么就调用autowireConstructor()方法自动构造一个对象。
  • 最后,如果不是上述情况,就根据无参的构造方法实例化一个对象。

推断构造方法的autowireConstructor()源码思路

  • 先检查是否指定了具体的构造方法和构造方法参数值,或者在BeanDefinition中缓存了具体的构造方法或构造方法参数值,如果存在那么则直接使用该构造方法进行实例化。
  • 如果没有确定的构造方法或构造方法参数值,进行下面的逻辑。
  • 如果没有确定的构造方法,那么则找出类中所有的构造方法。
  • 如果只有一个无参的构造方法,那么直接使用无参的构造方法进行实例化。
  • 如果有多个可用的构造方法或者当前Bean需要自动通过构造方法注入。
  • 根据所指定的构造方法参数值,确定所需要的最少的构造方法参数值的个数。
  • 对所有的构造方法进行排序,参数个数多的在前面。
  • 遍历每个构造方法。
  • 如果不是调用getBean方法时所指定的构造方法参数值,那么则根据构造方法参数类型找值。
  • 如果时调用getBean方法时所指定的构造方法参数值,就直接利用这些值。
  • 如果根据当前构造方法找到了对应的构造方法参数值,那么这个构造方法就是可用的,但是不一定这个构造方法就是最佳的,所以这里会涉及到是否有多个构造方法匹配了同样的值,这个时候就会用值和构造方法类型进行匹配程度的打分,找到一个最匹配的。

推断构造方法思路总结

  • 没有加@Autowired注解,有多个构造方法,返回null。使用无参构造!
  • 没有加@Autowired注解,只有一个有参构造,返回这个构造方法。使用这个构造方法!
  • 没有加@Autowired注解,只有一个无参构造,返回null。使用无参构造!
  • 加了@Autowired注解,只有一个required为true的构造方法,返回这个构造方法。使用这个构造方法!
  • 加了@Autowired注解,有多个required为true的构造方法,抛异常!
  • 加了@Autowired注解,有一个required为true和其他的required为false的构造方法,抛异常!
  • 加了@Autowired注解,没有required为true的构造方法,返回所有required为false的构造方法以及无参构造方法!优先使用参数个数比较长的,公共的构造方法。

Supplier是什么?

  • 存在的时候会调用Supplier重写的get方法去作为构造方法。
  • spring推断构造提供的一种机制。
beanDefinition.setInstanceSupplier(new Supplier<Object>() {
	@Override
	public Object get() {
		new Object();
	}
});

推断构造方法源码分析

/**
 * beanName:当前Bean的名称
 * mbd:当前Bean对应的合并后的BeanDefinition
 * args:调用getBean传入的参数,或者可以理解为构造方法需要的参数
 */
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
	// Make sure bean class is actually resolved at this point.
	// 保证Bean加载
	Class<?> beanClass = resolveBeanClass(mbd, beanName);
	// beanClass != null && 当前类不是public && 不允许访问非公共构造函数和方法。抛出异常
	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重写的get方法来得到对象
	Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
	if (instanceSupplier != null) {
		return obtainFromSupplier(instanceSupplier, beanName);
	}

	// @Bean对应的BeanDefinition,其实与FactoryBean的加载很类似
	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;
	// 如果你getBean的时候获取到参数,就不能走缓存!因为你传入的值大部分情况都是不一样的。
	if (args == null) {
		synchronized (mbd.constructorArgumentLock) {
			// 缓存当前的BeanDefinition使用的是那个构造方法,method对象
			if (mbd.resolvedConstructorOrFactoryMethod != null) {
				// 标志可以用缓存
				resolved = true;
				// autowireNecessary表示有没有必要要进行注入,比如当前BeanDefinition用的是无参构造方法,那么autowireNecessary为false,否则为true,表示需要给构造方法参数注入值
				// 简单说,无参的时候为false,第一次的时候为false
				autowireNecessary = mbd.constructorArgumentsResolved;
			}
		}
	}

	// 存在缓存,或者说缓存可以用
	if (resolved) {
		// 如果确定了当前BeanDefinition的构造方法,那么看是否需要进行对构造方法进行参数的依赖注入(构造方法注入)
		// autowireNecessary为true说明有参数,并且有缓存
		if (autowireNecessary) {
			// 方法内会拿到缓存好的构造方法的入参
			return autowireConstructor(beanName, mbd, null, null);
		}
		else {
			// 构造方法已经找到了,但是没有参数,那就表示是无参,直接进行实例化
			return instantiateBean(beanName, mbd);
		}
	}

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

	// 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);
}

无参构造方法调用:instantiateBean源码分析

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对象
		BeanWrapper bw = new BeanWrapperImpl(beanInstance);
		// 一系列的初始化操作
		initBeanWrapper(bw);
		// 返回这个BeanWrapper
		return bw;
	}
	catch (Throwable ex) {
		throw new BeanCreationException(
				mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);
	}
}

有参构造方法调用:autowireConstructor源码分析

/**
 * beanName:当前Bean的名称
 * mbd:当前Bean的BeanDefinition
 * chosenCtors:当前Bean可以进行注入的构造方法
 * explicitArgs:getBean的传参!
 */
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.
				// 没有通过BeanDefinition指定构造方法参数值,但是在调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;
}

实例化Bean的源码分析

/**
 * 默认的实例化
 */
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) {
				// 得到当前Bean
				final Class<?> clazz = bd.getBeanClass();
				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);
	}
}

寻找@Autowired注解的构造方法

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 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 {
								// 获取合并后的BeanDefinition,准备放入methodOverrides对象
								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()) {
						// 记录一下普通的构造方法,这个变量与kotlin有关
						nonSyntheticConstructors++;
					}
					else if (primaryConstructor != null) {
						continue;
					}

					// 当前遍历的构造方法是否写了@Autowired
					MergedAnnotation<?> ann = findAutowiredAnnotation(candidate);
					if (ann == null) {
						// 如果beanClass是代理类,则得到被代理的类的类型。看有没有cglib产生的代理类的标志。
						Class<?> userClass = ClassUtils.getUserClass(beanClass);
						if (userClass != beanClass) {
							try {
								// 得到被代理的类的构造方法
								Constructor<?> superCtor =
										userClass.getDeclaredConstructor(candidate.getParameterTypes());
								// 看呗代理类上有没有构造方法
								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);
						}

						// 得到当前@Autowired为的required的值
						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);
}

结束语

  • 获取更多本文的前置知识文章,以及新的有价值的文章,让我们一起成为架构师!
  • 关注公众号,可以让你对MySQL、并发编程、spring源码有深入的了解!
  • 关注公众号,后续持续高效的学习JVM!
  • 这个公众号,无广告!!!每日更新!!!
    作者公众号.jpg
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值