【问题标题】:Dynamically adding a list of bean references to bean definition动态添加 bean 引用列表到 bean 定义
【发布时间】:2013-08-09 11:16:33
【问题描述】:

我正在尝试使用 ApplicationContextAware 接口在我的 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 引用列表添加到我的复合Bean?

【问题讨论】:

    标签: spring-mvc applicationcontext spring-bean


    【解决方案1】:

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

    这就是&lt;util:list/&gt; 用来编译列表的内容。创建一个 ManagedList 作为变量“检查”并将该对象设置为属性值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-05
      • 1970-01-01
      相关资源
      最近更新 更多