Package org.springframework.beans.factory.support

Examples of org.springframework.beans.factory.support.AutowireCandidateQualifier


  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) {
      Annotation targetAnnotation = null;
      if (bd.getResolvedFactoryMethod() != null) {
        targetAnnotation = bd.getResolvedFactoryMethod().getAnnotation(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 = ClassUtils.getUserClass(beanType).getAnnotation(type);
          }
        }
        if (targetAnnotation == null && bd.hasBeanClass()) {
          targetAnnotation = ClassUtils.getUserClass(bd.getBeanClass()).getAnnotation(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);
      }
View Full Code Here


      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 (node instanceof Element && DomUtils.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;
          }
View Full Code Here

      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 (node instanceof Element && 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;
          }
        }
View Full Code Here

      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 (node instanceof Element && 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;
          }
View Full Code Here

      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 (node instanceof Element && DomUtils.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;
          }
View Full Code Here

    SimpleTypeConverter typeConverter = new SimpleTypeConverter();
    Annotation[] annotations = (Annotation[]) descriptor.getAnnotations();
    for (Annotation annotation : annotations) {
      Class<? extends Annotation> type = annotation.annotationType();
      if (isQualifier(type)) {
        AutowireCandidateQualifier qualifier = bd.getQualifier(type.getName());
        if (qualifier == null) {
          qualifier = bd.getQualifier(ClassUtils.getShortName(type));
        }
        if (qualifier == null && bd.hasBeanClass()) {
          // look for matching annotation on the target class
          Class<?> beanClass = bd.getBeanClass();
          Annotation targetAnnotation = beanClass.getAnnotation(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);
          }
View Full Code Here

            def.setFactoryMethodName("getInstance");
            def.setLazyInit(true);
            def.setTargetType(adapter.getComponentImplementation());
            if (key instanceof String)
            {
               def.addQualifier(new AutowireCandidateQualifier(Named.class, key));
            }
            else if (key instanceof Class<?> && ((Class<?>)key).isAnnotation())
            {
               def.addQualifier(new AutowireCandidateQualifier((Class<?>)key));
            }
            else
            {
               def.setPrimary(true);
            }
View Full Code Here

      builder.setFactoryMethod("createSharedEntityManager");
      builder.addConstructorArgReference(definitions.beanName);

      AbstractBeanDefinition emBeanDefinition = builder.getRawBeanDefinition();

      emBeanDefinition.addQualifier(new AutowireCandidateQualifier(Qualifier.class, definitions.beanName));
      emBeanDefinition.setScope(definitions.beanDefinition.getScope());
      emBeanDefinition.setSource(definitions.beanDefinition.getSource());

      BeanDefinitionReaderUtils.registerWithGeneratedName(emBeanDefinition,
          (BeanDefinitionRegistry) definitions.beanFactory);
View Full Code Here

    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());
View Full Code Here

    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();
View Full Code Here

TOP

Related Classes of org.springframework.beans.factory.support.AutowireCandidateQualifier

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.