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

  • No unique bean with custom namespace


    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:
         <xsd:element  name="carrier">
                            <xsd:extension base="bean:identifiedType">
                                       <xsd:element name="factory">
                                                   <xsd:attribute name="jndi" type="xsd:string" />
                                                   <xsd:attribute name="ref" type="xsd:string" />
                                       <xsd:element name="destination">
                                                   <xsd:attribute name="jndi" type="xsd:string" />
                                                   <xsd:attribute name="ref" type="xsd:string" />
    A snippet of the BeanDefinitionParser:
    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";
          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);
                } else if (hasREF) {
    Snippet of Spring config:
    <jms:carrier id="primary">
         <jms:factory jndi="some/address" />
         <jms:destination jndi="some/other/address" />

  • #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.


    • #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.


      • #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.


        • #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.