← Back to list

Inversion of Control (IoC) Container — Complete Guide

🔹 What is IoC (Inversion of Control)?

Arpit Bhatt · 2026-07-04 18:11 · 0 claps · 2.5 min read paywalled
#java #spring-framework #ioc-container #inversion-of-control #programmingpulse
Open on Medium ↗
Wiki topics: OPS · LLMOps & Inference 💻 · Programming ☁️ · DevOps & Cloud

Inversion of Control (IoC) Container — Complete Guide

🔹 What is IoC (Inversion of Control)?

Inversion of Control (IoC) is a design principle where the control of object creation and dependency management is transferred from the application code to a container or framework.

👉 In simple words: Instead of your code creating objects, someone else (the container) creates and manages them.

🔹 Traditional Approach (Without IoC)

Let’s first understand the problem.

❌ Tight Coupling Example

class Engine {
    public void start() {
        System.out.println("Engine started");
    }
}

class Car {
    private Engine engine;
    public Car() {
        this.engine = new Engine(); // tightly coupled
    }
    public void drive() {
        engine.start();
        System.out.println("Car is running");
    }
}

🚨 Problem:

  • Car is tightly dependent on Engine
  • Hard to test
  • Hard to change implementation (e.g., ElectricEngine)

🔹 What is an IoC Container?

An IoC Container is a framework component that:

  • Creates objects (beans)
  • Injects dependencies
  • Manages lifecycle

👉 In Spring Framework, this is done by:

  • BeanFactory (basic)
  • ApplicationContext (advanced, commonly used)

🔹 Core Responsibilities of IoC Container

  1. Object Creation
  2. Dependency Injection (DI)
  3. Lifecycle Management
  4. Configuration Handling

🔹 Real-World Analogy 🏨

Think of a Hotel Room Service:

  • You (client) don’t cook food yourself
  • You just order → food arrives

Mapping:

Real WorldIoC ConceptYouClient (Car class)KitchenIoC ContainerFoodDependency (Engine)

👉 You don’t create food → it’s provided to you 👉 Same way, objects are injected, not created manually

🔹 Dependency Injection (DI)

IoC is achieved through Dependency Injection.

Types of DI:

  1. Constructor Injection
  2. Setter Injection
  3. Field Injection (not recommended)

🔹 IoC with Spring — Practical Example

✅ Step 1: Define Components

@Component
class Engine {
    public void start() {
        System.out.println("Engine started");
    }
}
@Component
class Car {
    private final Engine engine;

@Autowired
    public Car(Engine engine) {
        this.engine = engine;
    }
    public void drive() {
        engine.start();
        System.out.println("Car is running");
    }
}

✅ Step 2: Spring Boot Main Class

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(App.class, args);
        Car car = context.getBean(Car.class);
        car.drive();
    }
}

🔹 What Happens Internally?

  1. Spring scans @Component
  2. Creates objects (beans)
  3. Detects dependencies (Engine)
  4. Injects into Car
  5. Returns ready-to-use object

👉 You didn’t use new Engine() anywhere!

🔹 Benefits of IoC Container

✅ 1. Loose Coupling

Easily swap implementations

interface Engine { }
class PetrolEngine implements Engine { }
class ElectricEngine implements Engine { }

✅ 2. Easy Testing

Mock dependencies easily

✅ 3. Better Maintainability

Clean and modular code

✅ 4. Centralized Configuration

All object creation is managed in one place

🔹 Real-World Example (Software Level)

🎯 Scenario: Payment System

Without IoC:

class PaymentService {
    private CreditCardProcessor processor = new CreditCardProcessor();
}

👉 Hardcoded dependency

With IoC:

@Component
class PaymentService {
    private final PaymentProcessor processor;

@Autowired
    public PaymentService(PaymentProcessor processor) {
        this.processor = processor;
    }
}

👉 Now you can switch:

  • Credit Card
  • UPI
  • PayPal

WITHOUT changing PaymentService

🔹 IoC vs Dependency Injection

IoCDependency InjectionConceptImplementationControl is invertedDependencies are injectedHigh-level principlePractical technique

🔹 Bean Lifecycle (Spring IoC)

  1. Bean Instantiation
  2. Dependency Injection
  3. Initialization
  4. Usage
  5. Destruction

🔹 Types of IoC Containers in Spring

1. BeanFactory

  • Lightweight
  • Lazy loading

2. ApplicationContext

  • Advanced features
  • Event handling
  • Internationalization
  • Recommended

🔹 Common Annotations

AnnotationPurpose@ComponentMarks class as bean@AutowiredInject dependency@ServiceBusiness logic@RepositoryDatabase layer@ConfigurationJava config@BeanManual bean creation

🔹 When Should You Use IoC?

  • Large applications
  • Microservices
  • When flexibility is needed
  • When testing is important

🔹 Key Takeaways 🚀

  • IoC = Control moved to container
  • DI = Way to achieve IoC
  • Spring IoC Container manages everything
  • Leads to clean, scalable, and testable code

🔹 Short Summary

“Don’t call dependencies, let them be provided to you.”

____

Happy coding!

Follow my Instagram page — Programming_Pulse for daily programming tips and insights!

*https://www.instagram.com/programming_pulse*


메타데이터
post_id
7db074d65f68
slug
inversion-of-control-ioc-container-complete-guide-7db074d65f68
url
https://medium.com/@arpitbhatt027/inversion-of-control-ioc-container-complete-guide-7db074d65f68
canonical_url
https://medium.com/@arpitbhatt027/inversion-of-control-ioc-container-complete-guide-7db074d65f68
author_url
https://medium.com/@arpitbhatt027
status
ok
fetched_at
2026-07-29 13:30:38