Skip to main content
Beginner Step-by-Step Tutorial 10 min read Spring Boot 3.x / Spring Framework 6

Spring Boot Dependency Injection & IoC Architecture Guide

Wire loosely coupled Spring Boot 3 services cleanly with constructor injection, immutable fields, and explicit interfaces.

Written by Ravi Shankar Mishra (Founder & Lead Systems Architect, MockAttempt Academy) Reviewed by MockAttempt Engineering Editorial Desk
Last updated: Verification Policy

Learning Objectives

  • Trace how Spring’s `ApplicationContext` creates, resolves, and caches Singleton beans at startup.
  • Explain why constructor injection beats field `@Autowired` for immutability and unit testing.
  • Handle multiple interface implementations safely with `@Primary` or `@Qualifier`.

Prerequisites

  • Core Java Classes, Interfaces, and Polymorphism

What Problem Does Dependency Injection Solve?

When a controller runs new RazorpayGateway() directly inside its constructor, swapping payment providers or writing fast isolated unit tests without network calls becomes painful. Spring Inversion of Control (IoC) flips ownership: components declare required interfaces in their constructor, and Spring’s container instantiates and injects the matching bean at startup.

Since Spring Framework 4.3+, single-constructor classes do not even require @Autowired — constructor parameters automatically become managed dependencies.

Continue learning with Fixing Spring Data JPA N+1 Queries or practise Spring Boot Mock Tests inside our Java Backend Career Program.

Practical Programming Examples

Constructor-Injected Multi-Implementation Service Pattern (Spring Boot 3)

Wire a domain NotificationSender interface cleanly into OrderService without brittle field @Autowired annotations.

Spring Boot 3.3 / JDK 21
public interface NotificationSender {
    void sendReceipt(String recipient, long totalInr);
}

@Service
public class EmailNotificationSender implements NotificationSender {
    @Override
    public void sendReceipt(String recipient, long totalInr) {
        System.out.println("Sent GST receipt for INR " + totalInr + " to " + recipient);
    }
}

@Service
public class EnrollmentService {
    private final NotificationSender notifier;

    public EnrollmentService(NotificationSender notifier) {
        this.notifier = notifier;
    }

    public void confirmSeat(String email) {
        notifier.sendReceipt(email, 53_100L);
    }
}
Expected Output
Sent GST receipt for INR 53100 to student@example.com
Step-by-Step Execution
  1. Spring component scanning registers `EmailNotificationSender` as the bean satisfying `NotificationSender`.
  2. When instantiating `EnrollmentService`, the IoC container invokes its single constructor and passes the managed bean.
  3. Declaring `private final` ensures `notifier` can never be null or reassigned mid-request.

Common Mistakes to Avoid

  • Injecting concrete implementation classes instead of interfaces where interchangeable strategies belong.
  • Using `@Autowired` on private fields, hiding mandatory dependencies until runtime.

Production Best Practices

  • Default to constructor injection + `private final` dependencies across every `@Service` and `@RestController`.
  • Break circular A ↔ B dependencies cleanly by extracting a shared C coordinator or domain event listener.

Technical Interview Relevance

Top Spring Boot interview topic covering ApplicationContext vs BeanFactory, Singleton vs Prototype scopes, and proxy self-invocation traps.

Practice & Self-Check Questions

Are Spring Singleton beans thread-safe automatically?

No — Singleton simply means one instance per container. Keep service beans stateless (store per-request state in local variables, not instance fields).

Optional Next Step

Have Questions About Learning Spring Boot Structured Cohorts?

Request the JBCP syllabus & weekend batch schedule from an Academic Counselor.

View JBCP Details →
Learn. Practise. Prove. Get Career-Ready.

Ready to Build Your IT Career?

Explore industry-led programmes and develop practical skills through training, assessments and interview preparation.