📦 Spring Boot IoC Container
🧠 What is IoC Container?
The IoC (Inversion of Control) Container is the core part of Spring that:
- ✅ Creates your objects (beans)
- ✅ Manages them
- ✅ Injects their dependencies
- ✅ Controls their lifecycle
You don’t manually create or connect objects — Spring does it for you.
🔁 Inversion of Control?
Normal (manual) object creation:
MyService service = new MyService();
Spring Boot style (IoC-managed):
@Component
public class MyService { }
@Autowired
MyService service; // Spring injects this
This is Inversion of Control — you give up control to the container.
📦 In Spring Boot
Spring Boot automatically starts the IoC container when your application runs:
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args); // starts IoC container
}
}
Under the hood, this creates an ApplicationContext, which is the Spring IoC container.
✅ What IoC Container Does
| Task | Example |
|---|---|
| Finds beans | @Component, @Service, etc. |
| Creates beans | Using constructors |
| Injects dependencies | @Autowired, constructor injection |
| Manages lifecycle | @PostConstruct, @PreDestroy |
📌 One Line Summary
The IoC Container in Spring Boot is the system that automatically finds, creates, connects, and manages your objects (beans).
Comments
Post a Comment