Spring – Setter Injection with Non-String Collection
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 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
// Java Program to Illustrate Company Class package com.geeksforgeeks.org; // Importing required classes import java.util.*; // Class class Company { // Class data members private String companyName; private List<Employee> employees; // Setter public void setCompanyName(String companyName) { this .companyName = companyName; } // Setter public void setEmployees(List<Employee> employees) { this .employees = employees; } // method public String getCompanyName() { return companyName; } public List<Employee> getEmployees() { return employees; } // Method public void display() { System.out.println( "Company: " + companyName); System.out.println( "Empoyees:" ); // Iterating over using for each loop for (Employee employee : employees) { System.out.println(employee.toString()); } } } |
B. Employee.java
Employee class has the following properties:
- Name
- Employee ID
- Department
Java
// Java Program to Illustrate Employee Class package com.geeksforgeeks.org; // Class class Employee { // Class data members private String name; private String employeeID; private String department; // Method public String getName() { return name; } // Setter public void setName(String name) { this .name = name; } // Getter public String getEmployeeID() { return employeeID; } // Setter public void setEmployeeID(String employeeID) { this .employeeID = employeeID; } // Getter public String getDepartment() { return department; } // Setter public void setDepartment(String department) { this .department = department; } // Method // Overriding toString() method of String class @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 xsi:schemaLocation="http://www.springframework.org/schema/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
// Java Program to Illustrate Application Class package com.geeksforgeeks.org; // Importing required classes from respective packages 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 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 Company class Company c = (Company)factory.getBean( "company" ); // Calling display() method inside main() method c.display(); } } |
Output:
Company: GeeksForGeeks Employees: [Name: John, Employee ID: 211, Department: Penetration testing] [Name: Sam, Employee ID: 212, Department: Ethical hacking]
Please Login to comment...