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 has a list of employees.
Java
package com.geeksforgeeks.org;
import java.util.*;
class Company {
private String companyName;
private List<String> employees;
public void setCompanyName(String companyName)
{
this .companyName = companyName;
}
public void setEmployees(List<String> employees)
{
this .employees = employees;
}
public String getCompanyName() { return companyName; }
public List<String> getEmployees() { return employees; }
public void display()
{
System.out.println( "Company: " + companyName);
System.out.println( "Employee list: " + companyName);
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
http:www.springframework.org/schema/beans/spring-beans-3.0.xsd">
< bean id = "company" class = "com.geeksforgeeks.org.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
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);
Employee e = (Employee)factory.getBean( "employee" );
e.display();
}
}
|
Output:
Company: GeeksForGeeks
Employee list:
-John
-Max
-Sam
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