Announcement Announcement Module
Collapse
No announcement yet.
Set of bean references as a constructor argument with a BeanDefinitionBuilder Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Set of bean references as a constructor argument with a BeanDefinitionBuilder

    I have been trying to create a bean definition programmatically with a BeanDefinitionBuilder that will have a constructor argument that is a set of bean references but I keep getting the following error. Any idea on how to get this working?

    If I use the equivalent XML bean definitions and load that context then it works fine.



    Here is the code:

    Code:
            
    
    Set<String> brokerBeanNames = new HashSet<String>();
      . . . 
    BeanDefinitionBuilder brokerFactoryBrokerSetBeanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(SetFactoryBean.class).addPropertyValue("sourceSet", brokerBeanNames);                    
              
    registerBeanDefinition(brokerFactoryBrokerSetBeanDefinitionBuilder, brokerFactoryBrokerSetBeanName);
      
    BeanDefinitionBuilder brokerFactoryBeanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(BrokerFactoryImpl.class)
                                                                                               .addConstructorArgReference(brokerFactoryBrokerSetBeanName);
              
    registerBeanDefinition(brokerFactoryBeanDefinitionBuilder, BROKER_FACTORY_BEAN_NAME);


    Here is an example of the XML that I am looking to generate.

    Code:
    <bean id="ServiceBus_BrokerFactory" class="com.bus.impl.BrokerFactoryImpl">
       <constructor-arg name="brokers">
           <set>
               <ref bean="ServiceBusBroker_defaultBroker"/>
           </set>
       </constructor-arg>
    </bean>
    
    <bean id="ServiceBusBroker_defaultBroker" class="com.bus.impl.RabbitMQBroker"/>

    Here is the error I am receiving.

    Caused by: org.springframework.beans.factory.UnsatisfiedDepen dencyException: Error creating bean with name 'ServiceBusBrokerFactory': Unsatisfied dependency expressed through constructor argument with index 0 of type [java.util.Set]: Could not convert constructor argument value of type [java.util.LinkedHashSet] to required type [java.util.Set]: Failed to convert value of type 'java.util.LinkedHashSet' to required type 'java.util.Set'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.bus.Broker]: no matching editors or conversion strategy found
    at org.springframework.beans.factory.support.Construc torResolver.createArgumentArray(ConstructorResolve r.java:702)
    at org.springframework.beans.factory.support.Construc torResolver.autowireConstructor(ConstructorResolve r.java:196)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireConstructor(Abs tractAutowireCapableBeanFactory.java:1035)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBeanInstance(Abst ractAutowireCapableBeanFactory.java:939)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:485)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 94)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:225)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:291 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveReference(BeanDefinitio nValueResolver.java:323)
    ... 55 more
    Last edited by MichaelBeausoleil; Jun 2nd, 2014, 06:25 AM.

  • #2
    This question is probably a better candidate for StackOverflow, perhaps with the "spring" tag.

    Could you post your question there?
    The forums are moving to stackoverflow, a page explaining all this will be available there in a few days: http://spring.io/questions

    Comment

    Working...
    X