Understanding Spring IoC (Inversion of Control) and Dependency Injection (DI)
This article explains the concepts of Inversion of Control (IoC) and Dependency Injection (DI) in the Spring framework, describing how containers manage object creation, reduce coupling, improve testability, and embody the Hollywood principle for more flexible Java backend development.
Anyone who has studied the Spring framework has heard of IoC (Inversion of Control) and DI (Dependency Injection), but beginners often find these concepts vague; this article shares insights from various experts and the author’s own understanding of Spring IoC.
1. What is IoC
IoC – Inversion of Control – is not a technology but a design philosophy; in Java it means handing over the objects you design to a container rather than creating them directly inside your classes.
Who controls whom, and what is controlled: In traditional Java SE programs, objects are created with new inside the class (the program actively creates dependencies). With IoC, a dedicated container creates these objects, controlling their lifecycle and external resource acquisition.
Why it is a reversal and what is reversed: Traditionally the program actively obtains dependencies (the “forward” direction). With IoC the container reverses this by locating and injecting dependencies, so the program passively receives them.
2. What IoC can do
IoC is not a concrete technology but a principle that guides the design of loosely‑coupled, high‑quality software. By moving object creation and lookup to the container, coupling is reduced, testing becomes easier, components become reusable, and the overall architecture becomes more flexible.
The biggest change is not in code but in mindset: the application no longer actively creates resources; it waits for the IoC container to provide them, embodying the Hollywood rule “Don’t call us, we’ll call you.”
3. IoC and DI
DI – Dependency Injection – means that the container decides the dependency relationships at runtime and injects the required resources into components.
The purpose of DI is to increase component reuse and build a flexible, extensible platform, allowing developers to configure required resources without writing code to locate them.
Key points to understand:
Who depends on whom: the application depends on the IoC container.
Why the dependency is needed: the application needs the container to supply external resources.
Who injects whom: the IoC container injects the required objects into the application.
What is injected: objects, resources, or constant data needed by the application.
IoC and DI describe the same idea from different angles; Martin Fowler coined the term “Dependency Injection” in 2004 to clarify that the container injects dependencies into objects.
4. Significance of IoC and DI
In typical Java development without Spring, each object creates its collaborators with new , leading to tight coupling and hard‑to‑test code.
With Spring, the container creates collaborator objects, stores them, and supplies them to dependent objects when needed, freeing the business code from creation details.
Thus, control over object creation is transferred to the container, turning the container into a factory that manages object lifecycles and dependencies.
DI is essentially another name for IoC, emphasizing that the way of obtaining dependencies is reversed.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.