Dependency injection
What is Dependency Injection?
Dependency Injection (DI) is a design pattern in which one object is injected into another to build loosely coupled applications.
It is a core concept of the Inversion of Control (IoC) principle in the Spring Framework.
Dependency Injection (DI) is a design pattern in which one object is injected into another to build loosely coupled applications.
It is a core concept of the Inversion of Control (IoC) principle in the Spring Framework.
Types of Dependency Injection
- Constructor Injection
- Setter (Property) Injection – covered in this guide.
Why Use Setter Injection?
Java Bean Class: Student.java
XML Configuration File: configure.xml
Main Class: App.java
Output:
- You can inject only partial dependencies (unlike constructor injection which needs all dependencies upfront).
- Useful when a class has optional properties.
- Setter methods offer flexibility and clarity in bean configuration.
Key Point
Setter injection uses tags in the XML configuration.
Setter injection uses
Works with primitive types, collections, or even other beans.
The Spring Container (ApplicationContext) handles object creation and dependency injection.
The Spring Container (ApplicationContext) handles object creation and dependency injection.
Example: Injecting a Student Object with Multiple Fields
Project Structure:
Project Structure:
com.quipoin
│
├── Student.java
├── App.java
└── configure.xml
Java Bean Class: Student.java
package com.quipoin;
import java.util.Map;
public class Student { private String name; private int id; private Map<String, Integer> marks; private char gender;
public void setName(String name) { this.name = name; } public String getName() { return name; }
public void setId(int id) { this.id = id; } public int getId() { return id; }
public void setMarks(Map<String, Integer> marks) { this.marks = marks; } public Map<String, Integer> getMarks() { return marks; }
public void setGender(char gender) { this.gender = gender; } public char getGender() { return gender; }}
XML Configuration File: configure.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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="com.quipoin.Student" name="st"> <property name="name" value="Naveen"/> <property name="id" value="101"/> <property name="gender" value="M"/> <property name="marks"> <map> <entry key="Java" value="68"/> <entry key="sql" value="85"/> <entry key="Spring" value="49"/> <entry key="jdbc" value="73"/> </map> </property> </bean></beans>
Main Class: App.java
package com.quipoin;
import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("configure.xml");
Student s1 = (Student) context.getBean("st");
System.out.println("Student Name: " + s1.getName()); System.out.println("Student Id: " + s1.getId()); System.out.println("Marks: " + s1.getMarks()); System.out.println("Gender: " + s1.getGender()); }}
Output:
Student Name: NaveenStudent Id: 101Marks: {Java=68, sql=85, Spring=49, jdbc=73}Gender: M
How Setter Injection Works
- Spring loads the XML configuration.
- Creates a Student object.
- Uses the setName(), setId(), setGender(), and setMarks() methods to inject values.
- Injected object is retrieved using getBean() in the main class.