gpt4 book ai didi

spring-mvc - 动态添加 bean 引用列表到 bean 定义

转载 作者:行者123 更新时间:2023-12-03 23:11:33 25 4
gpt4 key购买 nike

我正在尝试使用 ApplicationContextAware 接口(interface)在我的 ApplicationContext 中动态注册一些 spring bean。我正在使用 BeanDefitionBuilder 构建 bean 定义,并使用 DefaultListableBeanFactory.registerBeanDefinition() 注册它们。我现在尝试构建的 bean 在 XML 中看起来像这样:

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

我有一个可用的 (bean1, bean2, bean3) 的 BeanDefinitions 列表。当我尝试使用
BeanDefinitionBuilder.genericBeanDefinition(SomeClass.class)
.addPropertyValue("checks", checks);

我最终得到了错误
 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

如何以编程方式将 bean 引用列表添加到我的compositeBean?

最佳答案

看看 org.springframework.beans.factory.support.ManagedList

就是这样 <util:list/>用于编译列表。创建一个 ManagedList 作为变量“检查”并将该对象设置为属性值。

关于spring-mvc - 动态添加 bean 引用列表到 bean 定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18145474/

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