Search in sources :

Example 11 with AutowireCandidateQualifier

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;
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AutowireCandidateQualifier(org.springframework.beans.factory.support.AutowireCandidateQualifier) Method(java.lang.reflect.Method) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) AutowireCandidateQualifier(org.springframework.beans.factory.support.AutowireCandidateQualifier)

Aggregations

AutowireCandidateQualifier (org.springframework.beans.factory.support.AutowireCandidateQualifier)11 Test (org.junit.Test)7 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)7 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)5 TestBean (org.springframework.tests.sample.beans.TestBean)5 ITestBean (org.springframework.tests.sample.beans.ITestBean)4 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)4 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)4 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)2 Qualifier (org.springframework.beans.factory.annotation.Qualifier)2 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)2 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 Map (java.util.Map)1 Properties (java.util.Properties)1 BeanMetadataAttribute (org.springframework.beans.BeanMetadataAttribute)1 AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)1 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)1 BeanDefinitionCustomizer (org.springframework.beans.factory.config.BeanDefinitionCustomizer)1 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)1