Spring Boot
Article 5: Dependency Injection – The Heart of Spring Boot
By Utility Zone · 2026-01-27T18:30:24.609499
1. Introduction
If you understand Dependency Injection (DI), Spring Boot will stop feeling magical and start feeling logical.
Most confusion in Spring Boot comes from not understanding:
- How objects are created
- How objects talk to each other
This article fixes that.
2. The Problem Without Dependency Injection
Consider this example:
public class OrderController {
private OrderService orderService = new OrderService();
}
What’s Wrong Here?
- Controller is tightly coupled to
OrderService - Hard to test
- Hard to change implementation
- Violates good design principles
👉 This approach does not scale.
3. What Is Dependency Injection?
Dependency Injection means:
Objects do not create their dependencies. Dependencies are provided to them.
Instead of:
I create what I need
You say:
Give me what I need
Spring becomes the object factory.
4. Spring Container & Beans
4.1 What Is Spring Container?
- Manages object creation
- Manages object lifecycle
- Injects dependencies automatically
4.2 What Is a Bean?
A bean is simply:
An object managed by Spring
5. Creating Beans Using Annotations
5.1 @Component
@Component
public class EmailService {
public void send() {
System.out.println("Email sent");
}
}
Spring automatically:
- Creates this object
- Stores it in container
5.2 Specialized Annotations
| Annotation | Usage |
|---|---|
@Component | Generic |
@Service | Business logic |
@Repository | Database layer |
@Controller | MVC |
@RestController | REST APIs |
👉 All are beans.
6. Injecting Dependencies
6.1 Constructor Injection (Recommended)
@RestController
public class OrderController {
private final OrderService orderService;
public OrderController(OrderService orderService) {
this.orderService = orderService;
}
}
✔ Best practice
✔ Easy testing
✔ Immutable
6.2 Field Injection (Not Recommended)
@Autowired
private OrderService orderService;
❌ Hard to test
❌ Hidden dependencies
Avoid in real projects.
7. Using @Service Layer
@Service
public class OrderService {
public String placeOrder() {
return "Order placed successfully";
}
}
Controller:
@GetMapping("/order")
public String order() {
return orderService.placeOrder();
}
Spring automatically wires everything.
8. How Spring Finds Beans (Component Scanning)
Spring scans:
com.example.demo
and all sub-packages.
That’s why:
- Controllers must be under base package
- Services must be discoverable
9. Common Dependency Injection Errors
❌ NoSuchBeanDefinitionException
❌ Class outside scan path
❌ Missing annotation
❌ Multiple beans of same type
10. Multiple Implementations (Quick Intro)
public interface PaymentService {
void pay();
}
@Service
public class UpiPaymentService implements PaymentService { }
@Service
public class CardPaymentService implements PaymentService { }
Use:
@Qualifier("upiPaymentService")
We’ll cover this deeper later.
11. What You Should Understand Before Moving On
You should now clearly know:
- What Dependency Injection is
- What beans are
- Why constructor injection is best
- How Spring wires objects
12. What’s Next?
➡ Article 6: Configuration & Properties
- application.properties
- @Value
- @ConfigurationProperties
- Profiles
Type Next when you’re ready.