Newsletter

Spring Dependency Injection With List Collection Property

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

public class SampleBean
{
   private List 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">
            <list>
               <value>sun</value>
               <value>Oracle</value>
               <value>java4s</value>
               <value>sun</value>
               <ref bean="id2">
            </list>
      </property>
</bean>

<bean id="id2" class="TestBean"/>

</beans>

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

  • WelcomeBean.java
  • ClientLogic.java
  • spconfig.xml

Directory structure:

 

WelcomeBean.java

package java4s;
import java.util.List;
public class WelcomeBean {

       private List studentsData;      
    

    public void setStudentsData(List 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" >
      <list>
         <value>java4s</value>
         <value>sun</value>
         <value>oracle</value>
         <value>java4s</value>
      </list>
     </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();
        
    }

}

 

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

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

    Hi

    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:

    Hi…
    Good tutorial for spring beginner…Thanks for the post

  3. Hemalatha says:

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

  4. Rahul says:

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

  5. Shivaraj says:

    Superb…………

  6. Sandeep says:

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

    <bean id="id1" class="SampleBean">
    <property name="studentsData">
    <list>
    <value>sun</value>
    <value>Oracle</value>
    <value>java4s</value>
    <value>sun</value>
    <ref bean="id2">
    </list>
    </property>
    </bean>
    <bean id="id2" class="TestBean"/>
    </beans>

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

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.