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:
Implementation
A. Company.java
A company can have multiple employees. Here the Company class has a relationship with the Employee class. The Company class will contain the instances of Employee class. Hence, the Employee object will be the dependent object. We will use a list of this dependent object to demonstrate setter injection with non-string(dependent object) collection(list).
Java
package com.geeksforgeeks.org;
import java.util.*;
class Company {
private String companyName;
private List<Employee> employees;
public void setCompanyName(String companyName)
{
this .companyName = companyName;
}
public void setEmployees(List<Employee> employees)
{
this .employees = employees;
}
public String getCompanyName() { return companyName; }
public List<Employee> getEmployees()
{
return employees;
}
public void display()
{
System.out.println( "Company: " + companyName);
System.out.println( "Empoyees:" );
for (Employee employee : employees) {
System.out.println(employee.toString());
}
}
}
|
B. Employee.java
Employee class has the following properties:
- Name
- Employee ID
- Department
Java
package com.geeksforgeeks.org;
class Employee {
private String name;
private String employeeID;
private String department;
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;
}
@Override public String toString()
{
return ( "[Name: " + name
+ ", Employee Id: " + employeeID
+ ", Department: " + department + "]" );
}
}
|
C. applicationContext.xml
It is a configuration file that contains all the beans.
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
< beans
http:www.springframework.org/schema/beans/spring-beans-3.0.xsd">
< bean id = "employee1" class = "com.geeksforgeeks.org.Employee" >
< property name = "name" value = "John" ></ property >
< property name = "employeeID" value = "211" ></ property >
< property name = "department" value = "Penetration testing" ></ property >
</ bean >
< bean id = "employee2" class = "com.geeksforgeeks.org.Employee" >
< property name = "name" value = "Max" ></ property >
< property name = "employeeID" value = "212" ></ property >
< property name = "department" value = "Ethical hacking" ></ property >
</ bean >
< bean id = "company" class = "com.geeksforgeeks.org.Company" >
< property name = "companyName" value = "GeeksForGeeks" ></ property >
< property name = "employees" >
< list >
< ref bean = "employee1" />
< ref bean = "employee2" />
</ list >
</ property >
</ bean >
</ beans >
|
D. Main.java
This is our application(main) class which will call the display() method of the Company class
Java
package com.geeksforgeeks.org;
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;
public class Main {
public static void main(String[] args)
{
Resource resource = new ClassPathResource(
"applicationContext.xml" );
BeanFactory factory = new XmlBeanFactory(resource);
Company c = (Company)factory.getBean( "company" );
c.display();
}
}
|
Output:
Company: GeeksForGeeks
Employees:
[Name: John, Employee ID: 211, Department: Penetration testing]
[Name: Sam, Employee ID: 212, Department: Ethical hacking]
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 :
18 Feb, 2022
Like Article
Save Article