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.
Constructor Injection with Collection
Spring frameworks provide 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 example, we will see constructor injection with Map. The map will have both key and value as non-strings. Key will be Employee which has the following fields:
- Name
- Employee ID
- Department
Value will be Address which has the following parameters:
- House No.
- Pincode
- State
- Country
A. Company.java
// Java Program to Illustrate Company Class package com.geeksforgeeks.org;
// Importing required classes import com.geeksforgeeks.org.Address;
import com.geeksforgeeks.org.Employee;
import java.util.*;
import java.util.Map.Entry;
// Class public class Company {
// Class member variable
private Map<Employee, Address> employees;
// Constructor
public Company(Map<Employee, Address> employees)
{
// This keyword refers to current instance itself
this .employees = employees;
}
// Method
public void display()
{
// Iterating over Map using for each loop
for (Map.Entry<Employee, Address> entry :
employees.entrySet()) {
// Print statement
System.out.println(
"Employee Data ->"
+ entry.getKey().toString() + " Address ->"
+ entry.getValue().toString());
}
}
} |
B. Employee.java
// Java Program to Illustrate Employee Class package com.geeksforgeeks.org;
// Importing required classes import com.geeksforgeeks.org.Address;
// Class public class Employee {
// Class data members
private String name;
private String employeeID;
private String department;
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
// Overriding toString() method
public String toString()
{
return ( "[" + name + "," + employeeID + ","
+ department + "]" );
}
} |
C. Address.java
// Java Program to Illustrate Address Class package com.geeksforgeeks.org;
// Class public class Address {
// Class data members
private String houseNo;
private String pincode;
private String state;
private String country;
// Constructor
public Address(String houseNo, String pincode,
String state, String country)
{
super ();
this .houseNo = houseNo;
this .pincode = pincode;
this .state = state;
this .country = country;
}
// Method
// Overriding toString() method
public String toString()
{
return "[" + houseNo + "," + pincode + "," + state
+ "," + country + "]" ;
}
} |
D. applicationContext.xml
<? xml version = "1.0" encoding = "UTF-8" ?>
< beans xsi:schemaLocation="http://www.springframework.org/schema/beans
< bean id = "employee1" class = "com.geeksforgeeks.org.Employee" >
< constructor-arg value = "Ram" ></ constructor-arg >
< constructor-arg value = "101" ></ constructor-arg >
< constructor-arg value = "Software development" ></ constructor-arg >
</ bean >
< bean id = "user1" class = "com.geeksforgeeks.org.Address" >
< constructor-arg value = "110/4" ></ constructor-arg >
< constructor-arg value = "128933" ></ constructor-arg >
< constructor-arg value = "Delhi" ></ constructor-arg >
< constructor-arg value = "India" ></ constructor-arg >
</ bean >
< bean id = "company" class = "com.geeksforgeeks.org.Company" >
< constructor-arg >
< map >
< entry key-ref = "employee1" value-ref = "address1" ></ entry >
</ map >
</ constructor-arg >
</ bean >
</ beans >
|
E. Test.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.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
// Application class // Main class public 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 Employee class
Employee c = (Employee)factory.getBean( "company" );
// Calling print() method inside main() method
c.display();
}
} |
Output:
Employee Data -> [Ram, 101, software testing], Address -> [110/4, 128933, Delhi, India]