Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConversionNotSupportedException when using RuntimeBeanRefrence for a list of objects

Tags:

java

spring

We are currently using spring framework and are using following XML :-

<bean id="A" class="com.foo.baar.A" >
    <property name="attributes">
        <set value-type="com.foo.bar.B">
            <ref bean="X" />
            <ref bean="Y" />
        </set>
    </property>
</bean>

<bean id="X" class="com.foo.bar.X" />
<bean id="Y" class="com.foo.bar.Y" />

where class X and class Y extend class B

class A has setter as follows :-

public void setAttributes(List<B> attributes) {
    this.attributes = attributes;
}

Now, I have to eliminate the above XML and I am setting the beans programatically as following :-

List<Object> beanRefrences = new ArrayList<Object>();
for(String attribute : attributes) {
    Object beanReference = new RuntimeBeanReference(attribute);
    beanRefrences.add(beanReference);
}
mutablePropertyValues.add(propertyName, beanRefrences);

With above code, I am getting following error :-

nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.ArrayList' to required type 'java.util.List' for property 'attributes'; 
nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.beans.factory.config.RuntimeBeanReference] to required type [com.foo.bar.B] for property 'attributes[0]': no matching editors or conversion strategy found 

Can anyone give me pointers on how to make it work correctly?

like image 825
DexterMorgan Avatar asked Nov 12 '22 02:11

DexterMorgan


1 Answers

After taking a look at Spring's implementation of BeanDefinitionValueResolver one can see that a traditional, ordinary List is not sufficient. You need to use a ManagedList:

List<Object> beanRefrences = new ManagedList<>();
for(String attribute : attributes) {
    Object beanReference = new RuntimeBeanReference(attribute);
    beanRefrences.add(beanReference);
}
mutablePropertyValues.add(propertyName, beanRefrences);
like image 70
whiskeysierra Avatar answered Nov 14 '22 21:11

whiskeysierra