Open In App

Spring – Setter Injection with Dependent Object

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

Dependency Injection is the main functionality provided by Spring IOC(Inversion of Control). The Spring-Core module is responsible for injecting dependencies through either Constructor or Setter methods.

In Setter Dependency Injection(SDI) the dependency will be injected with the help of setters and getters methods. A bean-configuration file is used to set DI as SDI in the bean. For this, the property to be set with the SDI is declared under the <property> tag in the bean-config file.

Setter 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 and will be contained in the Employee class.

Implementation: The following example demonstrates a setter injection with a dependent object.

A. Employee.java

Each employee has the following properties:

  • Name
  • Employee ID
  • Department
  • Address(Dependent object)

Example: Employee Class 

Java




// Java Program to Illustrate Employee Class
 
package com.geeksforgeeks.org;
 
// Class
class Employee {
 
    // Class member members
    private String name;
    private String employeeID;
    private String department;
    private Address address;
 
    // Setter
    public void setName(String name) { this.name = name; }
 
    // Setter
    public void setemployeeID(String employeeID)
    {
        this.employeeID = employeeID;
    }
 
    // Setter
    public void setdepartment(String department)
    {
        this.department = department;
    }
 
    // Setter
    public void setAddress(Address address)
    {
        this.address = address;
    }
 
    // Getter
    public String getName() { return name; }
 
    // Getter
    public String getemployeeID() { return employeeID; }
 
    // Getter
    public String getdepartment() { return department; }
 
    // Getter
    public Address getAddress() { return address; }
 
    // Method
    public void display()
    {
        // Print statements
        System.out.println("Name: " + getName());
        System.out.println("Employee ID: "
                           + getEmployeeID());
        System.out.println("Department: "
                           + getDepartment());
        System.out.println("Address: "
                           + getAddress().toString());
    }
}


 

 

B. Address.java

 

Address has the following properties:

 

  • House Number
  • Pincode
  • State
  • Country

 

Example:

 

Java




package com.geeksforgeeks.org;
 
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

We will use ref attribute of property element for specifying the reference of the address bean in the employee bean.

XML




<?xml version="1.0" encoding="UTF-8"?> 
<beans 
   
    <bean id="address" class="com.geeksforgeeks.org.Address"
        <property name="houseNo" value="110/4"></property
        <property name="pincode" value="121212"></property
        <property name="state" value="Delhi"></property
        <property name="country" value="India"></property
    </bean
   
    <bean id="employee" class="com.geeksforgeeks.org.Employee"
        <property name="name" value="Ram"></property
        <property name="employeeID" value="1001"></property
        <property name="department" value="Software development"></property>
        <property name="address" ref="address"></property
    </bean
   
</beans


D. Application File (Test.java)

This class will call the display() method of Employee class.

Example:

Java




// Java Program to Illustrate Application Class
 
package com.geeksforgeeks.org;
 
// Importing required classes
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
 
// Application(Main) Class
class Test {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating a 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");
 
        // Calling print() method inside main() method
        e.display();
    }
}


 

 

Output:

 

Name: Ram
Employee ID: 1001
Department: Software development
Address: [110/4, 121212, Delhi, India]

 



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

Similar Reads