Newsletter

Spring Dependency Injection With Set Collection Property

Spring » on Aug 9, 2011 { 18 Comments } By Sivateja

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 .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

18 Responses to “Spring Dependency Injection With Set Collection Property”
  1. Rod Marinaro says:

    Very interesting info!Perfect just what I was looking for!

  2. Abhishek says:

    Why the set method for “TestBean” not called ?

  3. Java4s says:

    @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 🙂

    • Mahendra Singh says:

      <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?

  4. ashraf says:

    salute to this sir

  5. Ashok says:

    Good work i can understand its very easily…

  6. surya says:

    Nice tutorial… How spring will assess the data type of variables in the Set Property?

  7. Praveen says:

    How can i create Client Application by using Spring3.0

  8. Ashu Ranjan says:

    Great site.. Spring becomes easy to understand. thnks Sir..for excellent work.

  9. Vishal Bhamawat says:

    Seems that it always create a HashSet Object,is their any way to create TreeSet Object?
    Thanks,

  10. anilkumar says:

    sir, your explaination is understandable. kindly provide more information about springs and hibernate.

  11. annapurna says:

    Great Tutorial for beginners like me.what is the use of TestBean in this example. little bit confusing

  12. Jayaram says:

    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

  13. Naveen Gupta says:

    Hi,
    I am trying this example and getting the error.Invalid ‘studentsData property’.

  14. saleem says:

    It's great site to get easily understand Spring concept..Thx Very much to ur dedication.

  15. MeMe says:

    Dear

    How to download examples? (i mean the program code)
    and from where???!!

    Thanks in advance …

  16. soumya ranjan praharaj says:

    sorry to say….actually i think it is internally creating LinkedHashset object not Hashset object…..and in map it is internally creating LinkedHashmap object….

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.