Understanding Dependency Injection and Autowiring in Spring Boot
When preparing for Spring Boot interviews, one of the most commonly asked and fundamental topics is Dependency Injection (DI) and how Autowiring works. These concepts are central to how Spring Framework operates, promoting loose coupling and better testability.
1. What is Dependency Injection (DI)?
Dependency Injection is a design pattern used to implement IoC (Inversion of Control), where the control of creating and managing objects is transferred from the class itself to the Spring container.
In simpler terms, instead of creating objects using new
, Spring injects the required dependencies into your class.
@Component
public class BookService {
private final BookRepository bookRepository;
public BookService(BookRepository bookRepository) {
this.bookRepository = bookRepository; // Dependency is injected here
}
}
2. Types of Dependency Injection in Spring
- Constructor Injection (Recommended)
- Setter Injection
- Field Injection (Not recommended for testability)
Constructor Injection Example:
@Service
public class UserService {
private final UserRepository repo;
public UserService(UserRepository repo) {
this.repo = repo;
}
}
3. What is Autowiring?
Autowiring is a feature in Spring that allows Spring to automatically resolve and inject collaborating beans into your class without using explicit new
keyword or @Bean
method everywhere.
You can use:
@Autowired
@Inject
(Javax)@Resource
(JSR-250)
Autowiring Example:
@Component
public class NotificationService {
@Autowired
private EmailService emailService; // Automatically injected by Spring
}
4. Best Practices for Dependency Injection
- Use constructor injection for required dependencies.
- Avoid field injection because it makes unit testing harder.
- Use
@Autowired
only on one constructor to keep things clean (or omit it entirely as of Spring 4.3+). - Always prefer interfaces for injected types to make code more testable and loosely coupled.
5. Common Interview Questions
- What is the difference between field injection and constructor injection?
- Why is constructor injection preferred?
- Can you inject multiple beans of the same type? How?
- What happens if no qualifying bean is found for
@Autowired
? - What is the use of
@Qualifier
?
6. Bonus: Using @Qualifier
If multiple beans of the same type exist, use @Qualifier
to resolve the conflict.
@Component("smsService")
public class SmsService implements MessageService { }
@Component("emailService")
public class EmailService implements MessageService { }
@Autowired
@Qualifier("emailService")
private MessageService messageService;
7. Conclusion
Dependency Injection and Autowiring are core to writing scalable, testable, and maintainable Spring Boot applications. Mastering them is essential for clearing backend interviews.
Understand how the Spring container works, prefer constructor-based injection, and know how to handle multiple beans using @Qualifier
.
Follow InterviewYatra.com for more posts like this and stay interview-ready!
0 Comments