Spring Dependency Injection With List Collection Property

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

This is same as <set />, right previous session, but few changes are there, let us see the difference between these two <set /> and <list />

  • In the previous example [ Dependency In The Form Of Set Collection Property ] just change to <list /> where ever we have <set />
  • Set doesn’t allows duplicate right.., but List will allows duplicate values too
  • Create List object like,  List l= new ArrayList()
  • Set cannot put values in order, but List will keep the added values in the same order

If you still need the syntax just see


Note: Mates see, in our xml i have been added sun two times, as List will allows the duplicate values, but set doesn’t.  That’s it mates, but Map is not like these two collection and little more important than these two, so we will see in depth regarding this Map in the next session.

Mates am not giving any example here as its same as setter injection ( Hello World program only, see how easy ;) ),  but you can download the example and test at your locals.

Complete Example On Spring Setter Injection With List Collection

files will be used..

  • spconfig.xml

Directory structure:







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.


7 Responses to “Spring Dependency Injection With List Collection Property”
  1. Antony says:


    Thanks for the post. Its really easy to understand.

    I have a doubt here. If in case I use list with generic type then how I inject the list through spring configuration.

    Eg: List studentList;

  2. Harikrishna says:

    Good tutorial for spring beginner…Thanks for the post

  3. Hemalatha says:

    Thanks for the tutorial………clear explanations….learnt a lot…once again tanq:)

  4. Rahul says:

    Nice post for list. Move from the list(in your code) .

  5. Sandeep says:

    Why is it necessary to pass the reference of TestBean within the <list></list> tag. Please clarify.

    <bean id="id1" class="SampleBean">
    <property name="studentsData">
    <ref bean="id2">
    <bean id="id2" class="TestBean"/>

  6. Ravi Kumar says:

    Hi Siva,
    It's good to learn from your website. It's so easy.
    Look, I want to point out that the setStudentsData() method in SampleBean class has parameter of type Set. I think it should be List.


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.