NashTech Blog

Understanding Inversion of Control (IoC) in Spring

Table of Contents

In traditional Java applications (pre-framework or plain Java SE), developers manually manage object creation, lifecycle, and wiring. This means:

  1. You decide when an object is instantiated
  2. You manage its dependencies
  3. You coordinate lifecycle and destruction

This tightly couples construction logic to business logic, makes testing cumbersome, and leads to complex initialization sequences as the codebase grows.

Inversion of Control (IoC) — a core principle of modern frameworks — reverses this relationship:

IoC is a design principle where the responsibility of managing object creation, object lifecycles, dependency graphs, and configuration is delegated to a container capable of constructing and composing components dynamically.

Objects no longer control their own creation. A container does.

Spring achieves IoC primarily through Dependency Injection (DI) and the IoC Container (ApplicationContext).


1. What IoC Really Means (Beyond Beginner Definition)

IoC is often explained as “framework calls your code instead of you calling it.”
That’s correct but incomplete.

Definition:

IoC is a design principle where the responsibility of managing object creation, object lifecycles, dependency graphs, and configuration is delegated to a container capable of constructing and composing components dynamically.

Spring’s ApplicationContext is that container.

IoC reduces coupling, removes explicit instantiation (new), and enables late binding, configurability, bean lifecycle management, and testability.


2. Pre-IoC: Manual Wiring (Example)


public class App {
    public static void main(String[] args) {
        DatabaseConnection conn = new DatabaseConnection();
        UserRepository repo = new UserRepository(conn);
        UserService service = new UserService(repo);

        service.start();
    }
}

Issues:

  • Manual instantiation order
  • Hard-coded dependencies
  • Not configurable
  • No lifecycle hooks
  • Impossible to swap implementations without touching code

This approach doesn’t scale past trivial applications.


3. With Spring IoC: Declarative Composition


@Service
public class UserService {
    private final UserRepository repo;

    public UserService(UserRepository repo) {
        this.repo = repo;
    }
}


@Repository
public class UserRepository {
    private final DatabaseConnection connection;

    public UserRepository(DatabaseConnection connection) {
        this.connection = connection;
    }
}


@Component
public class DatabaseConnection { }

Here:

  • You declare dependencies
  • Spring resolves, instantiates, and injects them
  • Your code becomes agnostic of construction logic

This is a foundational shift: from imperative wiring to declarative composition.


4. IoC Container (ApplicationContext) Deep Dive

The IoC container is responsible for:

✔ Component scanning

Discovering candidate beans annotated with:

@Component, @Service, @Repository, @Controller, @Configuration, etc.

Note: @Bean methods inside @Configuration classes are not scanned but are exec-defined bean factories.


✔ Bean instantiation

Creating bean instances in an order defined by their dependency graph.

Spring determines constructor requirements, resolves dependencies, validates cycles, and respects bean scopes.


✔ Dependency Injection

Injection strategies:

  • Constructor Injection (recommended, immutable, test-friendly)
  • Field Injection (discouraged)
  • Setter Injection (optional dependencies)

Spring resolves the dependency tree recursively.


✔ Lifecycle management

Bean lifecycle phases include:

  • Instantiation
  • Dependency injection
  • @PostConstruct callbacks
  • Proxy creation (AOP, transactional beans, security)
  • Destruction callbacks (@PreDestroy)

✔ Caching & Scoping

Default scope: Singleton per ApplicationContext
Others: prototype, request, session, application, websocket.


5. What Exactly Is “Inverted”?

Before IoC:

  • You control object creation
  • You orchestrate dependency order
  • You decide lifecycle
  • Application code depends on infrastructure code

After IoC:

TraditionalWith IoC
You create objectsContainer creates objects
You resolve dependenciesContainer injects dependencies
You manage lifecycleContainer manages lifecycle
You call frameworkFramework calls your components
Hard wiringDeclarative configuration

IoC is fundamentally a shift of control from application code → container.


6. Spring Boot Startup Flow (Detailed)

A senior engineer should understand the full bootstrapping sequence:

  1. Bootstrap ApplicationContext
    Prepare environment, load configurations, evaluate conditions.
  2. Component Scan & Bean Discovery
    Build a registry of bean definitions.
  3. Bean Factory Post Processing
    Apply BeanFactoryPostProcessor, PropertySources processing, @Configuration class enhancement, etc.
  4. Bean Instantiation
    Create beans according to dependency graph and scopes.
  5. Dependency Injection
    Constructor injection → populate prototype dependencies → wrap proxies if needed.
  6. Lifecycle Callbacks
    Invoke BeanPostProcessor, AOP proxy weaving, @PostConstruct, lifecycle interfaces.
  7. Application Ready Event
    Context available; web server starts; request pipeline becomes active.
  8. Handling Requests
    Controller beans are reused (singleton); dependencies are injected once.

7. Why IoC Matters (Senior-Level Benefits)

✔ Testability

Mocking dependencies becomes trivial.

✔ Scalability

Large apps with hundreds of beans scale via declarative wiring.

✔ Pluggability

Swap implementations without changing business logic.

✔ Extensibility

Use conditional beans, profiles, automatic config.

✔ Separation of Concerns

Construction logic is removed from application logic.


Conclusion

IoC is the backbone of the Spring ecosystem.
It enables declarative programming, reduces coupling, and allows Spring to manage object graphs, lifecycle, proxies, scoping, and configuration.

In short:

You write the components.
Spring decides how they’re created, wired, managed, and executed.

Picture of thanhdoancong

thanhdoancong

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

Scroll to Top