Open In App
Related Articles

Spring – Setter Injection with Collection

Improve Article
Save Article
Like Article

Dependency Injection is the main functionality provided by Spring IOC(Inversion of Control). The Spring-Core module is responsible for injecting dependencies through either Constructor or Setter methods. In Setter Dependency Injection(SDI) the dependency will be injected with the help of setters and getters methods. A bean-configuration file is used to set DI as SDI in the bean. For this, the property to be set with the SDI is declared under the <property> tag in the bean-config file.

A Collection in java is a group of individual objects. Spring framework provides us facility of Setter injection using the following Collections:

  • List
  • Map
  • Set



A company has a list of employees.


// Java program to Illustrate Company Class
// Importing required classes
import java.util.*;
// Company Class
class Company {
    // Class data members
    private String companyName;
    private List<String> employees;
    // Setter
    public void setCompanyName(String companyName)
        this.companyName = companyName;
    // Setter
    public void setEmployees(List<String> employees)
        this.employees = employees;
    // Getter
    public String getCompanyName() { return companyName; }
    // Getter
    public List<String> getEmployees() { return employees; }
    // Method
    public void display()
        System.out.println("Company: " + companyName);
        System.out.println("Employee list: " + companyName);
        // Iterating over using for each loop
        for (String employee : employees) {
            System.out.println("-" + employee);


B. applicationContext.xml


We will use property element for setter injection. The name element of the property attribute will be equal to the variable name and the value element will contain the value you want to assign to that variable.



<?xml version="1.0" encoding="UTF-8"?> 
    <bean id="company" class=""
        <property name="companyName" value="GeeksForGeeks"></property
        <property name="employees">



// Java Program to Illustrate Application Class
// Importing required classes
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
// Application (Main) Class
public class Main {
    // Main driver method
    public static void main(String[] args)
        // Creating a new class path resource
        Resource resource = new ClassPathResource(
        // Creating an object of BeanFactory class
        BeanFactory factory = new XmlBeanFactory(resource);
        // Creating an object of Employee class
        Employee e = (Employee)factory.getBean("employee");
        // Calling display() method inside main() method


Company: GeeksForGeeks
Employee list:

Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 21 Feb, 2022
Like Article
Save Article
Similar Reads
Complete Tutorials