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.
Example:
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 the 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:
Example:
1) Employee.java
Create a class Employee:
Java
package com.geeksforgeeks.org;
import com.geeksforgeeks.org.IGeek;
import java.util.List;
public class Employee {
private String name;
private String employeeID;
private String department;
private List<String> address;
public Employee(List<String> address)
{
this .address = (List<String>)address;
}
public String getName() { return name; }
public void setName(String name) { this .name = name; }
public String getemployeeID() { return employeeID; }
public void setemployeeID(String employeeID)
{
this .employeeID = employeeID;
}
public String getdepartment() { return department; }
public void setdepartment(String department)
{
this .department = department;
}
public List<String> getAddress() { return address; }
public void display()
{
System.out.println( "Name: " + getName());
System.out.println( "Employee ID: "
+ getEmployeeID());
System.out.println( "Department: "
+ getDepartment());
System.out.println( "Address: " + getAddress());
}
}
|
2) applicationContext.xml
To define the list use the list element of constructor-arg.
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
< beans
< bean id = "employee" class = "com.geeksforgeeks.org.Employee" >
< constructor-arg value = "Ram" ></ constructor-arg >
< constructor-arg value = "101" ></ constructor-arg >
< constructor-arg value = "Software testing" ></ constructor-arg >
< constructor-arg >
< list >
< value >Gurugram</ value >
< value >Haryana</ value >
< value >India</ value >
</ list >
</ constructor-arg >
</ bean >
</ beans >
|
3) Test.java
Testing the spring application:
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 e = (Employee)factory.getBean( "employee" );
e.display();
}
}
|
Output:
Name: Ram
Employee ID: 101
Department: Software testing
Address: [Gurugram, Haryana, 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 :
30 Jan, 2022
Like Article
Save Article