Spring Framework FAQ: Top Questions
10. What is the purpose of @Autowired in Spring?
@Autowired
is used to automatically inject dependencies by type. It works with constructors, fields, and setter methods.
📘 Modes:
- By Type (default)
- Optional injection with
required=false
- Qualifying beans using
@Qualifier
📥 Example:
@Autowired
private OrderService orderService;
🏆 Expected Output:
Spring injects an OrderService bean into this field.
🛠️ Use Cases:
- Injecting service or repository beans.
- Decoupling object creation from business logic.