Open In App

Spring Setter Injection

Last Updated : 01 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Dependency Injection (DI) is a design pattern used to inject the dependency of a class into another class. It implements Inversion of Control (IoC container). The IOC container is the heart of the Spring framework. In the Spring Framework, DI is a core functionality that allows one object to inject the dependencies of another object.

We can inject one object property into another object by using three ways, but in this article, we can focus only on setter injection.

Prerequisite: Dependency Injection

Types of Dependency Injection

Spring supports three types of Dependency Injection.

  1. Constructor injection: Dependencies are provided through constructors.
  2. Setter injection: Dependencies are injected through public setter methods.
  3. Field injection: Dependencies are injected directly into the class by fields.

This article focuses on Setter Dependency Injection, demonstrating how to implement it using Spring’s XML-based configuration.

Setter Dependency Injection in Spring

Setter Dependency injection is used to inject dependencies by setter methods. In setter method (DI), Spring calls the setter method of the class after creating the object, to inject the required dependency for a class. And basically, dependencies are injected by a public setter method.

Advantages of setter injection

  • It allows partial injection.
  • It provides flexibility.
  • It is used to easier to read and manage in large configurations

When to Use Setter Injection

  • Use Setter Injection when the dependency is optional.
  • Use Setter Injection When You need to change the dependency at runtime.
  • It is easy to replace dependencies manually.

Example: 

Let’s take a real-world example. Like a college class, it depends on a Department class. We inject the Department object into the College object by the setter method.

Step 1: Create a Department class:

Java
// it is a simple POJO class
public class Department {
    private String name;
    // define public setter method
    public void setName(String name) { this.name = name; }
    // define public getter method.
    public String printDepartment() { return name; }
}


It is a simple POJO class that defines the public getter and setter methods for getting and setting the value of the variable.

Step 2: Create a simple College class.

Java
public class College {
    // Create reference variable of Department class
    
    private Department department;

    // Define Setter method for injection
    
    public void setDepartment(Department department)
    {
        this.department = department;
    }
    // for displaying the department informtion
    
    public void displayDepartment()
    {
        System.out.println(department.printDepartment()+"Department injected");
    }
}


In this code, we take the reference of a department class and inject it into the college class by using the setter method.

3. Create an XML file, config.xml.

Java
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd"> 
    <!-- define a bean for department class--> 
    <bean id="d" class="com.example.Department"> 
        <property name="name" value="Computer Science"/> 
    </bean> 
    <!-- Bean for College with Setter Injection --> 
    <bean id="c" class="com.example.College"> 
        <property name="department" ref="d"/> 
    </bean> 
</beans>


Explanation:

  • <bean>: It is used to declare a bean definition.
  • Id: It is a unique identifier used to retrieve the bean.
  • <property>: It is used to inject values or references to other beans.
  • Ref: It is used to inject one bean into another by referencing its ID.
  • This is a configuration file; its name is config.xml. It defines beans and their dependencies for the Spring container.

Step 4. Now, create a Main class to run the application.

Java
// Main.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");

        College college = (College) context.getBean("c");
        college.displayDepartment();
    }
}

Output:

Computer
output

How the Application Works 

  1. First, the IoC container loads the config.xml file from the classpath.
  2. IOC container reads all <bean> definitions and creates instances of the mentioned classes.
  3. It injects the object of the Department class into the College class object using the setter method.
  4. The getBean("c") method fetches the College bean from the container.

Article Tags :
Practice Tags :

Similar Reads