Constructor Dependency Injection with Array of Object Or Non string

In some scenarios, we may need to inject an array of custom object to an object.

Consider the example where Person has multiple Addresses

In this case, we can use Array of Address which stores the addresses

Let us see the same in spring through Constructor Dependency Injection

Create a class called Person

It contains 3 attributes id,name and addresses where addresses is an Array of Address.

It also contains showAddresses() method to display the addresses injected to the person object.

  1. package com.kb.di;
  2.  
  3. public class Person {
  4.     private int id;
  5.     private String name;
  6.     private Address[] addresses;
  7.  
  8.  
  9.  
  10.     public Person(int id, String name, Address[] addresses) {
  11.         this.id = id;
  12.         this.name = name;
  13.         this.addresses = addresses;
  14.     }
  15.  
  16.  
  17.  
  18.     public void showAddresses() {
  19.         int index=1;
  20.     for (Address address : addresses) {
  21.         System.out.println("Address "+index+":");
  22.         System.out.println(address);
  23.         index++;
  24.        
  25.     }
  26.  
  27.     }
  28.  
  29. }
package com.kb.di;

public class Person {
	private int id;
	private String name;
	private Address[] addresses;



	public Person(int id, String name, Address[] addresses) {
		this.id = id;
		this.name = name;
		this.addresses = addresses;
	}



	public void showAddresses() {
		int index=1;
	for (Address address : addresses) {
		System.out.println("Address "+index+":");
		System.out.println(address);
		index++;
		
	}

	}

}

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.     public Address(int houseNo, String streetName, String city, String postalCode) {
  14.         super();
  15.         this.houseNo = houseNo;
  16.         this.streetName = streetName;
  17.         this.city = city;
  18.         this.postalCode = postalCode;
  19.     }
  20. @Override
  21.     public String toString() {
  22.         return "House No-> " + houseNo + " Street Name-> " + streetName + " City->" + city + " Postal Code->"
  23.                 + postalCode;
  24.     }
  25. }
package com.kb.di;

public class Address {

	private int houseNo;
	
	private String streetName;

	private String city;
	
	private String postalCode;

	public Address(int houseNo, String streetName, String city, String postalCode) {
		super();
		this.houseNo = houseNo;
		this.streetName = streetName;
		this.city = city;
		this.postalCode = postalCode;
	}
@Override
	public String toString() {
		return "House No-> " + houseNo + " Street Name-> " + streetName + " City->" + city + " Postal Code->"
				+ postalCode;
	}
}


Create a spring bean definition file

In this file, we used < constructor-arg > tag with < array > to define the addresses
< array > tag is used to inject the addresses defined as array of Address in the Person class.
< ref > tag is used to inject our custom address bean defined in the same file.

  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.     <bean id="person" class="com.kb.di.Person">
  8.         <constructor-arg value="1" type="int" />
  9.         <constructor-arg value="Raj" />
  10.         <constructor-arg>
  11.             <array>
  12.                 <ref bean="add1" />
  13.                 <ref bean="add2" />
  14.             </array>
  15.         </constructor-arg>
  16.     </bean>
  17.     <bean id="add1" class="com.kb.di.Address">
  18.         <constructor-arg value="33" type="int" />
  19.         <constructor-arg value="M G Road" />
  20.         <constructor-arg value="Bangalore" />
  21.         <constructor-arg value="560001" />
  22.     </bean>
  23.  
  24.     <bean id="add2" class="com.kb.di.Address">
  25.         <constructor-arg value="44" type="int" />
  26.         <constructor-arg value="Residency Road" />
  27.         <constructor-arg value="Mumbai" />
  28.         <constructor-arg value="400051" />
  29.     </bean>
  30. </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">
		<constructor-arg value="1" type="int" />
		<constructor-arg value="Raj" />
		<constructor-arg>
			<array>
				<ref bean="add1" />
				<ref bean="add2" />
			</array>
		</constructor-arg>
	</bean>
	<bean id="add1" class="com.kb.di.Address">
		<constructor-arg value="33" type="int" />
		<constructor-arg value="M G Road" />
		<constructor-arg value="Bangalore" />
		<constructor-arg value="560001" />
	</bean>

	<bean id="add2" class="com.kb.di.Address">
		<constructor-arg value="44" type="int" />
		<constructor-arg value="Residency Road" />
		<constructor-arg value="Mumbai" />
		<constructor-arg 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 ConstructorDI class

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

  1. package com.kb.di;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. public class ConstructorDI {
  7.    
  8.     public static void main(String[] args) {
  9.         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");  
  10.         Person person = (Person)applicationContext.getBean("person");
  11.         person.showAddresses();
  12.     }
  13. }
package com.kb.di;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

Run the ConstructorDI 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