use of org.springframework.beans.factory.support.AutowireCandidateQualifier in project spring-framework by spring-projects.
the class BeanFactoryAnnotationUtils method isQualifierMatch.
/**
* Check whether the named bean declares a qualifier of the given name.
* @param qualifier the qualifier to match
* @param beanName the name of the candidate bean
* @param beanFactory the {@code BeanFactory} from which to retrieve the named bean
* @return {@code true} if either the bean definition (in the XML case)
* or the bean's factory method (in the {@code @Bean} case) defines a matching
* qualifier value (through {@code <qualifier>} or {@code @Qualifier})
* @since 5.0
*/
public static boolean isQualifierMatch(Predicate<String> qualifier, String beanName, BeanFactory beanFactory) {
// Try quick bean name or alias match first...
if (qualifier.test(beanName)) {
return true;
}
if (beanFactory != null) {
for (String alias : beanFactory.getAliases(beanName)) {
if (qualifier.test(alias)) {
return true;
}
}
try {
if (beanFactory instanceof ConfigurableBeanFactory) {
BeanDefinition bd = ((ConfigurableBeanFactory) beanFactory).getMergedBeanDefinition(beanName);
// Explicit qualifier metadata on bean definition? (typically in XML definition)
if (bd instanceof AbstractBeanDefinition) {
AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
AutowireCandidateQualifier candidate = abd.getQualifier(Qualifier.class.getName());
if (candidate != null) {
Object value = candidate.getAttribute(AutowireCandidateQualifier.VALUE_KEY);
if (value != null && qualifier.test(value.toString())) {
return true;
}
}
}
// Corresponding qualifier on factory method? (typically in configuration class)
if (bd instanceof RootBeanDefinition) {
Method factoryMethod = ((RootBeanDefinition) bd).getResolvedFactoryMethod();
if (factoryMethod != null) {
Qualifier targetAnnotation = AnnotationUtils.getAnnotation(factoryMethod, Qualifier.class);
if (targetAnnotation != null) {
return qualifier.test(targetAnnotation.value());
}
}
}
}
// Corresponding qualifier on bean implementation class? (for custom user types)
Class<?> beanType = beanFactory.getType(beanName);
if (beanType != null) {
Qualifier targetAnnotation = AnnotationUtils.getAnnotation(beanType, Qualifier.class);
if (targetAnnotation != null) {
return qualifier.test(targetAnnotation.value());
}
}
} catch (NoSuchBeanDefinitionException ex) {
// Ignore - can't compare qualifiers for a manually registered singleton object
}
}
return false;
}
Aggregations