Java Lesson 52 – Spring IoC | Dataplexa

Spring IoC (Inversion of Control)

In the previous lesson, you learned what Spring is and why it is used. Now it is time to understand the most important concept behind Spring — Inversion of Control (IoC).

IoC is the foundation on which the entire Spring framework is built. If you understand IoC clearly, everything else in Spring becomes easier.


What Is Inversion of Control?

In a traditional Java application, the programmer controls everything: object creation, dependency management, and object lifecycle.

In Spring, this control is inverted. Instead of your code creating objects, the Spring container creates and manages them.

This concept is called Inversion of Control.


Traditional Java Approach (Without IoC)

Let’s look at a simple real-world example.

An application has an OrderService that depends on a PaymentService.


public class PaymentService {
    public void pay() {
        System.out.println("Payment processed");
    }
}

public class OrderService {
    PaymentService paymentService = new PaymentService();

    public void placeOrder() {
        paymentService.pay();
    }
}

Here, OrderService directly creates PaymentService.

This creates tight coupling. Changing the payment logic requires changing the order logic.


Problem With This Approach

  • Hard to modify
  • Difficult to test
  • Not flexible
  • Not scalable

In large applications, this becomes a serious issue.


Spring IoC Solution

Spring solves this by taking control of object creation.

Instead of using new, Spring creates objects and injects them where needed.

Your classes focus only on business logic.


What Is the Spring Container?

The Spring Container is responsible for:

  • Creating objects (beans)
  • Managing object lifecycle
  • Injecting dependencies
  • Destroying objects when needed

It acts as a central manager for your application.


What Is a Bean?

A Bean is any object that is created and managed by Spring.

Once an object becomes a bean, Spring controls its lifecycle.

Beans are defined using configuration or annotations.


IoC With Dependency Injection

IoC is implemented in Spring using Dependency Injection (DI).

Instead of creating dependencies inside a class, they are provided from outside.


public class OrderService {

    private PaymentService paymentService;

    public OrderService(PaymentService paymentService) {
        this.paymentService = paymentService;
    }

    public void placeOrder() {
        paymentService.pay();
    }
}

Now, OrderService does not care how PaymentService is created.

Spring handles it.


Why IoC Is Powerful

  • Loose coupling
  • Better testability
  • Easy configuration changes
  • Clean architecture

You can replace implementations without changing business logic.


Real-World Analogy

Think of a restaurant.

A customer does not go into the kitchen to cook food. The restaurant manages chefs and services.

Similarly, Spring manages objects so your code doesn’t have to.


IoC in Enterprise Applications

Modern enterprise systems rely on IoC to manage hundreds or thousands of components.

Without IoC, maintaining such systems would be nearly impossible.


What Comes Next?

You now understand the idea behind IoC.

In the next lesson, we will see how IoC is implemented practically using Spring Dependency Injection (DI) with real configuration and annotations.