Beans and IoC Container
The IoC Container is Spring's brain. Understand this, and you understand Spring.
The Hyderabad Tiffin Service Analogy:
Imagine you need lunch at your office every day.
Traditional Java Approach: You cook it yourself. You go to the market, buy vegetables, cook on the stove, and serve it. Here, you keep control of the entire process. But it takes a lot of time and effort.
Spring IoC Approach: You subscribe to a Hyderabad Tiffin Service. You don't care about the gas, vegetables, or utensils. At 11 AM, the tiffin arrives at your desk. You have inverted (handed over) the control to the Tiffin Service.
In Spring, the food the service provides is called a Bean. The organization that delivers it to your desk is the IoC Container. You don't create objects using the new keyword in your code; Spring's Container delivers the ready-made object (Bean) to you.
- Bean: A simple Java object that is created, managed, and destroyed by the Spring IoC Container.
- IoC Container: Spring's brain. It looks at which class needs which other class, and automatically wires (connects) them.
- Application Context: The modern interface of the IoC Container. When your Spring Boot app starts, it scans all beans and keeps them in a memory bucket.
If you create an object using the new keyword, Spring doesn't know about it — that object is not a Spring Bean.
Let's see how Spring creates and manages a bean without you writing new. We will inject the ApplicationContext directly to inspect the bucket.
package com.devinhyderabad;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
private OfficeWorker worker;
@Autowired
private ApplicationContext context;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
worker.eatLunch();
TiffinService service = context.getBean(TiffinService.class);
System.out.println("Same tiffin? " + (service == worker.getTiffinService()));
}
}@Component
class TiffinService {
public String serveFood() {
return "Here is your Hyderabadi Biryani and Tamatar ki Chutney!";
}
}
@Component
class OfficeWorker {
@Autowired
private TiffinService tiffinService;
public void eatLunch() {
System.out.println("Eating: " + tiffinService.serveFood());
}
public TiffinService getTiffinService() {
return tiffinService;
}
}When you run this, Spring creates TiffinService first, puts it in the Application Context, sees that OfficeWorker needs it, and injects it. No new TiffinService() was written by us! The final print statement prints true, proving Spring holds a single instance in its container.
Interview Question: "What is the difference between a POJO and a Spring Bean?"
Answer: A POJO (Plain Old Java Object) is just a Java object. It becomes a Spring Bean only when it is instantiated, assembled, and managed by the Spring IoC Container. If you create an object using the new keyword, Spring doesn't know about it, and it is NOT a bean.
Enterprise Note: By letting Spring manage objects, we achieve loose coupling. If tomorrow the TiffinService changes its internal logic, OfficeWorker doesn't care because Spring handles the delivery. This makes enterprise code highly testable and maintainable.
Key Takeaways
- ✅ IoC Container manages object lifecycle — no `new` keyword needed
- ✅ A Spring Bean is any object managed by the Spring IoC Container
- ✅ ApplicationContext is the modern IoC Container interface
- ✅ Loose coupling makes enterprise code testable and maintainable
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login