Open In App

Spring – Constructor Injection with Collection

Improve
Improve
Like Article
Like
Save
Share
Report

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.geeksforgeeks.org.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.geeksforgeeks.org.GFG">
        <constructor-arg>
            <bean class="com.geeksforgeeks.org.impl.CsvGFG" />
        </constructor-arg>
    </bean>
       
<bean id="CsvGFG" class="com.geeksforgeeks.org.impl.CsvGFG" />
<bean id="JsonGFG" class="com.geeksforgeeks.org.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.geeksforgeeks.org.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.geeksforgeeks.org.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]


Last Updated : 30 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads