IoC (Inversion of Control) and IOC Container in Depth
Before understanding the IoC Container, let's first understand IoC. What is IoC? IoC (Inversion of Control) is a design principle where the control of creating and managing objects…
Before understanding the IoC Container, let’s first understand IoC.
What is IoC?
IoC (Inversion of Control) is a design principle where the control of creating and managing objects is transferred from the application code to the Spring Framework.
Normally, a class creates its own dependencies.
Without IoC
public class UserService {
private UserRepository repository = new UserRepository();
}
Here:
UserServiceis responsible for creatingUserRepository.- The application controls object creation.
- Classes become tightly coupled.
With IoC:
@Service
public class UserService {
private final UserRepository repository;
public UserService(UserRepository repository) {
this.repository = repository;
}
}
Now:
UserServicenever createsUserRepository.- Spring creates it.
- Spring injects it.
- Control has been inverted (transferred to Spring).
That’s why it is called Inversion of Control.
What is an IoC Container?
The IoC Container is the core component of Spring that implements the IoC principle.
It is responsible for:
- Creating objects (beans)
- Managing their lifecycle
- Injecting dependencies
- Configuring beans
- Destroying beans when the application shuts down
Simply put:
An IoC Container is a software container that creates, manages, and provides Spring beans throughout the application’s lifecycle.
Is IoC Container a Class?
No.
This is an important interview point.
The IoC Container is a concept, not a specific class.
Spring provides implementations of this concept.
The two main container interfaces are:
BeanFactory
ApplicationContext
Both are IoC containers.
IoC Container (Concept)
│
├── BeanFactory (Basic IoC Container)
│
└── ApplicationContext (Advanced IoC Container)
What Does the IoC Container Actually Do?
Suppose we have:
@Repository
public class UserRepository {
}
@Service
public class UserService {
private final UserRepository repository;
public UserService(UserRepository repository) {
this.repository = repository;
}
}
During startup, the IoC container performs these steps:
Step 1: Finds the classes
Using Component Scan:
@Service
@Repository
@Controller
Step 2: Registers Bean Definitions
It notes:
UserRepository
UserService
should be managed by Spring.
Step 3: Creates Objects
new UserRepository()
new UserService(...)
Step 4: Injects Dependencies
UserRepository
│
â–¼
UserService
Step 5: Stores Beans
The container keeps them internally.
IoC Container
---------------------
UserRepository Bean
UserService Bean
OrderService Bean
EmployeeService Bean
---------------------
Whenever another class asks for UserService, the container provides it.
Real-Life Analogy
Imagine a hotel.
Without IoC
Every guest builds their own room.
Guest
↓
Build Room
↓
Sleep
Very inefficient.
With IoC
The hotel already has rooms.
Guest
↓
Hotel Reception
↓
Assigned Room
The guest simply requests a room.
The hotel manages:
- Room creation
- Cleaning
- Maintenance
- Allocation
Similarly, the IoC Container manages:
- Bean creation
- Bean storage
- Dependency injection
- Bean lifecycle
Your application simply asks for a bean.
Where Does ApplicationContext Fit?
Many developers confuse these two.
The relationship is:
IoC Container (Concept)
│
Implemented by
│
ApplicationContext
Or more accurately:
IoC Container
│
├── BeanFactory
│
└── ApplicationContext
So,
- IoC Container = Concept
- ApplicationContext = Spring’s advanced implementation (via its interface and concrete classes)
- BeanFactory = Basic implementation
Complete Startup Flow
main()
│
â–¼
SpringApplication.run()
│
Creates
â–¼
ApplicationContext
│
(ApplicationContext is the IoC Container)
│
â–¼
Component Scan
│
â–¼
Finds Beans
│
â–¼
BeanFactory Creates Beans
│
â–¼
Dependency Injection
│
â–¼
Beans Stored in IoC Container
│
â–¼
Application Ready
Responsibilities of the IoC Container
The IoC Container is responsible for:
- Creating bean objects.
- Configuring beans.
- Managing bean lifecycle.
- Injecting dependencies.
- Managing bean scopes (Singleton, Prototype, Request, Session).
- Calling lifecycle callbacks (
@PostConstruct,@PreDestroy, etc.). - Providing beans to the application whenever required.
Interview Answer (1 Minute)
“The IoC (Inversion of Control) Container is the core component of Spring responsible for implementing the IoC principle. Instead of application classes creating and managing their own objects, the IoC container takes over that responsibility. It creates beans, configures them, injects dependencies, manages their lifecycle and scopes, and provides them whenever required. In Spring,
BeanFactoryandApplicationContextare the two main IoC container interfaces, withApplicationContextbeing the more feature-rich and commonly used one.”
Quick Revision
| Concept | Meaning |
|---|---|
| IoC (Inversion of Control) | A design principle where control of object creation and dependency management is transferred from application code to the framework. |
| IoC Container | The Spring-managed container that implements IoC by creating, configuring, managing, and supplying beans. |
BeanFactory | The basic IoC container interface with core bean management capabilities. |
ApplicationContext | The advanced IoC container interface that extends BeanFactory with features like component scanning, event handling, and property management. |
| Dependency Injection (DI) | The mechanism used by the IoC container to provide required dependencies to beans automatically. |
One-line interview summary
IoC is the principle, the IoC Container is the mechanism that implements that principle, and
ApplicationContextis the advanced Spring IoC container that your Spring Boot application uses to manage beans.