Search code examples
javaspringspring-mvcapplicationcontext

How to create a bean collection into an XML Java confifuration in Spring?


I am studying for the Spring Core certification and I have the following doubt related to the definition of a beans collection into an XML configuration.

For example I have this XML configuration snippet:

<bean id="service" class="com.acme.service.TransferServiceImpl">
    <property name="customerPolicies">
        <list>
            <ref bean="privateBankingCustomerPolicy"/>
            <ref bean="retailBankingCustomerPolicy"/>
            <bean class="com.acme.DefaultCustomerPolicy"/>
        </list>
    </property>
</bean>

Can you help me to understand how exactly work?

On the documentation I read that it is called the public void setCustomerPollicies(java.util.List policies)) {...} method. I think that it depend my the fact that have to be the collection initialized with the beans object into the list. Is it right?

My doubt is: why the object into the list are different type? (a ref to a privateBankingCustomerPolicy bean, a ref to a retailBankingCustomerPolicy bean and an inner bean having type as com.acme.DefaultCustomerPolicy)?

Tnx


Solution

  • If the signature is like this and no generics is used then any type can be added to this list.

    public void setCustomerPollicies(java.util.List policies)
    

    If you want type restriction at runtime then you must use generics

    public void setCustomerPollicies(java.util.List<Policy> policies)