0

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

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

1 Answers1

1

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)
shazin
  • 21,379
  • 3
  • 54
  • 71
  • Ok but in practice what happens? I am setting a list with 2 references (refereces to the privateBankingCustomerPolicy and retailBankingCustomerPolicy) anto to another reference to an inner bean (com.acme.DefaultCustomerPolicy)? TNX – AndreaNobili Nov 21 '14 at 13:00
  • 1
    In practice a java.util.List implementation is created and ur policies specified are added to that list and finally that list is set to your method. – shazin Nov 21 '14 at 14:26