What are IoC and dependency injection (DI) in Spring?

IoC (Inversion of Control) is a design principle in which the responsibility of creating and managing objects is transferred from the application code to a container or framework. DI (Dependency Injection) is the most common technique used to implement IoC by providing required dependencies from outside rather than creating them inside the class.

Key Points

Example

Consider a UserService class that requires a UserRepository. Instead of creating the UserRepository object using the new keyword, Spring automatically injects it into UserService. This makes the code more flexible and easier to test.

@Service
public class UserService {

    private final UserRepository userRepository;

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

Interview Tip

A concise interview answer is

IoC is a design principle where the Spring container manages object creation and lifecycle. DI is a technique used to achieve IoC by injecting required dependencies into a class instead of letting the class create them. This promotes loose coupling and improves maintainability.

Last updated: 11 November 2026

Previous question: What is the difference between Spring and Spring Boot?

Next question: What is the role of the IoC container in Spring?

More Spring Framework - Level I interview questions

All Spring Framework - Level I questions