4

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 参照のリストを自分の compositeBean にプログラムで追加するにはどうすればよいですか?

4

1 に答える 1

4

org.springframework.beans.factory.support.ManagedList を見てください。

それが<util:list/>、リストをコンパイルするために使用するものです。変数の「チェック」として ManagedList を作成し、そのオブジェクトをプロパティ値として設定します。

于 2013-08-12T16:36:08.110 に答える