spring源码-构造方法的推断

        Spring 是一个强大的依赖注入框架,在创建 Bean 时,框架需要通过一系列规则选择适合的构造方法来实例化对象。这一过程的复杂性主要体现在以下两个方面:

1 简介

1.1 为什么需要推断构造方法?

在实际开发中,一个类通常可能有以下几种构造方法:

  1. 无参构造方法:适合简单实例化场景。

  2. 单参数构造方法:需要传入一个参数来完成初始化。

  3. 多参数构造方法:需要多个依赖对象,参数类型可能重复或复杂。

例如:

public class ExampleService {
    public ExampleService() {
        // 无参构造方法
    }
    public ExampleService(String name) {
        // 单参构造方法
    }
    public ExampleService(String name, int age) {
        // 多参构造方法
    }
}

如果一个类存在多个构造方法,Spring 需要明确:

  • 哪个构造方法是合适的?

  • 如何满足其参数依赖?

这种选择需要根据 构造方法的参数类型注解标记 和 外部传参 等信息进行推断。

1.2 推断构造方法的典型场景

以下几种场景最常见:

  1. 显式配置构造方法参数

    1. 在 XML 或 Java Config 中明确指定构造方法的参数值。

    2. Spring 会优先使用匹配的构造方法。

<bean id="exampleService" class="com.example.ExampleService">
    <constructor-arg value="John" />
</bean>

@Bean
public ExampleService exampleService() {
    return new ExampleService("John");
}
  1. 使用 @Autowired 标注构造方法

    1. 如果多个构造方法中有一个标注了 @Autowired,Spring 优先选择它。

    2. 当 @Autowired 的构造方法参数存在依赖时,Spring 自动解析和注入。

public class ExampleService {
    @Autowired
    public ExampleService(DependencyA dependencyA) {
        // 自动注入 dependencyA
    }
}
  1. 无参构造方法的默认处理

    1. 如果没有显式配置参数,也没有标注 @Autowired 的构造方法,Spring 默认使用无参构造方法。

public class ExampleService {
    public ExampleService() {
        // 无参构造
    }
}
  1. 构造参数动态推断

    1. 当存在多个构造方法,且参数类型无法完全匹配时,Spring 会通过 类型转换 或 默认值 来动态推断。

public class ExampleService {
    public ExampleService(String name, Integer age) {
        // 参数动态匹配
    }
}

1.3 推断过程的设计目标

Spring 的设计目标是实现 灵活且高效 的构造方法选择:

  • 支持多种配置方式:兼容 XML、注解、Java Config。

  • 智能参数推断:即使参数类型复杂或部分缺失,也能动态适配。

  • 注解优先:@Autowired 标注优先级最高,明确表达开发者的意图。

  • 默认安全性:若无任何特殊配置,仍然能通过无参构造方法完成实例化。

通过背景介绍,可以明确,Spring 的构造方法推断不仅是一个简单的选择逻辑,而是结合了 配置解析依赖注入 和 参数适配 的完整体系。

核心流程分析

2.1 入口

核心类:AbstractAutowireCapableBeanFactory.java

核心方法:createBeanInstance

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

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

    // 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);
}
2.2 获取构造方法

核心类:AutowiredAnnotationBeanPostProcessor.java

核心方法:determineCandidateConstructors

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());
                         ann = findAutowiredAnnotation(superCtor);
                      }
                      catch (NoSuchMethodException ex) {
                         // Simply proceed, no equivalent superclass constructor found...
                      }
                   }
                }

                // 当前构造方法上加了@Autowired
                if (ann != null) {
                   // 整个类中如果有一个required为true的构造方法,那就不能有其他的加了@Autowired的构造方法
                   // 已经扫描到一个@Autowired注解并且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) {
                      // 如果扫描的了一个required为true的构造方法 并且上轮扫描到被@Autowired标记的构造方法
                      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);
}
2.3 构造方法返回null

核心类:AbstractAutowireCapableBeanFactory.java

核心方法: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 bw = new BeanWrapperImpl(beanInstance);
       initBeanWrapper(bw);
       return bw;
    }
    catch (Throwable ex) {
       throw new BeanCreationException(
             mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);
    }
}

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();
             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);
    }
}
2.4 构造方法返回有值

核心类:ConstructorResolver.java

核心方法:autowireConstructor

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. 没有@Autowired情况

    1. 如果只有一个构造器,那么就直接用这个构造器实例化

    2. 如果有多个构造器,且不包含无参构造器,报错。

    3. 如果有多个构造器且包含无参构造器,用无参构造器。

  2. 有@Autowired情况

    1. 如果出现了一个@Autowired(required=true)的注解但是还存在其他@Autowired的构造方法,直接报错。

    2. 如果仅有一个@Autowired(required=true)的构造方法,那就直接用这个构造方法。

    3. 如果存在多个@Autowired(required=false)的构造方法,那么用参数最多的那个构造方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值