Constructor Dependency Injection with Collection of Integer / Long example

In some scenarios, we may need to inject a Collection of Integers/Long to an object rather than just one Integer/Long value.

In such cases, we inject a Collection of Integer/Long through a constructor.

Collection could be either List or Set but most of the requirements are best fit with List

Consider the example where one Person has multiple phone numbers

In this case, we are required to inject List of Long to person object.

Let us see the same in spring through Constructor Dependency Injection

Create a class called Person

It contains 3 attributes id,name and phoneNumbers where phoneNumbers is a list of Long

It also contains showPersonPhoneNumbers() method to display the list of phoneNumbers that we injected to the person object.

  1. package com.kb.di;
  3. import java.util.List;
  5. public class Person {
  6.     private int id;
  7.     private String name;
  8.     private List<Long> phoneNumbers;
  11.     public Person(int id, String name, List<Long> phoneNumbers) {
  12.         super();
  13. = id;
  14. = name;
  15.         this.phoneNumbers = phoneNumbers;
  16.     }
  19.     public void showPersonPhoneNumbers(){
  20.         for (Long phoneNumber : phoneNumbers) {
  21.             System.out.println(phoneNumber);
  23.         }
  25.     }
  26. }
package com.kb.di;

import java.util.List;

public class Person {
	private int id;
	private String name;
	private List<Long> phoneNumbers;

	public Person(int id, String name, List<Long> phoneNumbers) {
		super(); = id; = name;
		this.phoneNumbers = phoneNumbers;

	public void showPersonPhoneNumbers(){
		for (Long phoneNumber : phoneNumbers) {

Create a spring bean definition file

In this file, we used < constructor-arg > tag with < list > to define the list of Long
and value-type =java.lang.Long specifies that our list is of type Long.

If we want List of Integer then we can change the value-type to java.lang.Integer

< value > tag will take the values and convert it into the value-type specified

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns=""
  3.     xmlns:xsi=""
  4.     xmlns:p=""
  5.     xsi:schemaLocation="
  8.  <bean id="person" class="com.kb.di.Person">  
  9.     <constructor-arg value="1" type="int"></constructor-arg>  
  10.     <constructor-arg value="Raj"></constructor-arg>
  11.     <constructor-arg>
  12.     <list value-type="java.lang.Long">
  13.     <value>9999912345</value>
  14.     <value>9999956789</value>
  15.     </list>
  16.     </constructor-arg>
  17.  </bean>  
  18.   </beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
 <bean id="person" class="com.kb.di.Person">  
	<constructor-arg value="1" type="int"></constructor-arg>  
	<constructor-arg value="Raj"></constructor-arg> 
	<list value-type="java.lang.Long">

If we need to inject Set rather than List as in this case phone numbers needs to be unique, just change the above < list > tag to < set > tag.
And also make the phoneNumbers as Set in Person class instead of List

Create a pom.xml file

  1. <project xmlns="" xmlns:xsi=""
  2.   xsi:schemaLocation="">
  3.   <modelVersion>4.0.0</modelVersion>
  5.   <groupId>Spring</groupId>
  6.   <artifactId>SpringCore</artifactId>
  7.   <version>0.0.1-SNAPSHOT</version>
  8.   <packaging>jar</packaging>
  10.   <name>SpringCore</name>
  11.   <url></url>
  13.   <properties>
  14.     <>UTF-8</>
  15.   </properties>
  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>
  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="" xmlns:xsi=""





Create a ConstructorDI class
This class loads the spring beans using application context and calls the person class’s method to display the list of phone numbers.

  1. package com.kb.di;
  3. import org.springframework.beans.factory.BeanFactory;
  4. import org.springframework.beans.factory.xml.XmlBeanFactory;
  5. import org.springframework.context.ApplicationContext;
  6. import;
  7. import;
  8. import;
  10. public class ConstructorDI {
  12.     public static void main(String[] args) {
  13.         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");  
  14.         Person person = (Person)applicationContext.getBean("person");
  15.         person.showPersonPhoneNumbers();
  18.     }
  19. }
package com.kb.di;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;

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

Run the ConstructorDI class and see the output as below

About the Author

Founder of
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

Connect with me on Facebook for more updates

Share this article on