Multiple Injection Types in a Single Spring Bean
Fast recall
Quick revision
Core answer
Whether you know you can match the injection style to each dependency's role rather than picking one for the whole class.
Thinking you must choose a single injection type for the entire class.
Recommend constructor (required) plus setter (optional) and explain the constructor-then-setter resolution order.
Interview-ready guide
Interview answer
For example, ReportService can require ReportRepository in its constructor and accept an optional AuditSink through ObjectProvider or an optional setter. generate() must behave correctly when no AuditSink exists. @PostConstruct can validate the final populated state, but it should not repair an ambiguous dependency contract.
Multiple Injection Types in a Single Spring Bean
Direct Answer
A Spring bean can use constructor injection together with annotated fields or methods. Use the constructor for dependencies required to create a valid object. An optional method/setter can be used when the object has a clear fallback. Do not make business logic depend on a supposed universal order between field and setter injection; those are post-processing details, while initialization callbacks run after dependency population.
Code Example
@Service
public class CustomerService {
// Required dependencies - constructor injection
private final CustomerRepository repository;
private final ValidationService validator;
// Optional dependencies - setter injection
private CacheManager cacheManager;
private NotificationService notificationService;
// Constructor for required dependencies (single ctor, no @Autowired needed)
public CustomerService(CustomerRepository repository, ValidationService validator) {
this.repository = repository;
this.validator = validator;
}
@Autowired(required = false)
public void setCacheManager(CacheManager cacheManager) {
this.cacheManager = cacheManager;
}
@Autowired(required = false)
public void setNotificationService(NotificationService notificationService) {
this.notificationService = notificationService;
}
public Customer createCustomer(CustomerDto dto) {
validator.validate(dto); // required, always available
var customer = repository.save(dto.toEntity());
if (cacheManager != null) { // optional feature
cacheManager.put(customer.getId(), customer);
}
if (notificationService != null) { // optional feature
notificationService.sendWelcomeEmail(customer);
}
return customer;
}
}
Common Mistakes & Pitfalls
- Mixing without purpose: Freshers use different injection types across dependencies of similar importance, which hides what's required versus optional. Be consistent: constructor for required, setter for optional.
- Adding field injection to the mix: Spring allows it alongside constructor and setter, but it doesn't mean you should. Stick to constructor plus setter for clean, testable code.
- Not documenting the pattern: When mixing, add a short comment marking which dependencies are required versus optional, especially in a codebase with many contributors.
Check your understanding
Answer these without looking back at the article.
- What order does Spring process different injection types?
- Is it bad practice to mix constructor and field injection?
- How do you decide which dependencies are optional?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.