Open In App

Spring – Dependency Injection by Setter Method

Last Updated : 19 Nov, 2021
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. The design principle of Inversion of Control emphasizes keeping the Java classes independent of each other and the container frees them from object creation and maintenance. These classes, managed by Spring, must adhere to the standard definition of Java-Bean. Dependency Injection in Spring also ensures loose coupling between the classes.

Need for Dependency Injection

Suppose class One needs the object of class Two to instantiate or operate a method, then class One is said to be dependent on class Two. Now though it might appear okay to depend on a module on the other but in the real world, this could lead to a lot of problems, including system failure. Hence such dependencies need to be avoided. Spring IOC resolves such dependencies with Dependency Injection, which makes the code easier to test and reuse. Loose coupling between classes can be possible by defining interfaces for common functionality and the injector will instantiate the objects of required implementation. The task of instantiating objects is done by the container according to the configurations specified by the developer.

Types of Spring Dependency Injection

There are two types of Spring Dependency Injection. They are: 

  • Setter Dependency Injection (SDI): This is the simpler of the two DI methods. In this, the DI will be injected with the help of setter and/or getter methods. Now to set the DI as SDI in the bean, it is done through the bean-configuration file For this, the property to be set with the SDI is declared under the <property> tag in the bean-config file.
  • Constructor Dependency Injection (CDI): In this, the DI will be injected with the help of constructors. Now to set the DI as CDI in bean, it is done through the bean-configuration file For this, the property to be set with the CDI is declared under the <constructor-arg> tag in the bean-config file.

Dependency Injection by Setter Method with Example

Setter injection is a dependency injection in which the spring framework injects the dependency object using the setter method. The call first goes to no argument constructor and then to the setter method. It does not create any new bean instance. Let’s see an example to inject dependency by the setter method.

  1. (POJO class)
  2. config.xml 

1. file


package com.spring;
public class Student {
    private String studentName;
    private String studentCourse;
    public String getStudentName() 
      return studentName;
    public void setStudentName(String studentName)
        this.studentName = studentName;
    public String getStudentCourse()
        return studentCourse;
    public void setStudentCourse(String studentCourse)
        this.studentCourse = studentCourse;
    @Override public String toString()
        return "Student{"
            + "studentName=" + studentName + 
             ", studentCourse=" + studentCourse + '}';

2. Config.xml file


<?xml version="1.0" encoding="UTF-8"?>
     <bean class="com.springframework.Student" name="stud">
       <property name="studentName">
               <value> John </value
       <property name="studentCourse">
               <value> Spring Framework </value

3. file


package com.spring;
import org.springframework.context.ApplicationContext;
public class GFG {
    public static void main(String[] args)
        ApplicationContext context = new ClassPathXmlApplicationCotenxt("config.xml");
        Student student= (Student)context.getBean("stud");


Student{ studentName= John  , studentCourse= Spring Framework }

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads