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.
Illustration:
Java
package com.geeksforgeeks.org;
import com.geeksforgeeks.org.IGeek;
public class GFG {
IGeek geek;
GFG(IGeek geek)
{
this .geek = geek;
}
}
|
Setting the CDI in the bean-config File
XML
< beans
< bean id = "GFG" class = "com.geeksforgeeks.org.GFG" >
< constructor-arg >
< bean class = "com.geeksforgeeks.org.impl.CsvGFG" />
</ constructor-arg >
</ bean >
< bean id = "CsvGFG" class = "com.geeksforgeeks.org.impl.CsvGFG" />
< bean id = "JsonGFG" class = "com.geeksforgeeks.org.impl.JsonGFG" />
</ beans >
|
Constructor Injection with Map
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 String.
A. Company.java
A company can have multiple employees. Each employee will have an employee ID and department. The ’employees’ map will store employee IDs as key and the respective departments will be stored as values.
Example:
Java
package com.geeksforgeeks.org;
import java.util.*;
import java.util.Map.Entry;
public class Company {
private Map<String, String> employees;
public Company(Map<String, String> employees)
{
this .employees = employees;
}
public void display()
{
for (Map.Entry<String, String> entry :
employees.entrySet()) {
System.out.println(
"Employee Id ->" + entry.getKey() + ","
+ " Department->" + entry.getValue());
}
}
}
|
B. applicationContext.xml
The entry attribute of map will be used to store key and value information.
Example:
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
< beans
< bean id = "company" class = "com.geeksforgeeks.org.Company" >
< constructor-arg >
< map >
< entry key = "101" value = "Software development" ></ entry >
< entry key = "102" value = "Software testing" ></ entry >
< entry key = "103" value = "Security" ></ entry >
</ map >
</ constructor-arg >
</ bean >
</ beans >
|
C. 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 ID -> 101, Department -> Software development
Employee ID -> 102, Department -> Software testing
Employee ID -> 103, Department -> Security
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 :
22 Feb, 2022
Like Article
Save Article