Spring - Dependency Injection by Setter Method - GeeksforGeeks

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.
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.
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.
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.
Employee.java (POJO class)config.xml Main.java
Employee.java (POJO class)
config.xml 
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  xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/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 }
Java-Spring
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Generics in Java
Introduction to Java
Comparator Interface in Java with Examples
Internal Working of HashMap in Java
Strings in Java