Difficulty: Beginner
What is Inversion of Control, and how is Dependency Injection related to it in Spring?
Let me explain this the way I wish someone had explained it to me in my first year. Normally when your class needs a collaborator, it creates one itself: an OrderService does new PaymentGateway(). That looks harmless, but now OrderService is welded to one specific gateway. You cannot swap it for a test double, you cannot switch from Razorpay to Stripe without editing OrderService, and you own the object's entire lifecycle. Inversion of Control flips that responsibility. Instead of your code controlling how dependencies are created, an external container controls it and hands them to you.
The everyday analogy is a restaurant. In a home kitchen you go shopping, chop, and cook. In a restaurant you tell the waiter what you want and the meal arrives. You have inverted control of preparation to someone else. IoC is the principle; Dependency Injection is the most common technique that implements it. The container, called the ApplicationContext in Spring, builds the objects, called beans, figures out who needs whom, and passes the dependencies in, typically through the constructor.
Why does this matter in practice? First, loose coupling: OrderService depends on a PaymentGateway interface, not a concrete class, so implementations can change without touching it. Second, testability: in a unit test you pass a mock gateway to the constructor and never touch the network. Third, centralised lifecycle management: the container decides scope, initialisation, destruction, and can wrap beans with proxies for transactions or security. That last point is what makes features like @Transactional possible at all.
How does Spring know what to create? During startup it scans for classes annotated with stereotypes such as @Component and @Service, reads @Bean methods from @Configuration classes, and builds BeanDefinitions, which are recipes for each bean. It then instantiates them in dependency order and injects collaborators by type. If two beans of the same type exist, injection becomes ambiguous and you resolve it with @Qualifier or @Primary. If none exist, the application fails fast at startup with a NoSuchBeanDefinitionException, which is a feature: you find wiring mistakes before deployment rather than at runtime.
Interviewers like to probe the difference between IoC and DI. IoC is the broad design principle where a framework calls your code or supplies your objects, sometimes called the Hollywood principle: do not call us, we will call you. DI is a specific pattern within it. Another IoC form is the template method or event callbacks, which is why a servlet container calling your controller is also IoC. Be ready to mention that DI does not require Spring; you can hand-wire objects in a main method and still practise DI. Spring simply automates the wiring and lifecycle at scale.
One last edge case: DI is about objects with behaviour, not simple data. You do not inject a User entity or a DTO; those are created per request with new. Injecting everything blindly is a common beginner mistake and leads to bloated contexts.
// Tightly coupled: hard to test or swap
class OrderServiceBad {
private final PaymentGateway gateway = new RazorpayGateway();
}
// Loosely coupled: the container supplies the dependency
interface PaymentGateway { void pay(double amount); }
@Component
class RazorpayGateway implements PaymentGateway {
public void pay(double amount) { System.out.println("Paid " + amount); }
}
@Service
class OrderService {
private final PaymentGateway gateway;
public OrderService(PaymentGateway gateway) {
this.gateway = gateway;
}
public void checkout(double amount) { gateway.pay(amount); }
}
OrderService knows only the interface. Spring finds RazorpayGateway, creates it once, and passes it to the constructor.
IoC, Dependency Injection, Spring Container, Loose Coupling