🔥 Launch Offer: Interview Preparation Bundle @ ₹2,499 ₹9,999 • 75% OFF. Offer expires in 🔥 Launch Offer: Interview Preparation Bundle @ ₹2,499 ₹9,999 • 75% OFF. Offer expires in Interview Preparation Bundle @ ₹2,499 ₹9,999

00:00:00 Get Bundle
home spring boot ioc-inversion-of-control-and-ioc-container-in-depth
Interview guide Spring Boot 18 sections

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…

Start reading
By javainterviewprep June 10, 2026 4 min read

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:

  • UserService is responsible for creating UserRepository.
  • 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:

  • UserService never creates UserRepository.
  • 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, BeanFactory and ApplicationContext are the two main IoC container interfaces, with ApplicationContext being the more feature-rich and commonly used one.”


Quick Revision

ConceptMeaning
IoC (Inversion of Control)A design principle where control of object creation and dependency management is transferred from application code to the framework.
IoC ContainerThe Spring-managed container that implements IoC by creating, configuring, managing, and supplying beans.
BeanFactoryThe basic IoC container interface with core bean management capabilities.
ApplicationContextThe 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 ApplicationContext is the advanced Spring IoC container that your Spring Boot application uses to manage beans.