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 is as depicted through below XML file as follows:
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 dependent object
If there exists a relationship between the classes of our spring application, then we create the instance of the dependent object also called the contained object. After creating the instance of the dependent object we pass it as an argument of the main class container. Example: If an employee has an address, then the instance of the address class is the dependent object.
A. Employee.java
Java
package com.geeksforgeeks.org;
import com.geeksforgeeks.org.Address;
public class Employee {
private String name;
private String employeeID;
private String department;
private Address address;
public Employee(String name, String employeeID,
String department, Address address)
{
this .name = name;
this .employeeID = employeeID;
this .department = department;
this .address = (Address)address;
}
public void display()
{
System.out.println( "Name: " + name);
System.out.println( "Employee ID: " + employeeID);
System.out.println( "Department: " + department);
System.out.println( "Address: " + address);
}
}
|
B. 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 + "]" ;
}
}
|
C. applicationContext.xml
<ref> is used to make reference to the dependent object.
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
< beans
< bean id = "address1" class = "com.geeksforgeeks.org.Address" >
< constructor-arg value = "110/4" ></ constructor-arg >
< constructor-arg value = "123092" ></ constructor-arg >
< constructor-arg value = "Delhi" ></ constructor-arg >
< constructor-arg value = "India" ></ constructor-arg >
</ bean >
< 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 >
< ref bean = "address1" />
</ constructor-arg >
</ bean >
</ beans >
|
D. 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 e = (Employee)factory.getBean( "employee" );
e.display();
}
}
|
Output:
Name: Ram
Employee ID: 101
Department: Software testing
Address: [110/4, 123092, 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