Monday, December 28, 2015

Spring - Injecting Collection

Listing 5: SpringBean.java


<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
        <bean id="CustomerBean" class="com.collection.core.CustomerBean">
 
               <!-- java.util.List -->
               <property name="lists">
                       <list>
                               <value>1</value>
                               <ref bean="PersonBean" />
                               <bean class="com.collection.core.PersonBean">
                                      <property name="name" value="UmeshList" />
                                      <property name="address" value="address" />
                                      <property name="age" value="28" />
                               </bean>
                       </list>
               </property>
 
               <!-- java.util.Set -->
               <property name="sets">
                       <set>
                               <value>1</value>
                               <ref bean="PersonBean" />
                               <bean class="com.collection.core.PersonBean">
                                      <property name="name" value="UmeshSet" />
                                      <property name="address" value="address" />
                                      <property name="age" value="28" />
                               </bean>
                       </set>
               </property>
 
               <!-- java.util.Map -->
               <property name="maps">
                       <map>
                               <entry key="Key 1" value="1" />
                               <entry key="Key 2" value-ref="PersonBean" />
                               <entry key="Key 3">
                                      <bean class="com.collection.core.PersonBean">
                                              <property name="name" value="UmeshMap" />
                                              <property name="address" value="address" />
                                              <property name="age" value="28" />
                                      </bean>
                               </entry>
                       </map>
               </property>
 
               <!-- java.util.Properties -->
               <property name="pros">
                       <props>
                               <prop key="admin">admin@nospam.com</prop>
                               <prop key="support">support@nospam.com</prop>
                       </props>
               </property>
 
        </bean>
 
        <bean id="PersonBean" class="com.collection.core.PersonBean">
               <property name="name" value="Umesh" />
               <property name="address" value="address 1" />
               <property name="age" value="28" />
        </bean>
 
</beans>

The above listing defines SpringBean.xml file to configure collection injection for CustomerBean class using PersonBean class. It defines four PersonBean type beans for four different type of collection tags <list>, <map>, <set> and <props>.

No comments:

Post a Comment