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:
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
package com.geeksforgeeks.org;
import com.geeksforgeeks.org.Address;
import com.geeksforgeeks.org.Employee;
import java.util.*;
import java.util.Map.Entry;
public class Company {
private Map<Employee, Address> employees;
public Company(Map<Employee, Address> employees)
{
this .employees = employees;
}
public void display()
{
for (Map.Entry<Employee, Address> entry :
employees.entrySet()) {
System.out.println(
"Employee Data ->"
+ entry.getKey().toString() + " Address ->"
+ entry.getValue().toString());
}
}
}
|
B. Employee.java
Java
package com.geeksforgeeks.org;
import com.geeksforgeeks.org.Address;
public class Employee {
private String name;
private String employeeID;
private String department;
public Employee(String name, String employeeID,
String department)
{
this .name = name;
this .employeeID = employeeID;
this .department = department;
}
public String toString()
{
return ( "[" + name + "," + employeeID + ","
+ department + "]" );
}
}
|
C. Address.java
Java
package com.geeksforgeeks.org;
public class Address {
private String houseNo;
private String pincode;
private String state;
private String country;
public Address(String houseNo, String pincode,
String state, String country)
{
super ();
this .houseNo = houseNo;
this .pincode = pincode;
this .state = state;
this .country = country;
}
public String toString()
{
return "[" + houseNo + "," + pincode + "," + state
+ "," + country + "]" ;
}
}
|
D. applicationContext.xml
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
< 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
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;
public class Test {
public static void main(String[] args)
{
Resource resource = new ClassPathResource(
"applicationContext.xml" );
BeanFactory factory = new XmlBeanFactory(resource);
Employee c = (Employee)factory.getBean( "company" );
c.display();
}
}
|
Output:
Employee Data -> [Ram, 101, software testing], Address -> [110/4, 128933, Delhi, India]
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
21 Feb, 2022
Like Article
Save Article