Tuesday, October 7, 2025
HomeLanguagesJavaSpring – Constructor Injection with Collection

Spring – Constructor Injection with Collection

In the constructor injection, the dependency injection will be injected with the help of constructors. Now to set the dependency injection as constructor dependency injection(CDI) in bean, it is done through the bean-configuration file For this, the property to be set with the constructor dependency injection is declared under the <constructor-arg> tag in the bean-config file.

Example:

Java




package com.geeksforgeeks.org;
  
import com.neveropen.tech.IGeek;
  
public class GFG {
  
    // The object of the interface IGeek
    IGeek geek;
  
    // Constructor to set the CDI
    GFG(IGeek geek) { this.geek = geek; }
}


Setting the CDI in the bean-config file: 

XML




<beans
   
    <bean id="GFG" class="com.neveropen.tech.GFG">
        <constructor-arg>
            <bean class="com.neveropen.tech.impl.CsvGFG" />
        </constructor-arg>
    </bean>
       
<bean id="CsvGFG" class="com.neveropen.tech.impl.CsvGFG" />
<bean id="JsonGFG" class="com.neveropen.tech.impl.JsonGFG" />
           
</beans>


Constructor Injection with the collection

Spring frameworks provide us the facility to inject collection values via constructor in our spring application. The following collections can be used inside the <constructor-arg> tag:

  • list
  • set
  • map

Example:

1) Employee.java

Create a class Employee:

Java




package com.geeksforgeeks.org;
  
import com.neveropen.tech.IGeek;
import java.util.List;
  
public class Employee {
    private String name;
    private String employeeID;
    private String department;
    private List<String> address;
  
    public Employee(List<String> address)
    {
        this.address = (List<String>)address;
    }
  
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public String getemployeeID() { return employeeID; }
    public void setemployeeID(String employeeID)
    {
        this.employeeID = employeeID;
    }
    public String getdepartment() { return department; }
    public void setdepartment(String department)
    {
        this.department = department;
    }
  
    public List<String> getAddress() { return address; }
  
    public void display()
    {
        System.out.println("Name: " + getName());
        System.out.println("Employee ID: "
                           + getEmployeeID());
        System.out.println("Department: "
                           + getDepartment());
        System.out.println("Address: " + getAddress());
    }
}


2) applicationContext.xml

To define the list use the list element of constructor-arg.

XML




<?xml version="1.0" encoding="UTF-8"?>  
<beans  
    
    <bean id="employee" class="com.neveropen.tech.Employee">  
        <constructor-arg value="Ram"></constructor-arg>  
        <constructor-arg value="101"></constructor-arg
        <constructor-arg value="Software testing"></constructor-arg
        <constructor-arg>  
            <list>  
                <value>Gurugram</value>  
                <value>Haryana</value>  
                <value>India</value>  
            </list>  
        </constructor-arg>  
    </bean>  
    
</beans>


3) Test.java

Testing the spring application:

Java




package com.geeksforgeeks.org;
  
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 Test {
    public static void main(String[] args)
    {
        Resource resource = new ClassPathResource(
            "applicationContext.xml");
        BeanFactory factory = new XmlBeanFactory(resource);
  
        Employee e = (Employee)factory.getBean("employee");
        e.display();
    }
}


Output:

Name: Ram
Employee ID: 101
Department: Software testing
Address: [Gurugram, Haryana, India]
RELATED ARTICLES

Most Popular

Dominic
32340 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6709 POSTS0 COMMENTS
Nicole Veronica
11874 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6832 POSTS0 COMMENTS
Ted Musemwa
7091 POSTS0 COMMENTS
Thapelo Manthata
6781 POSTS0 COMMENTS
Umr Jansen
6785 POSTS0 COMMENTS