详解Spring支持的各种数据类型的注入,你都用过哪些?

1 判断类型

注入的属性判断其类型:

成都创新互联是网站建设专家,致力于互联网品牌建设与网络营销,专业领域包括网站设计制作、成都网站制作、电商网站制作开发、成都微信小程序、微信营销、系统平台开发,与其他网站设计及系统开发公司不同,我们的整合解决方案结合了恒基网络品牌建设经验和互联网整合营销的理念,并将策略和执行紧密结合,且不断评估并优化我们的方案,为客户提供全方位的互联网品牌整合方案!

  1. Optional
  2. ObjectFactory
  3. ObjectProvider
  4. javax.inject.Provider
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {
    private static Class javaxInjectProviderClass;


    static {
        try {
            javaxInjectProviderClass =
                    ClassUtils.forName("javax.inject.Provider", DefaultListableBeanFactory.class.getClassLoader());
        } catch (ClassNotFoundException ex) {
            javaxInjectProviderClass = null;
        }
    }
    public Object resolveDependency(DependencyDescriptor descriptor, @Nullable String requestingBeanName,
            @Nullable Set autowiredBeanNames, @Nullable TypeConverter typeConverter) throws BeansException {
        if (Optional.class == descriptor.getDependencyType()) {
            return createOptionalDependency(descriptor, requestingBeanName);
        } else if (ObjectFactory.class == descriptor.getDependencyType() ||
                ObjectProvider.class == descriptor.getDependencyType()) {
            return new DependencyObjectProvider(descriptor, requestingBeanName);
        } else if (javaxInjectProviderClass == descriptor.getDependencyType()) {
            return new Jsr330Factory().createDependencyProvider(descriptor, requestingBeanName);
        } else {
            // 判断是否有@Lazy注解,如果有该注解那么会创建代理对象,否则返回null
            // 总结:如果注入的属性有@Lazy注解,那么会返回由ProxyFactory对象创建的代理对象
            Object result = getAutowireCandidateResolver().getLazyResolutionProxyIfNecessary(descriptor, requestingBeanName);
            if (result == null) {
                // 解析查找依赖注入的对象
                result = doResolveDependency(descriptor, requestingBeanName, autowiredBeanNames, typeConverter);
            }
            return result;
        }
    }
}

2 查找依赖对象

public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {
  public Object doResolveDependency(DependencyDescriptor descriptor, @Nullable String beanName,
            @Nullable Set autowiredBeanNames, @Nullable TypeConverter typeConverter) throws BeansException {
    InjectionPoint previousInjectionPoint = ConstructorResolver.setCurrentInjectionPoint(descriptor);
    try {
      Object shortcut = descriptor.resolveShortcut(this);
      if (shortcut != null) {
          return shortcut;
      }


      Class type = descriptor.getDependencyType();


      // 判断需要注入的类型是否是一个集合(如何:List beans)
      // 2.1 
      Object multipleBeans = resolveMultipleBeans(descriptor, beanName, autowiredBeanNames, typeConverter);
      if (multipleBeans != null) {
          return multipleBeans;
      }
      // 查找与所需类型匹配的bean实例
      // 2.2
      Map matchingBeans = findAutowireCandidates(beanName, type, descriptor);
      if (matchingBeans.isEmpty()) {
          if (isRequired(descriptor)) {
              raiseNoMatchingBeanFound(type, descriptor.getResolvableType(), descriptor);
          }
          return null;
      }


      String autowiredBeanName;
      Object instanceCandidate;


      // 如果找到多个
      if (matchingBeans.size() > 1) {
          // 在这里会判断多个Bean中是否有一个@Primary注解的Bean
          // 如果没有@Primary,继续获取是否有@Priority(优先级)注解
          // 如果没有则返回null,如果有则返回值越小的优先级越高(如果有多个优先级一样,则系统报错)
          // 如果上面都返回的null,那么最后会通过beanName进行匹配
          autowiredBeanName = determineAutowireCandidate(matchingBeans, descriptor);
          if (autowiredBeanName == null) {
              if (isRequired(descriptor) || !indicatesMultipleBeans(type)) {
                  // 如果是必须的 或者 不是集合类型的Bean注入;那么这里抛出异常
                  return descriptor.resolveNotUnique(descriptor.getResolvableType(), matchingBeans);
              } else {
                  return null;
              }
          }
          instanceCandidate = matchingBeans.get(autowiredBeanName);
      } else {
          // We have exactly one match.
          Map.Entry entry = matchingBeans.entrySet().iterator().next();
          autowiredBeanName = entry.getKey();
          instanceCandidate = entry.getValue();
      }


      if (autowiredBeanNames != null) {
          autowiredBeanNames.add(autowiredBeanName);
      }
      if (instanceCandidate instanceof Class) {
          instanceCandidate = descriptor.resolveCandidate(autowiredBeanName, type, this);
      }
      Object result = instanceCandidate;
      if (result instanceof NullBean) {
          if (isRequired(descriptor)) {
              raiseNoMatchingBeanFound(type, descriptor.getResolvableType(), descriptor);
          }
          result = null;
      }
      if (!ClassUtils.isAssignableValue(type, result)) {
          throw new BeanNotOfRequiredTypeException(autowiredBeanName, type, instanceCandidate.getClass());
      }
      return result;
    } finally {
        ConstructorResolver.setCurrentInjectionPoint(previousInjectionPoint);
    }
  }
}

2.1 判断集合类型

判断需要注入的属性的数据类型是否是如下几种。

  1. StreamDependencyDescriptor
  2. Array
  3. Collection
  4. Map
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {
  private Object resolveMultipleBeans(DependencyDescriptor descriptor, @Nullable String beanName,
          @Nullable Set autowiredBeanNames, @Nullable TypeConverter typeConverter) {


    Class type = descriptor.getDependencyType();


    if (descriptor instanceof StreamDependencyDescriptor) {
      Map matchingBeans = findAutowireCandidates(beanName, type, descriptor);
      if (autowiredBeanNames != null) {
        autowiredBeanNames.addAll(matchingBeans.keySet());
      }
      Stream stream = matchingBeans.keySet().stream()
              .map(name -> descriptor.resolveCandidate(name, type, this))
              .filter(bean -> !(bean instanceof NullBean));
      if (((StreamDependencyDescriptor) descriptor).isOrdered()) {
        stream = stream.sorted(adaptOrderComparator(matchingBeans));
      }
      return stream;
    } else if (type.isArray()) {
      Class componentType = type.getComponentType();
      ResolvableType resolvableType = descriptor.getResolvableType();
      Class resolvedArrayType = resolvableType.resolve(type);
      if (resolvedArrayType != type) {
        componentType = resolvableType.getComponentType().resolve();
      }
      if (componentType == null) {
        return null;
      }
      Map matchingBeans = findAutowireCandidates(beanName, componentType, new MultiElementDescriptor(descriptor));
      if (matchingBeans.isEmpty()) {
        return null;
      }
      if (autowiredBeanNames != null) {
        autowiredBeanNames.addAll(matchingBeans.keySet());
      }
      TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
      Object result = converter.convertIfNecessary(matchingBeans.values(), resolvedArrayType);
      if (result instanceof Object[]) {
        Comparator comparator = adaptDependencyComparator(matchingBeans);
        if (comparator != null) {
          Arrays.sort((Object[]) result, comparator);
        }
      }
      return result;
    } else if (Collection.class.isAssignableFrom(type) && type.isInterface()) {
      Class elementType = descriptor.getResolvableType().asCollection().resolveGeneric();
      if (elementType == null) {
        return null;
      }
      Map matchingBeans = findAutowireCandidates(beanName, elementType,new MultiElementDescriptor(descriptor));
      if (matchingBeans.isEmpty()) {
        return null;
      }
      if (autowiredBeanNames != null) {
        autowiredBeanNames.addAll(matchingBeans.keySet());
      }
      TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
      Object result = converter.convertIfNecessary(matchingBeans.values(), type);
      if (result instanceof List) {
        if (((List) result).size() > 1) {
          Comparator comparator = adaptDependencyComparator(matchingBeans);
          if (comparator != null) {
              ((List) result).sort(comparator);
          }
        }
      }
      return result;
    } else if (Map.class == type) {
      ResolvableType mapType = descriptor.getResolvableType().asMap();
      Class keyType = mapType.resolveGeneric(0);
      if (String.class != keyType) {
        return null;
      }
      Class valueType = mapType.resolveGeneric(1);
      if (valueType == null) {
        return null;
      }
      Map matchingBeans = findAutowireCandidates(beanName, valueType,new MultiElementDescriptor(descriptor));
      if (matchingBeans.isEmpty()) {
        return null;
      }
      if (autowiredBeanNames != null) {
        autowiredBeanNames.addAll(matchingBeans.keySet());
      }
      return matchingBeans;
    } else {
      return null;
    }
  }
}

2.2 查找Bean

public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {
  protected Map findAutowireCandidates(
          @Nullable String beanName, Class requiredType, DependencyDescriptor descriptor) {
    // 通过类型获取指定类型的Bean
    String[] candidateNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
            this, requiredType, true, descriptor.isEager());
    Map result = CollectionUtils.newLinkedHashMap(candidateNames.length);
    // resolvableDependencies 集合中保存了,容器内置的和自定义注册的依赖注入对象
    //(如:HttpServletRequest对象的注入为该集合内部指定的对象)
    for (Map.Entry, Object> classObjectEntry : this.resolvableDependencies.entrySet()) {
        Class autowiringType = classObjectEntry.getKey();
      if (autowiringType.isAssignableFrom(requiredType)) {
        Object autowiringValue = classObjectEntry.getValue();
        autowiringValue = AutowireUtils.resolveAutowiringValue(autowiringValue, requiredType);
        if (requiredType.isInstance(autowiringValue)) {
          result.put(ObjectUtils.identityToString(autowiringValue), autowiringValue);
          break;
        }
      }
    }
    for (String candidate : candidateNames) {
      // 判断是否自己引用自己
      if (!isSelfReference(beanName, candidate) && isAutowireCandidate(candidate, descriptor)) {
        addCandidateEntry(result, candidate, descriptor, requiredType);
      }
    }
    if (result.isEmpty()) {
      boolean multiple = indicatesMultipleBeans(requiredType);
      // Consider fallback matches if the first pass failed to find anything...
      DependencyDescriptor fallbackDescriptor = descriptor.forFallbackMatch();
      for (String candidate : candidateNames) {
        if (!isSelfReference(beanName, candidate) && isAutowireCandidate(candidate, fallbackDescriptor) &&
                (!multiple || getAutowireCandidateResolver().hasQualifier(descriptor))) {
          addCandidateEntry(result, candidate, descriptor, requiredType);
        }
      }
      if (result.isEmpty() && !multiple) {
        // Consider self references as a final pass...
        // but in the case of a dependency collection, not the very same bean itself.
        for (String candidate : candidateNames) {
          if (isSelfReference(beanName, candidate) &&
                  (!(descriptor instanceof MultiElementDescriptor) || !beanName.equals(candidate)) &&
                  isAutowireCandidate(candidate, fallbackDescriptor)) {
            addCandidateEntry(result, candidate, descriptor, requiredType);
          }
        }
      }
    }
    return result;
  }
}

以上就是Spring在注入属性的时候会判断是什么数据类型进行相应的操作

完毕!!!

分享名称:详解Spring支持的各种数据类型的注入,你都用过哪些?
网页网址:http://www.shufengxianlan.com/qtweb/news15/180765.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联