Sometimes you will use dependency injection to inject strings and values, other times to insert other objects. In the latter case use xml configuration in one of the following two ways.
1) Reference to an other bean:
<beans> <bean id="zoo" class="org.davis.Zoo"> <property name="animal1" ref="leon"/> </bean> <bean id="leon" class="org.davis.Leon"> <property name="name" value="Clio"/> </bean> </beans>
ref="leon" is used to refer to an other bean definition.
name="animal1" defines the name of the member object.
2) An inner bean:
<beans> <bean id="zoo" class="org.davis.Zoo"> <property name="animal1"> <bean class="org.davis.Leon"> <property name="name" value="Clio"/> </bean> </property> </bean> </beans> * * *
If you intend to inject an entire collection of beans, here is the way to do it:
<beans> <bean id="zoo" class="org.davis.Zoo"> <property name="animals"> <list> <ref bean="leon"/> <ref bean="tiger"/> </list> </property> </bean> <bean id="leon" class="org.davis.Leon"> <property name="name" value="Clio"/> </bean> <bean id="tiger" class="org.davis.Tiger"> <property name="name" value="Miau"/> </bean> </beans>
Copyright © 2013 Welcome to the website of Davis Fiore. All Rights Reserved.