Setter Dependency Injection with List of custom Object

In some scenarios, we may need to inject a list of our own custom object to another object.

Consider the example where Person has permanent and current addresses.

In this case, we are required to inject list of Address to person object.

Let us see the same in spring through Setter Dependency Injection

Create a class called Person

It contains 3 attributes id,name and addresses where addresses is a list of Address type

It contains 3 setter methods one for each attribute.

It also contains showPersonAddresses() method to display the list of addresses that we have injected to the person object.

  1. package com.kb.di;
  2.  
  3. import java.util.List;
  4.  
  5. public class Person {
  6.     private int id;
  7.     private String name;
  8.     private List<Address> addresses;
  9.    
  10.     public void showPersonAddresses() {
  11.         for (Address address : addresses) {
  12.             System.out.println(address);
  13.         }
  14.     }
  15.  
  16.     public void setId(int id) {
  17.         this.id = id;
  18.     }
  19.  
  20.     public void setName(String name) {
  21.         this.name = name;
  22.     }
  23.  
  24.     public void setAddresses(List<Address> addresses) {
  25.         this.addresses = addresses;
  26.     }
  27. }
package com.kb.di;

import java.util.List;

public class Person {
	private int id;
	private String name;
	private List<Address> addresses;
	
	public void showPersonAddresses() {
		for (Address address : addresses) {
			System.out.println(address);
		}
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setAddresses(List<Address> addresses) {
		this.addresses = addresses;
	}
}

Create a class called Address

  1. package com.kb.di;
  2.  
  3. public class Address {
  4.  
  5.     private int houseNo;
  6.    
  7.     private String streetName;
  8.  
  9.     private String city;
  10.    
  11.     private String postalCode;
  12.  
  13.  
  14. @Override
  15.     public String toString() {
  16.         return "House No-> " + houseNo + " Street Name-> " + streetName + " City->" + city + " Postal Code->"
  17.                 + postalCode;
  18.     }
  19.  
  20.  
  21. public void setHouseNo(int houseNo) {
  22.     this.houseNo = houseNo;
  23. }
  24.  
  25.  
  26. public void setStreetName(String streetName) {
  27.     this.streetName = streetName;
  28. }
  29.  
  30.  
  31. public void setCity(String city) {
  32.     this.city = city;
  33. }
  34.  
  35.  
  36. public void setPostalCode(String postalCode) {
  37.     this.postalCode = postalCode;
  38. }
  39. }
package com.kb.di;

public class Address {

	private int houseNo;
	
	private String streetName;

	private String city;
	
	private String postalCode;


@Override
	public String toString() {
		return "House No-> " + houseNo + " Street Name-> " + streetName + " City->" + city + " Postal Code->"
				+ postalCode;
	}


public void setHouseNo(int houseNo) {
	this.houseNo = houseNo;
}


public void setStreetName(String streetName) {
	this.streetName = streetName;
}


public void setCity(String city) {
	this.city = city;
}


public void setPostalCode(String postalCode) {
	this.postalCode = postalCode;
}
}

Create a spring bean definition file

In this file, we used < property > tag with < list > to define the list of Address

< ref bean=”beanId”> tag is used to inject the bean of custom type which is Address in this case
Where beanId is the id of custom bean

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  5.        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
  6.  
  7.  
  8.     <bean id="person" class="com.kb.di.Person">
  9.         <property name="id" value="1"></property>
  10.         <property name="name" value="Raj"></property>
  11.         <property name="addresses">
  12.             <list>
  13.                 <ref bean="permamentAdd" />
  14.                 <ref bean="currentAdd" />
  15.             </list>
  16.         </property>
  17.  
  18.     </bean>
  19.  
  20.     <bean id="permamentAdd" class="com.kb.di.Address">
  21.         <property name="houseNo" value="33" />
  22.         <property name="streetName" value="M G Road" />
  23.         <property name="city" value="Bangalore" />
  24.         <property name="postalCode" value="560001" />
  25.     </bean>
  26.  
  27.     <bean id="currentAdd" class="com.kb.di.Address">
  28.         <property name="houseNo" value="44" />
  29.         <property name="streetName" value="Residency Road" />
  30.         <property name="city" value="Mumbai" />
  31.         <property name="postalCode" value="400051" />
  32.     </bean>
  33.  
  34. </beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">


	<bean id="person" class="com.kb.di.Person">
		<property name="id" value="1"></property>
		<property name="name" value="Raj"></property>
		<property name="addresses">
			<list>
				<ref bean="permamentAdd" />
				<ref bean="currentAdd" />
			</list>
		</property>

	</bean>

	<bean id="permamentAdd" class="com.kb.di.Address">
		<property name="houseNo" value="33" />
		<property name="streetName" value="M G Road" />
		<property name="city" value="Bangalore" />
		<property name="postalCode" value="560001" />
	</bean>

	<bean id="currentAdd" class="com.kb.di.Address">
		<property name="houseNo" value="44" />
		<property name="streetName" value="Residency Road" />
		<property name="city" value="Mumbai" />
		<property name="postalCode" value="400051" />
	</bean>

</beans>

Create a pom.xml file

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3.   <modelVersion>4.0.0</modelVersion>
  4.  
  5.   <groupId>Spring</groupId>
  6.   <artifactId>SpringCore</artifactId>
  7.   <version>0.0.1-SNAPSHOT</version>
  8.   <packaging>jar</packaging>
  9.  
  10.   <name>SpringCore</name>
  11.   <url>http://maven.apache.org</url>
  12.  
  13.   <properties>
  14.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15.   </properties>
  16.  
  17.   <dependencies>
  18.   <dependency>
  19.     <groupId>org.springframework</groupId>
  20.     <artifactId>spring-core</artifactId>
  21.     <version>4.2.4.RELEASE</version>
  22. </dependency>
  23.   <dependency>
  24.     <groupId>org.springframework</groupId>
  25.     <artifactId>spring-context</artifactId>
  26.     <version>4.2.4.RELEASE</version>
  27. </dependency>
  28.  
  29.     <dependency>
  30.       <groupId>junit</groupId>
  31.       <artifactId>junit</artifactId>
  32.       <version>3.8.1</version>
  33.       <scope>test</scope>
  34.     </dependency>
  35.   </dependencies>
  36. </project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>Spring</groupId>
  <artifactId>SpringCore</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SpringCore</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-core</artifactId>
	<version>4.2.4.RELEASE</version>
</dependency>
  <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>4.2.4.RELEASE</version>
</dependency>
  
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Create a SetterDI class

This class loads the spring beans using application context and calls the person class method to display the list of addresses.

  1. package com.kb.di;
  2. import org.springframework.beans.factory.BeanFactory;
  3. import org.springframework.beans.factory.xml.XmlBeanFactory;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. import org.springframework.core.io.ClassPathResource;
  7. import org.springframework.core.io.Resource;
  8.  
  9. public class SetterDI  {
  10.     public static void main(String[] args) {
  11.         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");  
  12.         Person person = (Person)applicationContext.getBean("person");
  13.         person.showPersonAddresses();
  14.    
  15.         }
  16. }
package com.kb.di;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class SetterDI  {
	public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");  
        Person person = (Person)applicationContext.getBean("person");
        person.showPersonAddresses();
    
        }
}

Run the SetterDI class and see the output as below

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on