Анализ исходного кода Spring5 третий

Spring

Анализ исходного кода Spring5 (создание экземпляра bean-компонента)

Это было представлено в предыдущем блоге: Spring сохранил все bean-компоненты в объекте карты beanDefinitionMap во время процесса сканирования bean-компонентов, но эти bean-компоненты по-прежнему являются beanDefinition и не были созданы экземпляры, поэтому на этот раз я сосредоточусь на анализе bean-компонентов в процессе создания экземпляров. . Анализ исходного кода процесса сканирования бина:blog.CSDN.net/QQ_24101357…

  • finishBeanFactoryInitialization(beanFactory);
  • 在这里插入图片描述
  • 在这里插入图片描述
  • 在这里插入图片描述
  • Слить родительский класс, RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName); Например, это не очень важно:
<bean id="parentBd" class="com.llsydn.merge.PcBd">
	<property name="name" value="parent"></property>
</bean>

<bean id="childBd" parent="parentBd">
	<property name="name" value="children"></property>
</bean>

public class PcBd {
	private String name;
	public void setName(String name) {
		this.name = name;
	}
	public void test() {
		System.out.println(this.name);
	}
}

public class test{
	public static void main(String[] args) {
		AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
		ac.register(Appconfig.class);
		ac.refresh();

		PcBd parentBd = (PcBd) ac.getBean("parentBd");
		parentBd.test();

		PcBd childBd = (PcBd) ac.getBean("childBd");
		childBd.test();
	}
}
// 输出:
parent
children

Приведенный выше код в основном предназначен для реализации процесса подготовки создания экземпляра bean-компонента и получения всех отсканированных beanNames из beanDefinitionNames. Затем просмотрите beanDefinitionNames, вызовите метод getBean() один за другим и создайте экземпляры этих объектов bean.

  • getBean(beanName)
  • 在这里插入图片描述
  • isSingletonCurrentlyInCreation Считает, что пока нет необходимости создавать объект.

Модель весеннего авторазвода не имеет себе равных по технологии авторазводки.

  1. нет будет использовать технологию bytype
  2. Модель автосвязывания по умолчанию для bean-компонентов == нет
public class OrderService{
	IndexService indexService;
}
这个indexService,直接忽略了装配

public class OrderService{
	@Autowired
	IndexService indexService;
}
这个indexService会先使用bytype技术自动装配,找不到再使用byname自动装配
  • Затем перейдите к doGetBean() для анализа
protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
			@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
	/**
	 * 通过 name 获取 beanName。这里不使用 name 直接作为 beanName 有两个原因
	 * 1、name 可能会以 & 字符开头,表明调用者想获取 FactoryBean 本身,而非 FactoryBean
	 *   实现类所创建的 bean。在 BeanFactory 中,FactoryBean 的实现类和其他的 bean 存储
	 *   方式是一致的,即 <beanName, bean>,beanName 中是没有 & 这个字符的。所以我们需要
	 *   将 name 的首字符 & 移除,这样才能从缓存里取到 FactoryBean 实例。
	 * 2、还是别名的问题,转换需要
	 */
	final String beanName = transformedBeanName(name);
	Object bean;

	/**
	 * 这个方法在初始化的时候会调用,在getBean的时候也会调用
	 * 为什么需要这么做呢?
	 * 也就是说spring在初始化的时候先获取这个对象
	 * 判断这个对象是否被实例化好了(普通情况下绝对为空====有一种情况可能不为空lazy=true,第二次调用)
	 * 从spring的bean容器中获取一个bean,由于spring中bean容器是一个map(singletonObjects)
	 * 所以你可以理解getSingleton(beanName)等于beanMap.get(beanName)
	 * 由于方法会在spring环境初始化的时候(就是对象被创建的时候调用一次)调用一次
	 * 还会在getBean的时候调用一次
	 * 所以再调试的时候需要特别注意,不能直接断点在这里,
	 * 需要先进入到annotationConfigApplicationContext.getBean(IndexDao.class)
	 * 之后再来断点,这样就确保了我们是在获取这个bean的时候调用的
	 *
	 * 需要说明的是在初始化时候调用一般都是返回null
	 *
	 * 第一次调用:isSingletonCurrentlyInCreation以为还没到开始创建对象的时候
	 */
	Object sharedInstance = getSingleton(beanName);
	if (sharedInstance != null && args == null) {
		// 这里的代码是对于日志的记录,方便我们以后阅读应该注释,不影响spring功能
		 if (logger.isDebugEnabled()) {
		 	if (isSingletonCurrentlyInCreation(beanName)) {
		 		logger.debug("Returning eagerly cached instance of singleton bean '" + beanName +
		 				"' that is not fully initialized yet - a consequence of a circular reference");
		 	}
		 	else {
		 		logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
		 	}
		 }

		/**
		 * 如果 sharedInstance 是普通的单例 bean,下面的方法会直接返回。但如果
		 * sharedInstance 是 FactoryBean 类型的,则需调用 getObject 工厂方法获取真正的
		 * bean 实例。如果用户想获取 FactoryBean 本身,这里也不会做特别的处理,直接返回
		 * 即可。毕竟 FactoryBean 的实现类本身也是一种 bean,只不过具有一点特殊的功能而已。
		 */
		bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
	}

	else {
		// Fail if we're already creating this bean instance:
		// We're assumably within a circular reference.
		/**
		 * 原型,判断当前正在创建的bean是不是原型。
		 * 如果是原型不应该在初始化的时候创建
		 */
		if (isPrototypeCurrentlyInCreation(beanName)) {
			throw new BeanCurrentlyInCreationException(beanName);
		}

		// Check if bean definition exists in this factory. 父工厂
		BeanFactory parentBeanFactory = getParentBeanFactory();
		if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
			// Not found -> check parent.
			String nameToLookup = originalBeanName(name);
			if (parentBeanFactory instanceof AbstractBeanFactory) {
				return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
						nameToLookup, requiredType, args, typeCheckOnly);
			}
			else if (args != null) {
				// Delegation to parent with explicit args.
				return (T) parentBeanFactory.getBean(nameToLookup, args);
			}
			else {
				// No args -> delegate to standard getBean method.
				return parentBeanFactory.getBean(nameToLookup, requiredType);
			}
		}

		if (!typeCheckOnly) {
			//添加到alreadyCreated set集合当中,表示他已经创建过一场
			markBeanAsCreated(beanName);
		}

		try {
			final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
			checkMergedBeanDefinition(mbd, beanName, args);

			// Guarantee initialization of beans that the current bean depends on.
			String[] dependsOn = mbd.getDependsOn();
			if (dependsOn != null) {
				for (String dep : dependsOn) {
					if (isDependent(beanName, dep)) {
						throw new BeanCreationException(mbd.getResourceDescription(), beanName,
								"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
					}
					registerDependentBean(dep, beanName);
					try {
						getBean(dep);
					}
					catch (NoSuchBeanDefinitionException ex) {
						throw new BeanCreationException(mbd.getResourceDescription(), beanName,
								"'" + beanName + "' depends on missing bean '" + dep + "'", ex);
					}
				}
			}

			// 创建单例的bean
			if (mbd.isSingleton()) {
				sharedInstance = getSingleton(beanName, () -> {
					try {
						return createBean(beanName, mbd, args);
					}
					catch (BeansException ex) {
						// Explicitly remove instance from singleton cache: It might have been put there
						// eagerly by the creation process, to allow for circular reference resolution.
						// Also remove any beans that received a temporary reference to the bean.
						destroySingleton(beanName);
						throw ex;
					}
				});
				bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
			}

			else if (mbd.isPrototype()) {
				// It's a prototype -> create a new instance.
				Object prototypeInstance = null;
				try {
					beforePrototypeCreation(beanName);
					prototypeInstance = createBean(beanName, mbd, args);
				}
				finally {
					afterPrototypeCreation(beanName);
				}
				bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
			}

			else {
				String scopeName = mbd.getScope();
				final Scope scope = this.scopes.get(scopeName);
				if (scope == null) {
					throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
				}
				try {
					Object scopedInstance = scope.get(beanName, () -> {
						beforePrototypeCreation(beanName);
						try {
							return createBean(beanName, mbd, args);
						}
						finally {
							afterPrototypeCreation(beanName);
						}
					});
					bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
				}
				catch (IllegalStateException ex) {
					throw new BeanCreationException(beanName,
							"Scope '" + scopeName + "' is not active for the current thread; consider " +
							"defining a scoped proxy for this bean if you intend to refer to it from a singleton",
							ex);
				}
			}
		}
		catch (BeansException ex) {
			cleanupAfterBeanCreationFailure(beanName);
			throw ex;
		}
	}

	// Check if required type matches the type of the actual bean instance.
	if (requiredType != null && !requiredType.isInstance(bean)) {
		try {
			T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType);
			if (convertedBean == null) {
				throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
			}
			return convertedBean;
		}
		catch (TypeMismatchException ex) {
			if (logger.isDebugEnabled()) {
				logger.debug("Failed to convert bean '" + name + "' to required type '" +
						ClassUtils.getQualifiedName(requiredType) + "'", ex);
			}
			throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
		}
	}
	return (T) bean;
}
  • createBean(beanName, mbd, args); Анализ
@Override
protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
		throws BeanCreationException {

	if (logger.isDebugEnabled()) {
		logger.debug("Creating instance of bean '" + beanName + "'");
	}
	RootBeanDefinition mbdToUse = mbd;

	// Make sure bean class is actually resolved at this point, and
	// clone the bean definition in case of a dynamically resolved Class
	// which cannot be stored in the shared merged bean definition.
	Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
	if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
		mbdToUse = new RootBeanDefinition(mbd);
		mbdToUse.setBeanClass(resolvedClass);
	}

	// Prepare method overrides.
	// 处理 lookup-method 和 replace-method 配置,Spring 将这两个配置统称为 override method
	try {
		mbdToUse.prepareMethodOverrides();
	}
	catch (BeanDefinitionValidationException ex) {
		throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
				beanName, "Validation of method overrides failed", ex);
	}

	try {
		// 在 bean 初始化前应用后置处理,如果后置处理返回的 bean 不为空,则直接返回
		// 这个类需要通过代码演示
		// 这个主要的作用是,将bean的所有依赖去到,直接返回一个寡对象。实现InstantiationAwareBeanPostProcessor接口。
		// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
		Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
		if (bean != null) {
			return bean;
		}
	}
	catch (Throwable ex) {
		throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
				"BeanPostProcessor before instantiation of bean failed", ex);
	}
	try {
		// 调用doCreateBean 创建bean
		Object beanInstance = doCreateBean(beanName, mbdToUse, args);
		if (logger.isDebugEnabled()) {
			logger.debug("Finished creating instance of bean '" + beanName + "'");
		}
		return beanInstance;
	}
	catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {
		// A previously detected exception with proper bean creation context already,
		// or illegal singleton state to be communicated up to DefaultSingletonBeanRegistry.
		throw ex;
	}
	catch (Throwable ex) {
		throw new BeanCreationException(
				mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex);
	}
}
  • Object beanInstance = doCreateBean(beanName, mbdToUse, args); Анализ
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
			throws BeanCreationException {
	// Instantiate the bean. 包装类
	BeanWrapper instanceWrapper = null;
	if (mbd.isSingleton()) {
		instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
	}
	if (instanceWrapper == null) {
		/**
		 * 创建 bean 实例,并将实例包裹在 BeanWrapper 实现类对象中返回。
		 * createBeanInstance中包含三种创建 bean 实例的方式:
		 *   1. 通过工厂方法创建 bean 实例
		 *   2. 通过构造方法自动注入(autowire by constructor)的方式创建 bean 实例
		 *   3. 通过无参构造方法方法创建 bean 实例
		 *
		 * 若 bean 的配置信息中配置了 lookup-method 和 replace-method,则会使用 CGLIB
		 * 增强 bean 实例。关于lookup-method和replace-method后面再说。
		 */
		instanceWrapper = createBeanInstance(beanName, mbd, args);
	}
	//拿到包装类的原生对象
	final Object bean = instanceWrapper.getWrappedInstance();
	Class<?> beanType = instanceWrapper.getWrappedClass();
	if (beanType != NullBean.class) {
		mbd.resolvedTargetType = beanType;
	}

	// Allow post-processors to modify the merged bean definition.
	synchronized (mbd.postProcessingLock) {
		if (!mbd.postProcessed) {
			try {
				//非常重要
				applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
			}
			catch (Throwable ex) {
				throw new BeanCreationException(mbd.getResourceDescription(), beanName,
						"Post-processing of merged bean definition failed", ex);
			}
			mbd.postProcessed = true;
		}
	}

	// Eagerly cache singletons to be able to resolve circular references
	// even when triggered by lifecycle interfaces like BeanFactoryAware.
	boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
			isSingletonCurrentlyInCreation(beanName));
	if (earlySingletonExposure) {
		if (logger.isDebugEnabled()) {
			logger.debug("Eagerly caching bean '" + beanName +
					"' to allow for resolving potential circular references");
		}
		addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
	}

	// Initialize the bean instance.
	Object exposedObject = bean;
	try {
		//设置属性,非常重要
		populateBean(beanName, mbd, instanceWrapper);
		//执行后置处理器,aop就是在这里完成的处理
		exposedObject = initializeBean(beanName, exposedObject, mbd);
	}
	catch (Throwable ex) {
		if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {
			throw (BeanCreationException) ex;
		}
		else {
			throw new BeanCreationException(
					mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
		}
	}

	if (earlySingletonExposure) {
		Object earlySingletonReference = getSingleton(beanName, false);
		if (earlySingletonReference != null) {
			if (exposedObject == bean) {
				exposedObject = earlySingletonReference;
			}
			else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
				String[] dependentBeans = getDependentBeans(beanName);
				Set<String> actualDependentBeans = new LinkedHashSet<>(dependentBeans.length);
				for (String dependentBean : dependentBeans) {
					if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {
						actualDependentBeans.add(dependentBean);
					}
				}
				if (!actualDependentBeans.isEmpty()) {
					throw new BeanCurrentlyInCreationException(beanName,
							"Bean with name '" + beanName + "' has been injected into other beans [" +
							StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +
							"] in its raw version as part of a circular reference, but has eventually been " +
							"wrapped. This means that said other beans do not use the final version of the " +
							"bean. This is often the result of over-eager type matching - consider using " +
							"'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.");
				}
			}
		}
	}
	// Register bean as disposable.
	try {
		registerDisposableBeanIfNecessary(beanName, bean, mbd);
	}
	catch (BeanDefinitionValidationException ex) {
		throw new BeanCreationException(
				mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
	}
	return exposedObject;
}

Основная функция метода doCreateBean(): 1. Сначала используйте технологию отражения для создания объекта-экземпляра компонента. createBeanInstance (beanName, mbd, args) 2. Автоматически собирать свойства экземпляра объекта. populateBean(beanName, mbd, instanceWrapper); 3. Запустите постпроцессор для улучшения бина. инициализироватьBean (имя_компонента, выставленный объект, mbd);

  • createBeanInstance(beanName, mbd, args) создает экземпляры объектов с использованием отражения
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);

	/**
	 * 检测一个类的访问权限spring默认情况下对于非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());
	}

	Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
	if (instanceSupplier != null) {
		return obtainFromSupplier(instanceSupplier, beanName);
	}

	/**
	 *
	 * 如果工厂方法不为空,则通过工厂方法构建 bean 对象
	 * 这种构建 bean 的方式可以自己写个demo去试试
	 * 源码就不做深入分析了,有兴趣的同学可以和我私下讨论
	 * 如果设置了factoryMethod,就使用FactoryMethod方法实例化对象。(举例子实现)
	 */
	if (mbd.getFactoryMethodName() != null)  {
		return instantiateUsingFactoryMethod(beanName, mbd, args);
	}

	// Shortcut when re-creating the same bean...
	/**
	 * 从spring的原始注释可以知道这个是一个Shortcut,什么意思呢?快捷方式
	 * 当多次构建同一个 bean 时,可以使用这个Shortcut,
	 * 也就是说不在需要次推断应该使用哪种方式构造bean
	 * 比如在多次构建同一个prototype类型的 bean 时,就可以走此处的shortcut
	 * 这里的 resolved 和 mbd.constructorArgumentsResolved 将会在 bean 第一次实例化的过程中被设置
	 */
	boolean resolved = false;
	boolean autowireNecessary = false;  //是否必须要自动装配
	if (args == null) {
		synchronized (mbd.constructorArgumentLock) {
			if (mbd.resolvedConstructorOrFactoryMethod != null) {
				resolved = true;
				//如果已经解析了构造方法的参数,则必须要通过一个带参构造方法来实例
				autowireNecessary = mbd.constructorArgumentsResolved;
			}
		}
	}
	if (resolved) {
		if (autowireNecessary) {
			//通过构造方法自动装配的方式构造 bean 对象
			return autowireConstructor(beanName, mbd, null, null);
		}
		else {
			//通过默认的无参构造方法进行
			return instantiateBean(beanName, mbd);
		}
	}

	//1.当有无参构造方法,就默认使用该无参构造方法,实例化对象。
		//因为spring不知道,你是使用有参构造方法实例化对象,还是使用无参构造方法实例化对象,所以就简单一点,使用无参构造方法实例化对象。
	//2.当且仅当只有一个有参构造方法,就使用该有参构造方法,实例化对象。
		//当你有多个有参构造方法的时候,spring也是不知道该用哪个构造方法实例化对象,所以就简单一点,使用无参构造方法实例化对象。
	//由后置处理器决定返回哪些构造方法
	//拿到bean的构造方法,然后使用该构造方法实例化对象。(要用哪个构造方法实例化对象,交给BeanPostProcessors处理)
	Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
	if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||
			mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args))  {
		return autowireConstructor(beanName, mbd, ctors, args);
	}
	//使用默认的无参构造方法进行初始化
	return instantiateBean(beanName, mbd);
}

Основные шаги createBeanInstance: 1. Определите, доступен ли компонент. 2. Определите, есть ли у компонента фабричный метод, и если да, вызовите фабричный метод для создания экземпляра объекта. 3. Определите, создается ли экземпляр компонента во второй раз и существует ли объект создания ярлыка. 4. Определяем конструктор бина:

1. Когда bean-компонент имеет конструктор без параметров, конструктор без параметров используется по умолчанию для создания экземпляра объекта. 2. Если компонент имеет только один параметризованный конструктор, используйте параметризованный конструктор для создания экземпляра объекта. 3. Если компонент имеет несколько конструкторов с параметрами, используйте конструктор без параметров для создания экземпляра объекта. (весна не может определить, какой конструктор использовать)

Примечания: Spring использует метод конструктора для создания экземпляров объектов.

  • определить ConstructorsFromBeanPostProcessors(), какой конструктор использовать для создания экземпляра объекта и передачи его BeanPostProcessors для обработки
protected Constructor<?>[] determineConstructorsFromBeanPostProcessors(@Nullable Class<?> beanClass, String beanName)
			throws BeansException {
	if (beanClass != null && hasInstantiationAwareBeanPostProcessors()) {
		for (BeanPostProcessor bp : getBeanPostProcessors()) {
			if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {
				SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp;
				Constructor<?>[] ctors = ibp.determineCandidateConstructors(beanClass, beanName);
				if (ctors != null) {
					return ctors;
				}
			}
		}
	}
	return null;
}

#AutowiredAnnotationBeanPostProcessor
//决定使用哪个候选的构造方法
@Override
@Nullable
public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, final String beanName)
		throws BeanCreationException {

	// Let's check for lookup methods here..
	if (!this.lookupMethodsChecked.contains(beanName)) {
		try {
			ReflectionUtils.doWithMethods(beanClass, method -> {
				Lookup lookup = method.getAnnotation(Lookup.class);
				if (lookup != null) {
					Assert.state(this.beanFactory != null, "No BeanFactory available");
					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");
					}
				}
			});
		}
		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) {
				// 拿到bean的所有构造方法
				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);
				Constructor<?> requiredConstructor = null;
				Constructor<?> defaultConstructor = null;
				Constructor<?> primaryConstructor = BeanUtils.findPrimaryConstructor(beanClass);
				int nonSyntheticConstructors = 0;
				for (Constructor<?> candidate : rawCandidates) {
					if (!candidate.isSynthetic()) {
						//记录bean有多少个构造方法
						nonSyntheticConstructors++;
					}
					else if (primaryConstructor != null) {
						continue;
					}
					AnnotationAttributes ann = findAutowiredAnnotation(candidate);
					if (ann == null) {
						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...
							}
						}
					}
					if (ann != null) {
						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);
							}
							requiredConstructor = candidate;
						}
						candidates.add(candidate);
					}
					//如果构造方法的没有参数,就是默认的构造方法。就使用该默认构造方法实例化对象。
					else if (candidate.getParameterCount() == 0) {
						defaultConstructor = candidate;
					}
				}
				if (!candidates.isEmpty()) {
					// Add default constructor to list of optional constructors, as fallback.
					if (requiredConstructor == null) {
						if (defaultConstructor != null) {
							candidates.add(defaultConstructor);
						}
						else if (candidates.size() == 1 && logger.isWarnEnabled()) {
							logger.warn("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));
						}
					}
					candidateConstructors = candidates.toArray(new Constructor<?>[0]);
				}
				else if (rawCandidates.length == 1 && rawCandidates[0].getParameterCount() > 0) {
					//只有一个有参构造方法,返回该有参构造方法
					candidateConstructors = new Constructor<?>[] {rawCandidates[0]};
				}
				else if (nonSyntheticConstructors == 2 && primaryConstructor != null &&
						defaultConstructor != null && !primaryConstructor.equals(defaultConstructor)) {

					candidateConstructors = new Constructor<?>[] {primaryConstructor, defaultConstructor};
				}
				else if (nonSyntheticConstructors == 1 && primaryConstructor != null) {
					candidateConstructors = new Constructor<?>[] {primaryConstructor};
				}
				else {
					//返null,即使用默认无参构造方法实例化对象。
					candidateConstructors = new Constructor<?>[0];
				}
				this.candidateConstructorsCache.put(beanClass, candidateConstructors);
			}
		}
	}
	return (candidateConstructors.length > 0 ? candidateConstructors : null);
}