Announcement Announcement Module
No announcement yet.
Set of bean references as a constructor argument with a BeanDefinitionBuilder Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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:

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

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

    <bean id="ServiceBus_BrokerFactory" class="com.bus.impl.BrokerFactoryImpl">
       <constructor-arg name="brokers">
               <ref bean="ServiceBusBroker_defaultBroker"/>
    <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 torResolver.createArgumentArray(ConstructorResolve
    at torResolver.autowireConstructor(ConstructorResolve
    at AutowireCapableBeanFactory.autowireConstructor(Abs
    at AutowireCapableBeanFactory.createBeanInstance(Abst
    at AutowireCapableBeanFactory.doCreateBean(AbstractAu
    at AutowireCapableBeanFactory.createBean(AbstractAuto
    at BeanFactory$1.getObject( 94)
    at ingletonBeanRegistry.getSingleton(DefaultSingleton
    at BeanFactory.doGetBean( )
    at BeanFactory.getBean(
    at nitionValueResolver.resolveReference(BeanDefinitio
    ... 55 more
    Last edited by MichaelBeausoleil; Jun 2nd, 2014, 05: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: