Newsletter |
Spring Dependency Injection With Set Collection Property
Let us see few points, if we have Set property in our program
- 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 } }
Our 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”) then spring framework executes, following code internally [ not be visible 🙂 ]
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.
Actually nothing mates, just like primitive value injections, just we used Collections here nothing more than that 🙂 , download this example and test.
Complete Example On Spring Setter Injection With Set Collection Property
files will be used..
- WelcomeBean.java
- ClientLogic.java
- spconfig.xml
Directory structure:
WelcomeBean.java
package java4s; import java.util.Set; public class WelcomeBean { private Set studentsData; public void setStudentsData(Set studentsData) { this.studentsData = studentsData; } public void show() { System.out.println(studentsData); } }
spconfig.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean id="id1" class="java4s.WelcomeBean"> <property name="studentsData" > <set> <value>java4s</value> <value>sun</value> <value>oracle</value> <value>sun</value> <value>sun</value> </set> </property> </bean> </beans>
ClientLogic.java
package java4s; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class ClientLogic { public static void main(String[] args) { Resource res = new ClassPathResource("spconfig.xml"); BeanFactory factory = new XmlBeanFactory(res); Object o = factory.getBean("id1"); WelcomeBean wb = (WelcomeBean)o; wb.show(); } }
Note: Set wont allows duplicates, though you give duplicate values in spconfig.xml it will shows values only once in the output try this 😉
OutPut
You Might Also Like
::. About the Author .:: | ||
Very interesting info!Perfect just what I was looking for!
Why the set method for “TestBean” not called ?
@Abhishek
I have given that as an example to show the way of setting beans also in setters. However i have not used TestBean in working example 🙂
<set>
<value>sun</value>
<value>Oracle</value>
<value>java4s</value>
<ref bean="id2">
</set>
Here why <ref bean="id2"> is written under <set>. It should be written in <property> tag, isn't it?
Could you please explain?
salute to this sir
Good work i can understand its very easily…
Nice tutorial… How spring will assess the data type of variables in the Set Property?
How can i create Client Application by using Spring3.0
Great site.. Spring becomes easy to understand. thnks Sir..for excellent work.
Seems that it always create a HashSet Object,is their any way to create TreeSet Object?
Thanks,
sir, your explaination is understandable. kindly provide more information about springs and hibernate.
Great Tutorial for beginners like me.what is the use of TestBean in this example. little bit confusing
Hi Abhishek,
TestBean is the main class to process our requiremnt.i.e main method presents in this class.
if you have Still confusion,observe eclipse example given by SivaTeja sir.
Thanks,
jai
Hi,
I am trying this example and getting the error.Invalid ‘studentsData property’.
It's great site to get easily understand Spring concept..Thx Very much to ur dedication.
Dear
How to download examples? (i mean the program code)
and from where???!!
Thanks in advance …
You can download by clicking on download button at bottom right to the article.
sorry to say….actually i think it is internally creating LinkedHashset object not Hashset object…..and in map it is internally creating LinkedHashmap object….