Announcement Announcement Module
Collapse
No announcement yet.
No unique bean with custom namespace Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • No unique bean with custom namespace

    Hi

    I'm trying to create my own namespace for use in Spring.
    The XSD contains an element with two properties and includes the identifiedType type defined by Spring.
    I've created a NamespaceHandler and a BeanDefinitionParser, and all works fine...
    until I enter a second element in the Spring context of the same type.
    I then get a "no unique bean of type ..." exception.

    Does anyone have a hint as to what I might be missing?

    A snippet of the XSD:
    Code:
         <xsd:element  name="carrier">
                <xsd:complexType>
                      <xsd:complexContent>
                            <xsd:extension base="bean:identifiedType">
                                 <xsd:sequence>
                                       <xsd:element name="factory">
                                             <xsd:complexType>
                                                   <xsd:attribute name="jndi" type="xsd:string" />
                                                   <xsd:attribute name="ref" type="xsd:string" />
                                             </xsd:complexType>
                                       </xsd:element>
                                       <xsd:element name="destination">
                                             <xsd:complexType>
                                                   <xsd:attribute name="jndi" type="xsd:string" />
                                                   <xsd:attribute name="ref" type="xsd:string" />
                                             </xsd:complexType>
                                       </xsd:element>
                                 </xsd:sequence>
                            </xsd:extension>
                      </xsd:complexContent>
                </xsd:complexType>
          </xsd:element>
    A snippet of the BeanDefinitionParser:
    Code:
    public class JMSCarrierBeanDefinitionParser extends AbstractBeanDefinitionParser {
     
          public static final String FACTORY = "factory";
          public static final String DESTINATION = "destination";
          public static final String JNDI = "jndi";
          public static final String REF = "ref";
         
          @Override
          protected AbstractBeanDefinition parseInternal(Element element, ParserContext context) {
                BeanDefinitionBuilder root = BeanDefinitionBuilder.genericBeanDefinition(JMSCarrier.class);
    
                add(DomUtils.getChildElementByTagName(element, FACTORY), root);
                add(DomUtils.getChildElementByTagName(element, DESTINATION), root);
               
                return root.getBeanDefinition();
          }
         
          private void add(Element element, BeanDefinitionBuilder root) {
                final String jndi = element.getAttribute(JNDI);
                final String ref = element.getAttribute(REF);
                final boolean hasJNDI = (jndi != null) && (jndi.length() > 0);
                final boolean hasREF = (ref != null) && (ref.length() > 0);
               
                if (hasJNDI) {
                      BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(JndiObjectFactoryBean.class);
                      builder.addPropertyValue("jndiName", jndi);
                      root.addConstructorArgValue(builder.getBeanDefinition());
                } else if (hasREF) {
                      root.addConstructorArgReference(ref);
                }
          }
    }
    Snippet of Spring config:
    Code:
    <jms:carrier id="primary">
         <jms:factory jndi="some/address" />
         <jms:destination jndi="some/other/address" />
    </jms:carrier>

  • #2
    I think you are trying to auto-wire (by type) a property where there is more than one bean of that property type exits and context can not decide which one to take.

    Comment


    • #3
      That's what it look like for me too, but I'm unable to find information about how to support multiple top elements of the same type.

      Comment


      • #4
        I think you should provide more details about what you're doing, and the exact error message.

        If you have multiple beans of the same type, you can't autowire a property of that type by type.

        You could possibly use the @Resource annotation, which can specify the bean name.

        Comment


        • #5
          It looks like I was the fool that made the error.
          I was convinced the BeanDefinitionParser was to blame and forgot that the test wired the bean by type alone.
          Added the wanted bean id to the lookup and all was back to normal.

          Comment

          Working...
          X