Open In App
Related Articles

Spring – Setter Injection with Dependent Object

Improve Article
Save Article
Like Article

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.


Each employee has the following properties:

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

Example: Employee Class 


// Java Program to Illustrate Employee Class
// Class
class Employee {
    // Class member members
    private String name;
    private String employeeID;
    private String department;
    private Address address;
    // Setter
    public void setName(String 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());





Address has the following properties:


  • House Number
  • Pincode
  • State
  • Country





class Address {
    private String houseNo;
    private String pincode;
    private String state;
    private String country;
    public Address(String houseNo, String pincode, String state, String country) {
        this.houseNo = houseNo;
        this.pincode = pincode;
        this.state = state; = 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 version="1.0" encoding="UTF-8"?> 
    <bean id="address" class=""
        <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 id="employee" class=""
        <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

D. Application File (

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



// Java Program to Illustrate Application Class
// Importing required classes
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
// Application(Main) Class
class Test {
    // Main driver method
    public static void main(String[] args)
        // Creating a class path resource
        Resource resource = new ClassPathResource(
        // 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





Name: Ram
Employee ID: 1001
Department: Software development
Address: [110/4, 121212, 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 : 18 Feb, 2022
Like Article
Save Article
Similar Reads
Complete Tutorials