@Autowired Required for Constructor Injection in Spring Boot
Fast recall
Quick revision
Core answer
Whether you know the modern Spring convention that removes annotation noise and keeps service classes clean.
Learners add @Autowired to every constructor out of habit, not knowing it's optional for a single-constructor class.
Mention Lombok @RequiredArgsConstructor as the idiomatic modern approach — it generates the constructor so you write zero boilerplate.
Interview-ready guide
Interview answer
A service with one constructor taking OrderRepository is injected automatically. If a class has a no-argument constructor and another dependency constructor, Spring may use the default constructor unless configuration/annotations indicate otherwise. Marking the intended constructor is clearer when several overloads are eligible. Constructor parameters themselves follow normal bean-candidate resolution.
@Autowired Required for Constructor Injection in Spring Boot
Direct Answer
Spring Framework, not Spring Boot specifically, can use a class's single constructor without @Autowired since Spring 4.3. With multiple constructors, @Autowired can identify candidates and its required flag affects selection; a usable primary/default constructor can also be chosen under Spring's rules. It is too broad to say multiple constructors always require exactly one @Autowired annotation.
Code Example
// Manual constructor - no @Autowired needed (single constructor)
@Service
class OrderService {
private final OrderRepository orderRepo;
private final EmailService emailService;
public OrderService(OrderRepository orderRepo, EmailService emailService) {
this.orderRepo = orderRepo; // Spring auto-wires this single ctor
this.emailService = emailService;
}
public void placeOrder(Order order) {
orderRepo.save(order);
emailService.sendConfirmation(order);
}
}
// Lombok version - even cleaner, the constructor is generated
@Service
@RequiredArgsConstructor // generates a constructor for final fields
class OrderServiceLombok {
private final OrderRepository orderRepo;
private final EmailService emailService;
}
// Multiple constructors - @Autowired is required on the one to use
@Service
class ProductService {
private final ProductRepository repo;
@Autowired // required because there are 2 constructors
public ProductService(ProductRepository repo) {
this.repo = repo;
}
public ProductService() { // fallback no-arg constructor
this.repo = new InMemoryProductRepository();
}
}
Common Mistakes & Pitfalls
- Adding @Autowired unnecessarily: Freshers annotate every constructor because they saw it in old tutorials. It's harmless but adds noise — the annotation is only needed with multiple constructors.
- Not knowing @RequiredArgsConstructor: Many write constructors by hand even when all fields are final. Lombok's @RequiredArgsConstructor generates it for free, removing boilerplate from every class.
- Forgetting the multiple-constructor rule: Add a second constructor for testing and Spring no longer knows which to use. With two constructors you must mark one with @Autowired.
Check your understanding
Answer these without looking back at the article.
- What happens when a class has multiple constructors and no @Autowired?
- What does @RequiredArgsConstructor do?
- Is @Autowired required on setter injection?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.