84、Spring5.x源码之旅八十五Qualifier注解三

  • checkQualifier检查要候选bean定义是否有这个注解
      • checkQualifiers部分2

checkQualifier检查要候选bean定义是否有这个注解

这个方法其实就是检查候选的bean定义里有没有相关Qualifier注解,先检查下bean定义有没有添加type类型的全限定类型和短类名的AutowireCandidateQualifier对象,如果没有的话就看有没有设置type类型的AnnotatedElement对象,还没有的话就检查bean定义的工厂方法上有没有这个type注解,我们这个刚好在这上面,再没有的话就从bean装饰的定义中找注解,最后为空的话就从bean定义的目标类上去找。如果AutowireCandidateQualifier存在的话,就获取他的属性和type限定注解上的属性做匹配。最后返回是否找到或者是否匹配上。

protected boolean checkQualifier(
			BeanDefinitionHolder bdHolder, Annotation annotation, TypeConverter typeConverter) {
   
     

		Class<? extends Annotation> type = annotation.annotationType();
		RootBeanDefinition bd = (RootBeanDefinition) bdHolder.getBeanDefinition();

		AutowireCandidateQualifier qualifier = bd.getQualifier(type.getName());//检查bean定义有没有这个type全限定类名的AutowireCandidateQualifier对象
		if (qualifier == null) {
   
     
			qualifier = bd.getQualifier(ClassUtils.getShortName(type));//没有的话看下有没短的类名的
		}
		if (qualifier == null) {
   
     
			// First, check annotation on qualified element, if any
			Annotation targetAnnotation = getQualifiedElementAnnotation(bd, type);//检查bean定义里是否设置了type类型的限定类
			// Then, check annotation on factory method, if applicable
			if (targetAnnotation == null) {
   
     
				targetAnnotation = getFactoryMethodAnnotation(bd, type);//从工厂方法找是否有限定注解,这里frieds1的工厂方法上有Girl注解,所以得到了
			}
			if (targetAnnotation == null) {
   
     //从bean装饰的定义中找注解
				RootBeanDefinition dbd = getResolvedDecoratedDefinition(bd);
				if (dbd != null) {
   
     
					targetAnnotation = getFactoryMethodAnnotation(dbd, type);
				}
			}
			if (targetAnnotation == null) {
   
     //为空的话就从bean定义的目标类上去找
				// Look for matching annotation on the target class
				if (getBeanFactory() != null) {
   
     
					try {
   
     
						Class<?> beanType = getBeanFactory().getType(bdHolder.getBeanName());
						if (beanType != null) {
   
     
							targetAnnotation = AnnotationUtils.getAnnotation(ClassUtils.getUserClass(beanType), type);
						}
					}
					catch (NoSuchBeanDefinitionException ex) {
   
     
						// Not the usual case - simply forget about the type check...
					}
				}
				if (targetAnnotation == null && bd.hasBeanClass()) {
   
     
					targetAnnotation = AnnotationUtils.getAnnotation(ClassUtils.getUserClass(bd.getBeanClass()), type);
				}
			}
			if (targetAnnotation != null && targetAnnotation.equals(annotation)) {
   
     
				return true;//找到就返回
			}
		}
		// 匹配的限定注解的属性
		Map<String, Object> attributes = AnnotationUtils.getAnnotationAttributes(annotation);
		if (attributes.isEmpty() && qualifier == null) {
   
     
			// If no attributes, the qualifier must be present
			return false;
		}
		for (Map.Entry<String, Object> entry : attributes.entrySet()) {
   
     
			String attributeName = entry.getKey();
			Object expectedValue = entry.getValue();
			Object actualValue = null;
			// Check qualifier first
			if (qualifier != null) {
   
     
				actualValue = qualifier.getAttribute(attributeName);
			}
			if (actualValue == null) {
   
     
				// Fall back on bean definition attribute
				actualValue = bd.getAttribute(attributeName);
			}
			if (actualValue == null && attributeName.equals(AutowireCandidateQualifier.VALUE_KEY) &&
					expectedValue instanceof String && bdHolder.matchesName((String) expectedValue)) {
   
     
				// Fall back on bean name (or alias) match
				continue;
			}
			if (actualValue == null && qualifier != null) {
   
     
				// Fall back on default, but only if the qualifier is present
				actualValue = AnnotationUtils.getDefaultValue(annotation, attributeName);
			}
			if (actualValue != null) {
   
     
				actualValue = typeConverter.convertIfNecessary(actualValue, expectedValue.getClass());
			}
			if (!expectedValue.equals(actualValue)) {
   
     
				return false;
			}
		}
		return true;
	}

checkQualifiers部分2

如果前面判断不是Qualifier注解的,就进行注解上的注解获取,然后继续判断是不是Qualifier注解的。

	if (checkMeta) {
   
     //不是Qualifier直接的注解,就获取注解的注解,进行isQualifier判断
				boolean foundMeta = false;
				for (Annotation metaAnn : type.getAnnotations()) {
   
     
					Class<? extends Annotation> metaType = metaAnn.annotationType();
					if (isQualifier(metaType)) {
   
     
						foundMeta = true;
						// Only accept fallback match if @Qualifier annotation has a value...
						// Otherwise it is just a marker for a custom qualifier annotation.
						if ((fallbackToMeta && StringUtils.isEmpty(AnnotationUtils.getValue(metaAnn))) ||
								!checkQualifier(bdHolder, metaAnn, typeConverter)) {
   
     
							return false;//有Qualifier注解,不符合条件的就返回false
						}
					}
				}
				if (fallbackToMeta && !foundMeta) {
   
     //如果不是Qualifier直接,又没发现注解上有Qualifier注解就返回false
					return false;
				}
			}

大致流程就是这样,主要是看bean定义中是否有AutowireCandidateQualifier对象,没有的话看是否设置了AnnotatedElement对象,从这个上面去找有没有限定注解,再找不到从bean定义的工厂方法上找注解,再不行就从bean定义的目标类上去找。有的话是否全限定名或者短类名和限定注解相同,相同的话获取属性值和限定注解的属性值匹配,匹配上表示就返回true

好了,今天就到这里了,希望对学习理解有帮助,大神看见勿喷,仅为自己的学习理解,能力有限,请多包涵。