Spring Dependency Injection With Set Collection Property

Spring » on Aug 9, 2011 { 17 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




Now in our client when we call getBean(“id1″) then spring framework executes, following code internally [ not be visible :-) ]

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

  • spconfig.xml

Directory structure:


Note:  Set wont allows duplicates, though you give duplicate values in spconfig.xml it will shows values only once in the output try this ;)



You Might Also Like

  ::. About the Author .::

- Full Stack Java/J2EE & UI Web 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.


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


    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:

      <ref bean="id2">

      Here why <ref bean="id2"> is written under <set>. It should be written in <property> tag, isn't it?
      Could you please explain?

  4. Ashok says:

    Good work i can understand its very easily…

  5. surya says:

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

  6. Praveen says:

    How can i create Client Application by using Spring3.0

  7. Ashu Ranjan says:

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

  8. Vishal Bhamawat says:

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

  9. anilkumar says:

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

  10. annapurna says:

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

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


  12. Naveen Gupta says:

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

  13. saleem says:

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

  14. MeMe says:


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

    Thanks in advance …


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

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