gpt4 book ai didi

java - 将 RuntimeBeanRefrence 用于对象列表时出现 ConversionNotSupportedException

转载 作者:搜寻专家 更新时间:2023-11-01 00:57:08 24 4
gpt4 key购买 nike

我们目前正在使用 spring 框架并使用以下 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" />

其中 X 类和 Y 类扩展了 B 类

A 类有 setter 如下:-

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

现在,我必须删除上面的 XML,并且我正在以编程方式设置 bean,如下所示:-

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

任何人都可以指导我如何使其正常工作吗?

最佳答案

在查看了 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);

关于java - 将 RuntimeBeanRefrence 用于对象列表时出现 ConversionNotSupportedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17345053/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com