Open In App

Spring – Constructor Injection with Dependent Object

Last Updated : 21 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 {
 
    // The object of the interface IGeek
    IGeek geek;
 
    // Constructor to set the CDI
    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




// Java Program to Illustrate Employee Class
 
package com.geeksforgeeks.org;
import com.geeksforgeeks.org.Address;
 
// Class
public class Employee {
    private String name;
    private String employeeID;
    private String department;
 
    // Here 'address' is an dependent object.
    private Address address;
 
    // Parameterised constructor
    public Employee(String name, String employeeID,
                    String department, Address address)
    {
        // this keyword refers to current instance itself
        this.name = name;
        this.employeeID = employeeID;
        this.department = department;
        this.address = (Address)address;
    }
 
    // Method
    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




// Java Program to Illustrate Address Class
 
package com.geeksforgeeks.org;
 
public class Address {
    private String houseNo;
    private String pincode;
    private String state;
    private String country;
 
    // Constructor of Address Class
    public Address(String houseNo, String pincode,
                   String state, String country)
    {
        // Super keyword refers to parent class
        super();
        // This keyword refers to current instance itself
        this.houseNo = houseNo;
        this.pincode = pincode;
        this.state = state;
        this.country = country;
    }
 
    // Method
    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




// Java Program to Illustrate Application Class
 
package com.geeksforgeeks.org;
 
// Importing required classes from respective packages
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;
 
// Main(Application) class
public class Test {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating new 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 e = (Employee)factory.getBean("employee");
        e.display();
    }
}


Output:

Name: Ram
Employee ID: 101
Department: Software testing
Address: [110/4, 123092, Delhi, India]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads