zoukankan      html  css  js  c++  java
  • Spring 推断构造方法

    方法:org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#determineCandidateConstructors

     
     1 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#determineCandidateConstructors
     2 
     3 // Quick check on the concurrent map first, with minimal locking.
     4 Constructor<?>[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);
     5 if (candidateConstructors == null) {
     6    // Fully synchronized resolution now...
     7    synchronized (this.candidateConstructorsCache) {
     8       candidateConstructors = this.candidateConstructorsCache.get(beanClass);
     9       if (candidateConstructors == null) {
    10          Constructor<?>[] rawCandidates;
    11          try {
    12 //获取该class中的构造方法
    13             rawCandidates = beanClass.getDeclaredConstructors();
    14          }
    15          catch (Throwable ex) {
    16             throw new BeanCreationException(beanName,
    17                   "Resolution of declared constructors on bean Class [" + beanClass.getName() +
    18                   "] from ClassLoader [" + beanClass.getClassLoader() + "] failed", ex);
    19          }
    20          List<Constructor<?>> candidates = new ArrayList<Constructor<?>>(rawCandidates.length);
    21          Constructor<?> requiredConstructor = null;
    22          Constructor<?> defaultConstructor = null;
    23          for (Constructor<?> candidate : rawCandidates) {
    24 //在构造方法上查看是否有@Autowired
    25             AnnotationAttributes ann = findAutowiredAnnotation(candidate);
    26             if (ann == null) {
    27                Class<?> userClass = ClassUtils.getUserClass(beanClass);
    28                if (userClass != beanClass) {
    29                   try {
    30                      Constructor<?> superCtor =
    31                            userClass.getDeclaredConstructor(candidate.getParameterTypes());
    32                      ann = findAutowiredAnnotation(superCtor);
    33                   }
    34                   catch (NoSuchMethodException ex) {
    35                      // Simply proceed, no equivalent superclass constructor found...
    36                   }
    37                }
    38             }
    39             if (ann != null) {
    40                if (requiredConstructor != null) {
    41                   throw new BeanCreationException(beanName,
    42                         "Invalid autowire-marked constructor: " + candidate +
    43                         ". Found constructor with 'required' Autowired annotation already: " +
    44                         requiredConstructor);
    45                }
    46                boolean required = determineRequiredStatus(ann);
    47                if (required) {
    48                   if (!candidates.isEmpty()) {
    49                      throw new BeanCreationException(beanName,
    50                            "Invalid autowire-marked constructors: " + candidates +
    51                            ". Found constructor with 'required' Autowired annotation: " +
    52                            candidate);
    53                   }
    54            //1.如果既有@Autowired ,required=true。则赋值给requiredConstructor 
    55                   requiredConstructor = candidate;
    56                }
    57          //并添加到candidates
    58                candidates.add(candidate);
    59             }
    60             else if (candidate.getParameterTypes().length == 0) {
    61                defaultConstructor = candidate;
    62             }
    63          }
    64          if (!candidates.isEmpty()) {
    65             // Add default constructor to list of optional constructors, as fallback.
    66             if (requiredConstructor == null) {
    67                if (defaultConstructor != null) {
    68            //2.如果只有默认,没有required=true ,也添加到candidates
    69                   candidates.add(defaultConstructor);
    70                }
    71                else if (candidates.size() == 1 && logger.isWarnEnabled()) {
    72                   logger.warn("Inconsistent constructor declaration on bean with name '" + beanName +
    73                         "': single autowire-marked constructor flagged as optional - " +
    74                         "this constructor is effectively required since there is no " +
    75                         "default constructor to fall back to: " + candidates.get(0));
    76                }
    77             }
    78             candidateConstructors = candidates.toArray(new Constructor<?>[candidates.size()]);
    79          }
    80      //3.如果该方法只有一个构造方法。并且参数大于零。也把他加入candidates
    81          else if (rawCandidates.length == 1 && rawCandidates[0].getParameterTypes().length > 0) {
    82             candidateConstructors = new Constructor<?>[] {rawCandidates[0]};
    83          }
    84          else {
    85        //4.否则就没有构造方法。
    86             candidateConstructors = new Constructor<?>[0];
    87          }
    88          this.candidateConstructorsCache.put(beanClass, candidateConstructors);
    89       }
    90    }
    91 }
    92 return (candidateConstructors.length > 0 ? candidateConstructors : null);

  • 相关阅读:
    Revit命令之平面区域
    Revit平面视图控制
    电动手摇两用风机Revit族模型
    中田麻吉
    传递项目标准工具
    Sketchup机电专业BIM插件EngeeringToolBox
    机电专业协同模式
    lumion2.5下载及破解安装详细过程
    人防工程空调设计规范
    BIM软件之BIMsight
  • 原文地址:https://www.cnblogs.com/DidiLiu/p/13986237.html
Copyright © 2011-2022 走看看