Question

I'm trying to dynamically register some spring beans in my ApplicationContext by using the ApplicationContextAware interface. I'm using the BeanDefitionBuilder to build the bean definitions and I register them with DefaultListableBeanFactory.registerBeanDefinition(). The bean I'm now trying to build would look like this in XML:

<bean id="compositeBean" class="SomeClass">
<property name="checks">
     <list>
         <ref bean="bean1"/>
         <ref bean="bean2"/>
         <ref bean="bean3"/>
     </list>
</property>
</bean>

I have a list of BeanDefinitions for (bean1, bean2, bean3) above available. When I try to use

BeanDefinitionBuilder.genericBeanDefinition(SomeClass.class)
                .addPropertyValue("checks", checks);

I end up with the error

 org.springframework.beans.factory.BeanCreationException: Error
 creating bean with name 'compositeBean': Initialization of bean
 failed; nested exception is
 org.springframework.beans.ConversionNotSupportedException: Failed to
 convert property value of type
 'org.springframework.beans.factory.config.BeanDefinition[]' to
 required type 'SomeClass[]' for property 'checks'; nested exception is
 java.lang.IllegalStateException: Cannot convert value of type
 [org.springframework.beans.factory.support.GenericBeanDefinition] to
 required type [SomeClass] for property 'checks[0]': no matching
 editors or conversion strategy found

How can programmatically add the list of bean references to my compositeBean?

Was it helpful?

Solution

Take a look at org.springframework.beans.factory.support.ManagedList

That's what <util:list/> uses to compile a list. Create a ManagedList as your variable "checks" and set that object as the property value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top