Chapter 2.1☕ 16 min read

Beans and IoC Container

The IoC Container is Spring's brain. Understand this, and you understand Spring.

01The Concept: Inversion of Control

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.

02What is a Bean and the Application Context?
  1. Bean: A simple Java object that is created, managed, and destroyed by the Spring IoC Container.
  2. IoC Container: Spring's brain. It looks at which class needs which other class, and automatically wires (connects) them.
  3. 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.

03Full Working Code: The Bean Bucket

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()));
}
}
04The Java Code (Application.java)
@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.

05Why It Matters / Interview Note

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