Open In App

Spring – Dependency Injection by Setter Method

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: 

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




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"?>
<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




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 }

Article Tags :