Get all updates of Java4s
Follow Us: Email Newsletter

Spring Dependency Injection With Set Collection Property

Spring » On Aug 9, 2011 { 13 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 Author ♦

Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.


13 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 :-)

  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?

  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.


  13. Naveen Gupta says:

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


By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

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