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 the constructor injection, the dependency injection will be injected with the help of constructors. Now to set the dependency injection as constructor dependency injection(CDI) in bean, it is done through the bean-configuration file For this, the property to be set with the constructor dependency injection is declared under the <constructor-arg> tag in the bean-config file.
Spring framework provides us the facility to inject collection values via constructor in our spring application. The following collections can be used inside the <constructor-arg> tag.
- list
- set
- map
Implementation: In the following examples, we will see constructor injection with the non-string collection.
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 constructor 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 { private List<Employee> employees; // Constructor public Company(List<Employee> employees) { this .employees = employees; } // Method public void display() { Iterator<Employee> it = employees.iterator(); // Holds true until there is single element // remaining inside object while (it.hasNext()) { System.out.println(it.next().toString()); } } } |
B. Employee.java
Every employee has a name, employee ID, and department.
Java
// Java Program to Illustrate Employee Class package com.geeksforgeeks.org; // Class class Employee { // Class data member private String name; private String employeeID; private String department; // Constructor public Employee(String name, String employeeID, String department) { // This keyword refers to current instance itself this .name = name; this .employeeID = employeeID; this .department = department; } // Method public String toString() { return ( "[" + name + ", " + employeeID + ", " + department + "]" ); } } |
C. applicationContext.java
bean attribute of ref element will be used to specify the reference of ’employee1′ and ’employee2′ bean into the ‘company’ bean.
XML
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xsi:schemaLocation="http://www.springframework.org/schema/beans < bean id = "employee1" class = "com.neveropen.tech.Employee" > < constructor-arg value = "Sam" ></ constructor-arg > < constructor-arg value = "101" ></ constructor-arg > < constructor-arg value = "Software testing" ></ constructor-arg > </ bean > < bean id = "employee2" class = "com.neveropen.tech.Employee" > < constructor-arg value = "John" ></ constructor-arg > < constructor-arg value = "102" ></ constructor-arg > < constructor-arg value = "Software development" ></ constructor-arg > </ bean > < bean id = "company" class = "com.neveropen.tech.Company" > < constructor-arg > < list > < ref bean = "employee1" /> < ref bean = "employee2" /> </ list > </ constructor-arg > </ bean > </ beans > |
D. Test.java
Java
// Java Program to Illustrate Application Class package com.geeksforgeeks.org; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; // Application(Main) Class class Test { // Main driver method public static void main(String[] args) { // Creating a 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:
[Sam, 101, Software testing] [John, 102, Software development]