ktm5124
ktm5124

Reputation: 12123

Spring XML list of beans

I'd like to do something like

<bean id="xxx" class="yyy">
    <property name="list">
        <list>
            <ref bean="bean1" />
            <ref bean="bean2" />
        </list>
    </property>
</bean>

The beans "bean1" and "bean2" are two different implementations of an interface I. However, they belong to different classes. Does anyone know how I can do this? I have no problem constructing a list of Strings and a list of Integers. Why can't I construct a list of beans?

Thanks,
ktm

Upvotes: 1

Views: 10330

Answers (1)

Stephen C
Stephen C

Reputation: 719561

The beans "bean1" and "bean2" are two different implementations of an interface I. However, they belong to different classes. Does anyone know how I can do this?

It should work. Try it.

(There should be no runtime type problem here because the runtime type of the list is the raw type of the implementation class used.)

I have no problem constructing a list of Strings and a list of Integers. Why can't I construct a list of beans?

I think you are assuming that it won't work without trying it.

Upvotes: 5

Related Questions