Java 类org.springframework.beans.factory.support.AutowireCandidateQualifier 实例源码

项目:hekate    文件:HekateLockServiceConfigurer.java   
@Override
protected void registerBeans(InjectLock annotation, ResolvableType targetType, BeanDefinitionRegistry registry) {
    String name = LockBean.class.getName() + "-" + annotation.name() + "--" + annotation.name();

    if (!registry.containsBeanDefinition(name)) {
        AbstractBeanDefinition def = BeanDefinitionBuilder.rootBeanDefinition(LockBean.class)
            .setLazyInit(true)
            .addPropertyValue("region", annotation.region())
            .addPropertyValue("name", annotation.name())
            .getBeanDefinition();

        AutowireCandidateQualifier qualifier = new AutowireCandidateQualifier(annotation.annotationType());
        qualifier.addMetadataAttribute(new BeanMetadataAttribute("region", annotation.region()));
        qualifier.addMetadataAttribute(new BeanMetadataAttribute("name", annotation.name()));

        def.addQualifier(qualifier);

        registry.registerBeanDefinition(name, def);
    }
}
项目:spring4-understanding    文件:QualifierAnnotationTests.java   
@Test
public void testQualifiedByParentValue() {
    StaticApplicationContext parent = new StaticApplicationContext();
    GenericBeanDefinition parentLarry = new GenericBeanDefinition();
    parentLarry.setBeanClass(Person.class);
    parentLarry.getPropertyValues().add("name", "ParentLarry");
    parentLarry.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "parentLarry"));
    parent.registerBeanDefinition("someLarry", parentLarry);
    GenericBeanDefinition otherLarry = new GenericBeanDefinition();
    otherLarry.setBeanClass(Person.class);
    otherLarry.getPropertyValues().add("name", "OtherLarry");
    otherLarry.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "otherLarry"));
    parent.registerBeanDefinition("someOtherLarry", otherLarry);
    parent.refresh();

    StaticApplicationContext context = new StaticApplicationContext(parent);
    BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
    reader.loadBeanDefinitions(CONFIG_LOCATION);
    context.registerSingleton("testBean", QualifiedByParentValueTestBean.class);
    context.refresh();
    QualifiedByParentValueTestBean testBean = (QualifiedByParentValueTestBean) context.getBean("testBean");
    Person person = testBean.getLarry();
    assertEquals("ParentLarry", person.getName());
}
项目:spring4-understanding    文件:AutowiredAnnotationBeanPostProcessorTests.java   
@Test
public void testObjectFactoryQualifierInjection() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryQualifierInjectionBean.class));
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "testBean"));
    bf.registerBeanDefinition("testBean", bd);
    bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class));

    ObjectFactoryQualifierInjectionBean bean = (ObjectFactoryQualifierInjectionBean) bf.getBean("annotatedBean");
    assertSame(bf.getBean("testBean"), bean.getTestBean());
    bf.destroySingletons();
}
项目:spring4-understanding    文件:InjectAnnotationBeanPostProcessorTests.java   
@Test
public void testObjectFactoryInjection() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryQualifierFieldInjectionBean.class));
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "testBean"));
    bf.registerBeanDefinition("testBean", bd);
    bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class));

    ObjectFactoryQualifierFieldInjectionBean bean = (ObjectFactoryQualifierFieldInjectionBean) bf.getBean("annotatedBean");
    assertSame(bf.getBean("testBean"), bean.getTestBean());
    bf.destroySingletons();
}
项目:spring4-understanding    文件:InjectAnnotationBeanPostProcessorTests.java   
@Test
public void testObjectFactoryFieldInjectionIntoPrototypeBean() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    RootBeanDefinition annotatedBeanDefinition = new RootBeanDefinition(ObjectFactoryQualifierFieldInjectionBean.class);
    annotatedBeanDefinition.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    bf.registerBeanDefinition("annotatedBean", annotatedBeanDefinition);
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "testBean"));
    bf.registerBeanDefinition("testBean", bd);
    bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class));

    ObjectFactoryQualifierFieldInjectionBean bean = (ObjectFactoryQualifierFieldInjectionBean) bf.getBean("annotatedBean");
    assertSame(bf.getBean("testBean"), bean.getTestBean());
    ObjectFactoryQualifierFieldInjectionBean anotherBean = (ObjectFactoryQualifierFieldInjectionBean) bf.getBean("annotatedBean");
    assertNotSame(anotherBean, bean);
    assertSame(bf.getBean("testBean"), bean.getTestBean());
}
项目:spring4-understanding    文件:InjectAnnotationBeanPostProcessorTests.java   
@Test
public void testObjectFactoryMethodInjectionIntoPrototypeBean() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    RootBeanDefinition annotatedBeanDefinition = new RootBeanDefinition(ObjectFactoryQualifierMethodInjectionBean.class);
    annotatedBeanDefinition.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    bf.registerBeanDefinition("annotatedBean", annotatedBeanDefinition);
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "testBean"));
    bf.registerBeanDefinition("testBean", bd);
    bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class));

    ObjectFactoryQualifierMethodInjectionBean bean = (ObjectFactoryQualifierMethodInjectionBean) bf.getBean("annotatedBean");
    assertSame(bf.getBean("testBean"), bean.getTestBean());
    ObjectFactoryQualifierMethodInjectionBean anotherBean = (ObjectFactoryQualifierMethodInjectionBean) bf.getBean("annotatedBean");
    assertNotSame(anotherBean, bean);
    assertSame(bf.getBean("testBean"), bean.getTestBean());
}
项目:class-guard    文件:AutowiredAnnotationBeanPostProcessorTests.java   
@Test
public void testObjectFactoryQualifierInjection() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryQualifierInjectionBean.class));
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "testBean"));
    bf.registerBeanDefinition("testBean", bd);
    bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class));

    ObjectFactoryQualifierInjectionBean bean = (ObjectFactoryQualifierInjectionBean) bf.getBean("annotatedBean");
    assertSame(bf.getBean("testBean"), bean.getTestBean());
    bf.destroySingletons();
}
项目:class-guard    文件:InjectAnnotationBeanPostProcessorTests.java   
@Test
public void testObjectFactoryInjection() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryQualifierFieldInjectionBean.class));
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "testBean"));
    bf.registerBeanDefinition("testBean", bd);
    bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class));

    ObjectFactoryQualifierFieldInjectionBean bean = (ObjectFactoryQualifierFieldInjectionBean) bf.getBean("annotatedBean");
    assertSame(bf.getBean("testBean"), bean.getTestBean());
    bf.destroySingletons();
}
项目:class-guard    文件:InjectAnnotationBeanPostProcessorTests.java   
@Test
public void testObjectFactoryFieldInjectionIntoPrototypeBean() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    RootBeanDefinition annotatedBeanDefinition = new RootBeanDefinition(ObjectFactoryQualifierFieldInjectionBean.class);
    annotatedBeanDefinition.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    bf.registerBeanDefinition("annotatedBean", annotatedBeanDefinition);
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "testBean"));
    bf.registerBeanDefinition("testBean", bd);
    bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class));

    ObjectFactoryQualifierFieldInjectionBean bean = (ObjectFactoryQualifierFieldInjectionBean) bf.getBean("annotatedBean");
    assertSame(bf.getBean("testBean"), bean.getTestBean());
    ObjectFactoryQualifierFieldInjectionBean anotherBean = (ObjectFactoryQualifierFieldInjectionBean) bf.getBean("annotatedBean");
    assertNotSame(anotherBean, bean);
    assertSame(bf.getBean("testBean"), bean.getTestBean());
}
项目:class-guard    文件:InjectAnnotationBeanPostProcessorTests.java   
@Test
public void testObjectFactoryMethodInjectionIntoPrototypeBean() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    RootBeanDefinition annotatedBeanDefinition = new RootBeanDefinition(ObjectFactoryQualifierMethodInjectionBean.class);
    annotatedBeanDefinition.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    bf.registerBeanDefinition("annotatedBean", annotatedBeanDefinition);
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "testBean"));
    bf.registerBeanDefinition("testBean", bd);
    bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class));

    ObjectFactoryQualifierMethodInjectionBean bean = (ObjectFactoryQualifierMethodInjectionBean) bf.getBean("annotatedBean");
    assertSame(bf.getBean("testBean"), bean.getTestBean());
    ObjectFactoryQualifierMethodInjectionBean anotherBean = (ObjectFactoryQualifierMethodInjectionBean) bf.getBean("annotatedBean");
    assertNotSame(anotherBean, bean);
    assertSame(bf.getBean("testBean"), bean.getTestBean());
}
项目:hekate    文件:HekateRpcServiceConfigurer.java   
@Override
protected void registerBeans(InjectRpcClient annotation, ResolvableType targetType, BeanDefinitionRegistry registry) {
    String tag = annotation.tag().isEmpty() ? "" : '#' + annotation.tag();

    String name = RpcClientBean.class.getName() + tag + "-" + targetType.toString();

    if (!registry.containsBeanDefinition(name)) {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(RpcClientBean.class)
            .setLazyInit(true)
            .addPropertyValue("rpcInterface", targetType.getType().getTypeName());

        if (!annotation.tag().isEmpty()) {
            builder.addPropertyValue("tag", annotation.tag());
        }

        AbstractBeanDefinition def = builder.getBeanDefinition();

        AutowireCandidateQualifier qualifier = new AutowireCandidateQualifier(annotation.annotationType());

        if (!annotation.tag().isEmpty()) {
            qualifier.setAttribute("tag", annotation.tag());
        }

        def.addQualifier(qualifier);

        registry.registerBeanDefinition(name, def);
    }
}
项目:hekate    文件:HekateNetworkServiceConfigurer.java   
@Override
protected void registerBeans(InjectConnector annotation, ResolvableType targetType, BeanDefinitionRegistry registry) {
    String name = NetworkConnectorBean.class.getName() + "-" + annotation.value();

    if (!registry.containsBeanDefinition(name)) {
        AbstractBeanDefinition def = BeanDefinitionBuilder.rootBeanDefinition(NetworkConnectorBean.class)
            .setLazyInit(true)
            .addPropertyValue("protocol", annotation.value())
            .getBeanDefinition();

        def.addQualifier(new AutowireCandidateQualifier(annotation.annotationType(), annotation.value()));

        registry.registerBeanDefinition(name, def);
    }
}
项目:hekate    文件:HekateLocalMetricsServiceConfigurer.java   
@Override
protected void registerBeans(InjectCounter annotation, ResolvableType targetType, BeanDefinitionRegistry registry) {
    String name = CounterMetricBean.class.getName() + "-" + annotation.value();

    if (!registry.containsBeanDefinition(name)) {
        AbstractBeanDefinition def = BeanDefinitionBuilder.rootBeanDefinition(CounterMetricBean.class)
            .setLazyInit(true)
            .addPropertyValue("name", annotation.value())
            .getBeanDefinition();

        def.addQualifier(new AutowireCandidateQualifier(annotation.annotationType(), annotation.value()));

        registry.registerBeanDefinition(name, def);
    }
}
项目:hekate    文件:HekateLocalMetricsServiceConfigurer.java   
@Override
protected void registerBeans(InjectMetric annotation, ResolvableType targetType, BeanDefinitionRegistry registry) {
    String name = MetricBean.class.getName() + "-" + annotation.value();

    if (!registry.containsBeanDefinition(name)) {
        AbstractBeanDefinition def = BeanDefinitionBuilder.rootBeanDefinition(MetricBean.class)
            .setLazyInit(true)
            .addPropertyValue("name", annotation.value())
            .getBeanDefinition();

        def.addQualifier(new AutowireCandidateQualifier(annotation.annotationType(), annotation.value()));

        registry.registerBeanDefinition(name, def);
    }
}
项目:hekate    文件:HekateLockServiceConfigurer.java   
@Override
protected void registerBeans(InjectLockRegion annotation, ResolvableType targetType, BeanDefinitionRegistry registry) {
    String name = LockRegionBean.class.getName() + "-" + annotation.value();

    if (!registry.containsBeanDefinition(name)) {
        AbstractBeanDefinition def = BeanDefinitionBuilder.rootBeanDefinition(LockRegionBean.class)
            .setLazyInit(true)
            .addPropertyValue("region", annotation.value())
            .getBeanDefinition();

        def.addQualifier(new AutowireCandidateQualifier(annotation.annotationType(), annotation.value()));

        registry.registerBeanDefinition(name, def);
    }
}
项目:hekate    文件:HekateMessagingServiceConfigurer.java   
@Override
protected void registerBeans(InjectChannel annotation, ResolvableType targetType, BeanDefinitionRegistry registry) {
    String name = MessagingChannelBean.class.getName() + "-" + annotation.value();

    if (!registry.containsBeanDefinition(name)) {
        AbstractBeanDefinition def = BeanDefinitionBuilder.rootBeanDefinition(MessagingChannelBean.class)
            .setLazyInit(true)
            .addPropertyValue("channel", annotation.value())
            .getBeanDefinition();

        def.addQualifier(new AutowireCandidateQualifier(annotation.annotationType(), annotation.value()));

        registry.registerBeanDefinition(name, def);
    }
}
项目:lams    文件:AnnotatedBeanDefinitionReader.java   
public void registerBean(Class<?> annotatedClass, String name,
        @SuppressWarnings("unchecked") Class<? extends Annotation>... qualifiers) {

    AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
    if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {
        return;
    }

    ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
    abd.setScope(scopeMetadata.getScopeName());
    String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));
    AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
    if (qualifiers != null) {
        for (Class<? extends Annotation> qualifier : qualifiers) {
            if (Primary.class.equals(qualifier)) {
                abd.setPrimary(true);
            }
            else if (Lazy.class.equals(qualifier)) {
                abd.setLazyInit(true);
            }
            else {
                abd.addQualifier(new AutowireCandidateQualifier(qualifier));
            }
        }
    }

    BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
    definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
    BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
}
项目:lams    文件:BeanFactoryAnnotationUtils.java   
/**
 * 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 bf 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})
 */
private static boolean isQualifierMatch(String qualifier, String beanName, ConfigurableListableBeanFactory bf) {
    if (bf.containsBean(beanName)) {
        try {
            BeanDefinition bd = bf.getMergedBeanDefinition(beanName);
            if (bd instanceof AbstractBeanDefinition) {
                AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
                AutowireCandidateQualifier candidate = abd.getQualifier(Qualifier.class.getName());
                if ((candidate != null && qualifier.equals(candidate.getAttribute(AutowireCandidateQualifier.VALUE_KEY))) ||
                        qualifier.equals(beanName) || ObjectUtils.containsElement(bf.getAliases(beanName), qualifier)) {
                    return true;
                }
            }
            if (bd instanceof RootBeanDefinition) {
                Method factoryMethod = ((RootBeanDefinition) bd).getResolvedFactoryMethod();
                if (factoryMethod != null) {
                    Qualifier targetAnnotation = factoryMethod.getAnnotation(Qualifier.class);
                    if (targetAnnotation != null && qualifier.equals(targetAnnotation.value())) {
                        return true;
                    }
                }
            }
        }
        catch (NoSuchBeanDefinitionException ex) {
            // Ignore - can't compare qualifiers for a manually registered singleton object
        }
    }
    return false;
}
项目:lams    文件:BeanDefinitionParserDelegate.java   
/**
 * Parse a qualifier element.
 */
public void parseQualifierElement(Element ele, AbstractBeanDefinition bd) {
    String typeName = ele.getAttribute(TYPE_ATTRIBUTE);
    if (!StringUtils.hasLength(typeName)) {
        error("Tag 'qualifier' must have a 'type' attribute", ele);
        return;
    }
    this.parseState.push(new QualifierEntry(typeName));
    try {
        AutowireCandidateQualifier qualifier = new AutowireCandidateQualifier(typeName);
        qualifier.setSource(extractSource(ele));
        String value = ele.getAttribute(VALUE_ATTRIBUTE);
        if (StringUtils.hasLength(value)) {
            qualifier.setAttribute(AutowireCandidateQualifier.VALUE_KEY, value);
        }
        NodeList nl = ele.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            if (isCandidateElement(node) && nodeNameEquals(node, QUALIFIER_ATTRIBUTE_ELEMENT)) {
                Element attributeEle = (Element) node;
                String attributeName = attributeEle.getAttribute(KEY_ATTRIBUTE);
                String attributeValue = attributeEle.getAttribute(VALUE_ATTRIBUTE);
                if (StringUtils.hasLength(attributeName) && StringUtils.hasLength(attributeValue)) {
                    BeanMetadataAttribute attribute = new BeanMetadataAttribute(attributeName, attributeValue);
                    attribute.setSource(extractSource(attributeEle));
                    qualifier.addMetadataAttribute(attribute);
                }
                else {
                    error("Qualifier 'attribute' tag must have a 'name' and 'value'", attributeEle);
                    return;
                }
            }
        }
        bd.addQualifier(qualifier);
    }
    finally {
        this.parseState.pop();
    }
}
项目:spring4-understanding    文件:AnnotatedBeanDefinitionReader.java   
public void registerBean(Class<?> annotatedClass, String name,
        @SuppressWarnings("unchecked") Class<? extends Annotation>... qualifiers) {

    AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
    if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {
        return;
    }

    ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
    abd.setScope(scopeMetadata.getScopeName());
    String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));
    AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
    if (qualifiers != null) {
        for (Class<? extends Annotation> qualifier : qualifiers) {
            if (Primary.class == qualifier) {
                abd.setPrimary(true);
            }
            else if (Lazy.class == qualifier) {
                abd.setLazyInit(true);
            }
            else {
                abd.addQualifier(new AutowireCandidateQualifier(qualifier));
            }
        }
    }

    BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
    definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
    BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
}
项目:spring4-understanding    文件:BeanFactoryAnnotationUtils.java   
/**
 * 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 bf 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})
 */
private static boolean isQualifierMatch(String qualifier, String beanName, ConfigurableListableBeanFactory bf) {
    if (bf.containsBean(beanName)) {
        try {
            BeanDefinition bd = bf.getMergedBeanDefinition(beanName);
            if (bd instanceof AbstractBeanDefinition) {
                AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
                AutowireCandidateQualifier candidate = abd.getQualifier(Qualifier.class.getName());
                if ((candidate != null && qualifier.equals(candidate.getAttribute(AutowireCandidateQualifier.VALUE_KEY))) ||
                        qualifier.equals(beanName) || ObjectUtils.containsElement(bf.getAliases(beanName), qualifier)) {
                    return true;
                }
            }
            if (bd instanceof RootBeanDefinition) {
                Method factoryMethod = ((RootBeanDefinition) bd).getResolvedFactoryMethod();
                if (factoryMethod != null) {
                    Qualifier targetAnnotation = factoryMethod.getAnnotation(Qualifier.class);
                    if (targetAnnotation != null && qualifier.equals(targetAnnotation.value())) {
                        return true;
                    }
                }
            }
        }
        catch (NoSuchBeanDefinitionException ex) {
            // Ignore - can't compare qualifiers for a manually registered singleton object
        }
    }
    return false;
}
项目:spring4-understanding    文件:BeanDefinitionParserDelegate.java   
/**
 * Parse a qualifier element.
 */
public void parseQualifierElement(Element ele, AbstractBeanDefinition bd) {
    String typeName = ele.getAttribute(TYPE_ATTRIBUTE);
    if (!StringUtils.hasLength(typeName)) {
        error("Tag 'qualifier' must have a 'type' attribute", ele);
        return;
    }
    this.parseState.push(new QualifierEntry(typeName));
    try {
        AutowireCandidateQualifier qualifier = new AutowireCandidateQualifier(typeName);
        qualifier.setSource(extractSource(ele));
        String value = ele.getAttribute(VALUE_ATTRIBUTE);
        if (StringUtils.hasLength(value)) {
            qualifier.setAttribute(AutowireCandidateQualifier.VALUE_KEY, value);
        }
        NodeList nl = ele.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            if (isCandidateElement(node) && nodeNameEquals(node, QUALIFIER_ATTRIBUTE_ELEMENT)) {
                Element attributeEle = (Element) node;
                String attributeName = attributeEle.getAttribute(KEY_ATTRIBUTE);
                String attributeValue = attributeEle.getAttribute(VALUE_ATTRIBUTE);
                if (StringUtils.hasLength(attributeName) && StringUtils.hasLength(attributeValue)) {
                    BeanMetadataAttribute attribute = new BeanMetadataAttribute(attributeName, attributeValue);
                    attribute.setSource(extractSource(attributeEle));
                    qualifier.addMetadataAttribute(attribute);
                }
                else {
                    error("Qualifier 'attribute' tag must have a 'name' and 'value'", attributeEle);
                    return;
                }
            }
        }
        bd.addQualifier(qualifier);
    }
    finally {
        this.parseState.pop();
    }
}
项目:spring4-understanding    文件:InjectAnnotationBeanPostProcessorTests.java   
@Test
public void testObjectFactoryQualifierInjection() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryQualifierFieldInjectionBean.class));
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "testBean"));
    bf.registerBeanDefinition("testBean", bd);

    ObjectFactoryQualifierFieldInjectionBean bean = (ObjectFactoryQualifierFieldInjectionBean) bf.getBean("annotatedBean");
    assertSame(bf.getBean("testBean"), bean.getTestBean());
    bf.destroySingletons();
}
项目:my-spring-cache-redis    文件:AnnotatedBeanDefinitionReader.java   
public void registerBean(Class<?> annotatedClass, String name,
        @SuppressWarnings("unchecked") Class<? extends Annotation>... qualifiers) {

    AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
    if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {
        return;
    }

    ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
    abd.setScope(scopeMetadata.getScopeName());
    String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));
    AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
    if (qualifiers != null) {
        for (Class<? extends Annotation> qualifier : qualifiers) {
            if (Primary.class.equals(qualifier)) {
                abd.setPrimary(true);
            }
            else if (Lazy.class.equals(qualifier)) {
                abd.setLazyInit(true);
            }
            else {
                abd.addQualifier(new AutowireCandidateQualifier(qualifier));
            }
        }
    }

    BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
    definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
    BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
}
项目:spring    文件:AnnotatedBeanDefinitionReader.java   
public void registerBean(Class<?> annotatedClass, String name,
        @SuppressWarnings("unchecked") Class<? extends Annotation>... qualifiers) {

    AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
    if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {
        return;
    }

    ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
    abd.setScope(scopeMetadata.getScopeName());
    String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));
    AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
    if (qualifiers != null) {
        for (Class<? extends Annotation> qualifier : qualifiers) {
            if (Primary.class == qualifier) {
                abd.setPrimary(true);
            }
            else if (Lazy.class == qualifier) {
                abd.setLazyInit(true);
            }
            else {
                abd.addQualifier(new AutowireCandidateQualifier(qualifier));
            }
        }
    }

    BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
    definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
    BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
}
项目:spring    文件:BeanDefinitionParserDelegate.java   
/**
 * Parse a qualifier element.
 */
public void parseQualifierElement(Element ele, AbstractBeanDefinition bd) {
    String typeName = ele.getAttribute(TYPE_ATTRIBUTE);
    if (!StringUtils.hasLength(typeName)) {
        error("Tag 'qualifier' must have a 'type' attribute", ele);
        return;
    }
    this.parseState.push(new QualifierEntry(typeName));
    try {
        AutowireCandidateQualifier qualifier = new AutowireCandidateQualifier(typeName);
        qualifier.setSource(extractSource(ele));
        String value = ele.getAttribute(VALUE_ATTRIBUTE);
        if (StringUtils.hasLength(value)) {
            qualifier.setAttribute(AutowireCandidateQualifier.VALUE_KEY, value);
        }
        NodeList nl = ele.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            if (isCandidateElement(node) && nodeNameEquals(node, QUALIFIER_ATTRIBUTE_ELEMENT)) {
                Element attributeEle = (Element) node;
                String attributeName = attributeEle.getAttribute(KEY_ATTRIBUTE);
                String attributeValue = attributeEle.getAttribute(VALUE_ATTRIBUTE);
                if (StringUtils.hasLength(attributeName) && StringUtils.hasLength(attributeValue)) {
                    BeanMetadataAttribute attribute = new BeanMetadataAttribute(attributeName, attributeValue);
                    attribute.setSource(extractSource(attributeEle));
                    qualifier.addMetadataAttribute(attribute);
                }
                else {
                    error("Qualifier 'attribute' tag must have a 'name' and 'value'", attributeEle);
                    return;
                }
            }
        }
        bd.addQualifier(qualifier);
    }
    finally {
        this.parseState.pop();
    }
}
项目:spring-cloud-stream    文件:BindingBeanDefinitionRegistryUtils.java   
private static void registerBindingTargetBeanDefinition(Class<? extends Annotation> qualifier,
        String qualifierValue, String name, String bindingTargetInterfaceBeanName,
        String bindingTargetInterfaceMethodName, BeanDefinitionRegistry registry) {

    if (registry.containsBeanDefinition(name)) {
        throw new BeanDefinitionStoreException(bindingTargetInterfaceBeanName, name,
                "bean definition with this name already exists - " + registry.getBeanDefinition(name));
    }

    RootBeanDefinition rootBeanDefinition = new RootBeanDefinition();
    rootBeanDefinition.setFactoryBeanName(bindingTargetInterfaceBeanName);
    rootBeanDefinition.setUniqueFactoryMethodName(bindingTargetInterfaceMethodName);
    rootBeanDefinition.addQualifier(new AutowireCandidateQualifier(qualifier, qualifierValue));
    registry.registerBeanDefinition(name, rootBeanDefinition);
}
项目:class-guard    文件:AnnotatedBeanDefinitionReader.java   
public void registerBean(Class<?> annotatedClass, String name, Class<? extends Annotation>... qualifiers) {
    AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
    AnnotationMetadata metadata = abd.getMetadata();
    if (metadata.isAnnotated(Profile.class.getName())) {
        AnnotationAttributes profile = MetadataUtils.attributesFor(metadata, Profile.class);
        if (!this.environment.acceptsProfiles(profile.getStringArray("value"))) {
            return;
        }
    }
    ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
    abd.setScope(scopeMetadata.getScopeName());
    String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));
    AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
    if (qualifiers != null) {
        for (Class<? extends Annotation> qualifier : qualifiers) {
            if (Primary.class.equals(qualifier)) {
                abd.setPrimary(true);
            }
            else if (Lazy.class.equals(qualifier)) {
                abd.setLazyInit(true);
            }
            else {
                abd.addQualifier(new AutowireCandidateQualifier(qualifier));
            }
        }
    }
    BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
    definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
    BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
}
项目:class-guard    文件:BeanFactoryAnnotationUtils.java   
/**
 * 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 bf 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})
 */
private static boolean isQualifierMatch(String qualifier, String beanName, ConfigurableListableBeanFactory bf) {
    if (bf.containsBean(beanName)) {
        try {
            BeanDefinition bd = bf.getMergedBeanDefinition(beanName);
            if (bd instanceof AbstractBeanDefinition) {
                AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
                AutowireCandidateQualifier candidate = abd.getQualifier(Qualifier.class.getName());
                if ((candidate != null && qualifier.equals(candidate.getAttribute(AutowireCandidateQualifier.VALUE_KEY))) ||
                        qualifier.equals(beanName) || ObjectUtils.containsElement(bf.getAliases(beanName), qualifier)) {
                    return true;
                }
            }
            if (bd instanceof RootBeanDefinition) {
                Method factoryMethod = ((RootBeanDefinition) bd).getResolvedFactoryMethod();
                if (factoryMethod != null) {
                    Qualifier targetAnnotation = factoryMethod.getAnnotation(Qualifier.class);
                    if (targetAnnotation != null && qualifier.equals(targetAnnotation.value())) {
                        return true;
                    }
                }
            }
        }
        catch (NoSuchBeanDefinitionException ex) {
            // Ignore - can't compare qualifiers for a manually registered singleton object
        }
    }
    return false;
}
项目:class-guard    文件:BeanDefinitionParserDelegate.java   
/**
 * Parse a qualifier element.
 */
public void parseQualifierElement(Element ele, AbstractBeanDefinition bd) {
    String typeName = ele.getAttribute(TYPE_ATTRIBUTE);
    if (!StringUtils.hasLength(typeName)) {
        error("Tag 'qualifier' must have a 'type' attribute", ele);
        return;
    }
    this.parseState.push(new QualifierEntry(typeName));
    try {
        AutowireCandidateQualifier qualifier = new AutowireCandidateQualifier(typeName);
        qualifier.setSource(extractSource(ele));
        String value = ele.getAttribute(VALUE_ATTRIBUTE);
        if (StringUtils.hasLength(value)) {
            qualifier.setAttribute(AutowireCandidateQualifier.VALUE_KEY, value);
        }
        NodeList nl = ele.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            if (isCandidateElement(node) && nodeNameEquals(node, QUALIFIER_ATTRIBUTE_ELEMENT)) {
                Element attributeEle = (Element) node;
                String attributeName = attributeEle.getAttribute(KEY_ATTRIBUTE);
                String attributeValue = attributeEle.getAttribute(VALUE_ATTRIBUTE);
                if (StringUtils.hasLength(attributeName) && StringUtils.hasLength(attributeValue)) {
                    BeanMetadataAttribute attribute = new BeanMetadataAttribute(attributeName, attributeValue);
                    attribute.setSource(extractSource(attributeEle));
                    qualifier.addMetadataAttribute(attribute);
                }
                else {
                    error("Qualifier 'attribute' tag must have a 'name' and 'value'", attributeEle);
                    return;
                }
            }
        }
        bd.addQualifier(qualifier);
    }
    finally {
        this.parseState.pop();
    }
}
项目:class-guard    文件:InjectAnnotationBeanPostProcessorTests.java   
@Test
public void testObjectFactoryQualifierInjection() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryQualifierFieldInjectionBean.class));
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "testBean"));
    bf.registerBeanDefinition("testBean", bd);

    ObjectFactoryQualifierFieldInjectionBean bean = (ObjectFactoryQualifierFieldInjectionBean) bf.getBean("annotatedBean");
    assertSame(bf.getBean("testBean"), bean.getTestBean());
    bf.destroySingletons();
}
项目:lams    文件:QualifierAnnotationAutowireCandidateResolver.java   
/**
 * Match the given qualifier annotation against the candidate bean definition.
 */
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());
    if (qualifier == null) {
        qualifier = bd.getQualifier(ClassUtils.getShortName(type));
    }
    if (qualifier == null) {
        // First, check annotation on factory method, if applicable
        Annotation targetAnnotation = getFactoryMethodAnnotation(bd, type);
        if (targetAnnotation == null) {
            RootBeanDefinition dbd = getResolvedDecoratedDefinition(bd);
            if (dbd != null) {
                targetAnnotation = getFactoryMethodAnnotation(dbd, type);
            }
        }
        if (targetAnnotation == null) {
            // Look for matching annotation on the target class
            if (getBeanFactory() != null) {
                Class<?> beanType = getBeanFactory().getType(bdHolder.getBeanName());
                if (beanType != null) {
                    targetAnnotation = AnnotationUtils.getAnnotation(ClassUtils.getUserClass(beanType), type);
                }
            }
            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;
}
项目:holon-datastore-jpa    文件:JpaDatastoreRegistrar.java   
/**
 * Register a {@link JpaDatastore} bean
 * @param registry BeanDefinitionRegistry
 * @param dataContextId Data context id
 * @param primaryMode Primary mode
 * @param entityManagerFactoryBeanName EntityManagerFactory bean name reference
 * @param transactional Whether to add transactional behaviour to transactional datastore methods
 * @param beanClassLoader Bean class loader
 * @return Registered Datastore bean name
 */
public static String registerDatastore(BeanDefinitionRegistry registry, String dataContextId,
        PrimaryMode primaryMode, String entityManagerFactoryBeanName, boolean transactional, boolean autoFlush,
        ClassLoader beanClassLoader) {

    boolean primary = PrimaryMode.TRUE == primaryMode;
    if (!primary && PrimaryMode.AUTO == primaryMode) {
        if (registry.containsBeanDefinition(entityManagerFactoryBeanName)) {
            BeanDefinition bd = registry.getBeanDefinition(entityManagerFactoryBeanName);
            primary = bd.isPrimary();
        }
    }

    GenericDataContextBoundBeanDefinition definition = new GenericDataContextBoundBeanDefinition();
    definition.setDataContextId(dataContextId);

    Class<?> datastoreClass = transactional
            ? addTransactionalAnnotations(DefaultSpringJpaDatastore.class, dataContextId, beanClassLoader)
            : DefaultSpringJpaDatastore.class;

    definition.setBeanClass(datastoreClass);

    definition.setAutowireCandidate(true);
    definition.setPrimary(primary);
    definition.setDependsOn(entityManagerFactoryBeanName);

    if (dataContextId != null) {
        definition.addQualifier(new AutowireCandidateQualifier(Qualifier.class, dataContextId));
    }

    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("entityManagerFactory", new RuntimeBeanReference(entityManagerFactoryBeanName));
    pvs.add("autoFlush", autoFlush);
    definition.setPropertyValues(pvs);

    String beanName = BeanRegistryUtils.buildBeanName(dataContextId,
            EnableJpaDatastore.DEFAULT_DATASTORE_BEAN_NAME);

    registry.registerBeanDefinition(beanName, definition);

    StringBuilder log = new StringBuilder();
    if (dataContextId != null) {
        log.append("<Data context id: ");
        log.append(dataContextId);
        log.append("> ");
    }
    log.append("Registered JPA Datastore bean with name \"");
    log.append(beanName);
    log.append("\"");
    if (dataContextId != null) {
        log.append(" and qualifier \"");
        log.append(dataContextId);
        log.append("\"");
    }
    log.append(" bound to EntityManagerFactory bean: ");
    log.append(entityManagerFactoryBeanName);
    logger.info(log.toString());

    return beanName;

}
项目:holon-jdbc    文件:DataSourceRegistrar.java   
/**
 * Register a {@link PlatformTransactionManager} bound to given {@link DataSource} bean name
 * @param registry Bean definitions registry
 * @param dataSourceBeanName DataSource bean name
 * @param dataContextId Data context id
 * @param primaryMode Primary mode
 * @return Registered transaction manager bean name
 */
public static String registerDataSourceTransactionManager(BeanDefinitionRegistry registry,
        String dataSourceBeanName, String dataContextId, PrimaryMode primaryMode) {

    boolean primary = PrimaryMode.TRUE == primaryMode;
    if (PrimaryMode.AUTO == primaryMode && registry.containsBeanDefinition(dataSourceBeanName)) {
        BeanDefinition bd = registry.getBeanDefinition(dataSourceBeanName);
        if (bd.isPrimary()) {
            primary = true;
        }
    }

    GenericDataContextBoundBeanDefinition definition = new GenericDataContextBoundBeanDefinition();
    definition.setDataContextId(dataContextId);
    definition.setAutowireCandidate(true);
    definition.setPrimary(primary);
    definition.setBeanClass(DataSourceTransactionManager.class);

    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("dataSource", new RuntimeBeanReference(dataSourceBeanName));
    definition.setPropertyValues(pvs);

    if (dataContextId != null) {
        definition.addQualifier(new AutowireCandidateQualifier(Qualifier.class, dataContextId));
    }

    String tmBeanName = BeanRegistryUtils.buildBeanName(dataContextId,
            EnableDataSource.DEFAULT_TRANSACTIONMANAGER_BEAN_NAME);

    registry.registerBeanDefinition(tmBeanName, definition);

    StringBuilder log = new StringBuilder();
    if (dataContextId != null) {
        log.append("<Data context id: ");
        log.append(dataContextId);
        log.append("> ");
    }
    log.append("Registered DataSourceTransactionManager bean with name \"");
    log.append(tmBeanName);
    log.append("\"");
    if (dataContextId != null) {
        log.append(" and qualifier \"");
        log.append(dataContextId);
        log.append("\"");
    }
    LOGGER.info(log.toString());

    return tmBeanName;
}
项目:spring4-understanding    文件:QualifierAnnotationAutowireCandidateResolver.java   
/**
 * Match the given qualifier annotation against the candidate bean definition.
 */
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());
    if (qualifier == null) {
        qualifier = bd.getQualifier(ClassUtils.getShortName(type));
    }
    if (qualifier == null) {
        // First, check annotation on factory method, if applicable
        Annotation targetAnnotation = getFactoryMethodAnnotation(bd, type);
        if (targetAnnotation == null) {
            RootBeanDefinition dbd = getResolvedDecoratedDefinition(bd);
            if (dbd != null) {
                targetAnnotation = getFactoryMethodAnnotation(dbd, type);
            }
        }
        if (targetAnnotation == null) {
            // 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;
}
项目:put-it-to-rest    文件:Registry.java   
public <T> String register(final String id, final Class<T> type,
        final Supplier<BeanDefinitionBuilder> factory) {

    final String name = generateBeanName(id, type);

    if (isRegistered(name)) {
        LOG.debug("Bean [{}] is already registered, skipping it.", name);
        return name;
    }

    final AbstractBeanDefinition definition = factory.get().getBeanDefinition();

    definition.addQualifier(new AutowireCandidateQualifier(RestClient.class, id));
    definition.addQualifier(new AutowireCandidateQualifier(Qualifier.class, id));

    registry.registerBeanDefinition(name, definition);

    return name;
}
项目:spring    文件:QualifierAnnotationAutowireCandidateResolver.java   
/**
 * Match the given qualifier annotation against the candidate bean definition.
 */
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());
    if (qualifier == null) {
        qualifier = bd.getQualifier(ClassUtils.getShortName(type));
    }
    if (qualifier == null) {
        // First, check annotation on factory method, if applicable
        Annotation targetAnnotation = getFactoryMethodAnnotation(bd, type);
        if (targetAnnotation == null) {
            RootBeanDefinition dbd = getResolvedDecoratedDefinition(bd);
            if (dbd != null) {
                targetAnnotation = getFactoryMethodAnnotation(dbd, type);
            }
        }
        if (targetAnnotation == null) {
            // 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;
}
项目:riptide    文件:Registry.java   
public <T> String registerIfAbsent(final String id, final Class<T> type,
        final Supplier<BeanDefinitionBuilder> factory) {

    final String name = generateBeanName(id, type);

    if (isRegistered(name)) {
        LOG.debug("Bean [{}] is already registered, skipping it.", name);
        return name;
    }

    final AbstractBeanDefinition definition = factory.get().getBeanDefinition();

    definition.addQualifier(new AutowireCandidateQualifier(Qualifier.class, id));

    registry.registerBeanDefinition(name, definition);

    return name;
}
项目:class-guard    文件:QualifierAnnotationAutowireCandidateResolver.java   
/**
 * Match the given qualifier annotation against the candidate bean definition.
 */
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());
    if (qualifier == null) {
        qualifier = bd.getQualifier(ClassUtils.getShortName(type));
    }
    if (qualifier == null) {
        // First, check annotation on factory method, if applicable
        Annotation targetAnnotation = getFactoryMethodAnnotation(bd, type);
        if (targetAnnotation == null) {
            RootBeanDefinition dbd = getResolvedDecoratedDefinition(bd);
            if (dbd != null) {
                targetAnnotation = getFactoryMethodAnnotation(dbd, type);
            }
        }
        if (targetAnnotation == null) {
            // Look for matching annotation on the target class
            if (this.beanFactory != null) {
                Class<?> beanType = this.beanFactory.getType(bdHolder.getBeanName());
                if (beanType != null) {
                    targetAnnotation = AnnotationUtils.getAnnotation(ClassUtils.getUserClass(beanType), type);
                }
            }
            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;
}