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
Implementation:
A. Company.java
A company has a list of employees.
Java
// Java program to Illustrate Company Class package com.geeksforgeeks.org; // 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
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xsi:schemaLocation="http://www.springframework.org/schema/beans http:www.springframework.org/schema/beans/spring-beans-3.0.xsd"> < bean id = "company" class = "com.neveropen.tech.Company" > < property name = "companyName" value = "GeeksForGeeks" ></ property > < property name = "employees" > < list > < value >"John"</ value > < value >"Max"</ value > < value >"Sam"</ value > </ list > </ property > </ bean > </ beans > |
C. Main.java
Java
// Java Program to Illustrate Application Class package com.geeksforgeeks.org; // Importing required classes import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; // 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( "applicationContext.xml" ); // 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 e.display(); } } |
Output:
Company: GeeksForGeeks Employee list: -John -Max -Sam