Results 1 to 5 of 5

Thread: No unique bean with custom namespace

  1. #1
    Join Date
    Jan 2012
    Posts
    3

    Question 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. #2
    Join Date
    Dec 2010
    Location
    Singapore
    Posts
    302

    Default

    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.
    Amila Domingo

  3. #3
    Join Date
    Jan 2012
    Posts
    3

    Default

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Seattle, Washington
    Posts
    467

    Default

    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.

  5. #5
    Join Date
    Jan 2012
    Posts
    3

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •