2013-06-27 29 views
5

目前,我們正在使用Spring框架,並使用下面的XML: -ConversionNotSupportedException使用RuntimeBeanRefrence時對象的列表

<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" /> 

,其中X類和Y類擴展B類

類A有二傳手作爲如下: -

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

現在,我要消除上述XML,我編程設定豆如下: -

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

有了上面的代碼,我收到以下錯誤: -

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 

誰能給我如何使它正常工作的指針?

+0

好像您設置了'RuntimeBeanReference'的實例,您應該在其中設置'B'的實例。 – zeroflagL

+0

@DexterMorgan,你做到了嗎? – whiskeysierra

回答

0

在看了Spring的BeanDefinitionValueResolver實現後,可以看到傳統的普通List是不夠的。您需要使用ManagedList

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