URI:
   DIR Return Create A Forum - Home
       ---------------------------------------------------------
       Java
  HTML https://java.createaforum.com
       ---------------------------------------------------------
       *****************************************************
   DIR Return to: General Discussion
       *****************************************************
       #Post#: 5--------------------------------------------------
       Dependencies in the form of Collections
       By: shekar Date: September 3, 2014, 8:40 am
       ---------------------------------------------------------
       While creating a Spring bean (Pojo) , the bean class can use any
       one of the following four types of collections as a dependency.
       1. Set - <set>
       2. List - <list>
       3. Map - <map>
       4. Properties - <props>
       Except the above four types of collections, if the Spring bean
       class uses any other type of collection as a dependency then the
       Spring container does not inject that collection object to the
       Spring bean.
       In this case Spring programmer is responsible for injecting the
       collection object manually.
       Dependency Injection With Set Collection Property:
       If in our spring bean has a property of type Set then in the
       spring config xml file, we need to use <set> element to inform
       the Spring IOC container regarding that Set property
       In spring config xml, we can use <value /> and <ref /> tags as
       sub elements of <set> tag
       While configuring <set> in xml file it doesnt allow duplicate
       values, because Set collection is a unique collection
       In spring framework if one bean is collaborating[depends on]
       with other bean class then  spring ioc container first creates
       collaborator bean object after only dependent bean object
       Example:
       public class SampleBean
       {
       private Set studentsData;
       private TestBean tb;
       
       public void setStudentsData(Set studentsData)
       {
       this.studentsData = studentsData;
       }
       
       public void setTb(TestBean tb)
       {
       this.tb = tb;
       }
       
       public void m1()
       {
       // Some Logic
       }
       
       }
       XML:
       <beans>
       
       <bean id="id1" class="SampleBean">
       <property name="studentsData">
       <set>
       <value>sun</value>
       <value>Oracle</value>
       <value>java4s</value>
       <ref bean="id2">
       </set>
       </property>
       </bean>
       
       <bean id="id2" class="TestBean"/>
       
       </beans>
       Explanation:
       Now in our client when we call getBean(“id1&#8243;) then spring
       framework executes, following code internally
       TestBean tb = new TestBean();
       Set s = new HastSet();
       s.add("sun");
       s.add("oracle");
       s.add("java4s");
       SampleBean ob =new SampleBean();
       ob.setStudentsData(s);
       First spring ioc creates TestBean class object as SampleBean
       depends on TestBean, then creates the Set object and will add
       the data into the set object and finally our SampleBean object
       will be created and set object will be injected.
       Set Util:
       <util:set id="messageUtilSet">
       <ref bean="stringMessage01"/>
       <ref bean="stringMessage02"/>
       <value>Spring is fun to learn.</value>
       </util:set>
       <util:set id="messageUtilTreeSet"
       set-class="java.util.TreeSet">
       <ref bean="stringMessage01"/>
       <ref bean="stringMessage02"/>
       <value>Spring is fun to learn.</value>
       </util:set>
       Spring Dependency Injection With List Collection Property:
       The list element will create a list that can be injected into a
       constructor or property that takes a java.util.List.
       Example:
       <bean id="stringMessageList"
       
       class="org.springbyexample.springindepth.chapter03.collections.M
       essageContainer">
       <property name="messageList">
       <list>
       <ref local="stringMessage01"/>
       <ref local="stringMessage02"/>
       <value>Spring is fun to learn.</value>
       </list>
       </property>
       </bean>
       <bean id="messageList"
       
       class="org.springbyexample.springindepth.chapter03.collections.M
       essageContainer">
       <property name="messageList">
       <list>
       <ref local="message01"/>
       <ref local="message02"/>
       <bean
       class="org.springbyexample.springindepth.bean.Message">
       <property name="message" value="Spring is fun to
       learn." />
       </bean>
       </list>
       </property>
       </bean>
       
       In the above example:
       The first bean definition shows a List of Strings being set. The
       second bean definition shows a List of Messages using references
       and an inner bean definition to create the list.
       List Factory:
       <!-- String message factory list. -->
       <bean id="messageFactoryList"
       
       class="org.springframework.beans.factory.config.ListFactoryBean"
       >
       <property name="sourceList">
       <list>
       <ref bean="stringMessage01"/>
       <ref bean="stringMessage02"/>
       <value>Spring is fun to learn.</value>
       </list>
       </property>
       </bean>
       
       In the above example:
       This example uses a FactoryBean to create a List directly as the
       bean instead of setting it as a property on another bean. This
       way a bean can be created and inserted into multiple bean
       definitions.
       List Util:
       custom namespace utility is for creating lists.
       <util:list id="messageUtilList">
       <ref bean="stringMessage01"/>
       <ref bean="stringMessage02"/>
       <value>Spring is fun to learn.</value>
       </util:list>
       <util:list id="messageUtilLinkedList"
       list-class="java.util.LinkedList">
       <ref bean="stringMessage01"/>
       <ref bean="stringMessage02"/>
       <value>Spring is fun to learn.</value>
       </util:list>
       Spring Dependency Injection With Map Collection Property:
       Map will stores the data in key, value base that to key, value
       must be the objects of any type.
       One [ key, value ] is called one pair or one entry.
       Map.Entry will gives one entry
       Map is the interface, Entry is the static class in Map
       interface, so we can call Map.Entry just remember this concept
       as of now
       Actually In this spring we always use Map.Entry class object
       only
       xml:
       <map>
       <entry key="k1">
       <value>Oracle</value>
       </entry>
       <entry key="k2">
       <value>Sun</value>
       </entry>
       <entry key="k3">
       <value>Java4s</value>
       </entry>
       <entry key="k4" value-ref="id2" />
       </map>
       Map Util:
       <util:map id="messageUtilMap">
       <entry>
       <key>
       <value>1</value>
       </key>
       <ref bean="stringMessage01"/>
       </entry>
       <entry key="2" value-ref="stringMessage02" />
       <entry key-ref="key03" value="Spring is fun to learn." />
       </util:map>
       <util:map id="messageUtilTreeMap"
       map-class="java.util.TreeMap">
       <entry>
       <key>
       <value>1</value>
       </key>
       <ref bean="stringMessage01"/>
       </entry>
       <entry key="2" value-ref="stringMessage02" />
       <entry key-ref="key03" value="Spring is fun to learn." />
       </util:map>
       *****************************************************