Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Spring – Dependency Injection by Setter Method

  • Last Updated : 19 Nov, 2021

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. Employee.java (POJO class)
  2. config.xml 
  3. Main.java

1. Employee.java file

Java




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




<?xml version="1.0" encoding="UTF-8"?>
<beans 
                      https://www.springframework.org/schema/beans/spring-beans.xsd
                     http://www.springframework.org/schema/context
                      http://www.springframework.org/schema/context/spring-context.xsd">
    
     <bean class="com.springframework.Student" name="stud">
   
       <property name="studentName">
               <value> John </value
       <property/>
  
       <property name="studentCourse">
               <value> Spring Framework </value
          <property/>
  
    </bean>
    
</beans>


3. Main.java file

Java




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


Output:

Student{ studentName= John  , studentCourse= Spring Framework }


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!