@Autowired Annotation in Spring Explained
Fast recall
Quick revision
Core answer
Whether you understand Spring's core dependency injection mechanism — that the container creates and wires objects for you, and resolves @Autowired by type first.
Believing @Autowired matches by field name rather than by type first, or not realizing it works on constructors, setters, and fields — and that field injection breaks plain unit tests.
Explain that constructor injection is preferred because it enables final fields and immutability, makes dependencies explicit in the signature, and lets you unit test without the Spring context.
Interview-ready guide
Interview answer
@Autowired is handled by Spring while it creates a bean. On a constructor parameter such as PaymentGateway, Spring finds assignable bean candidates. One candidate is injected directly. If there are several, @Qualifier can narrow them and @Primary can mark the ordinary default. If no valid result remains, the bean cannot be created and startup fails. The top-level error is commonly UnsatisfiedDependencyException, with NoSuchBeanDefinitionException or NoUniqueBeanDefinitionException deeper in the cause chain. A single constructor is selected without adding @Autowired.
@Autowired Annotation in Spring Explained
Direct Answer
@Autowired marks an injection point that Spring should satisfy from matching beans. Candidate selection starts by type, then considers qualifier metadata and priorities such as @Primary; an injection-point name can be a fallback. A required dependency that cannot be resolved causes context creation to fail, often through an UnsatisfiedDependencyException whose cause explains whether the bean was missing or ambiguous.
Code Example
@Service
public class OrderService {
private final CustomerRepository customerRepo; // required, immutable
private PaymentGateway paymentGateway; // optional
// Constructor injection — preferred for required dependencies.
// Since Spring 4.3, @Autowired is optional on a single constructor.
@Autowired
public OrderService(CustomerRepository customerRepo) {
this.customerRepo = customerRepo;
}
// Setter injection — used for optional dependencies.
@Autowired
public void setPaymentGateway(PaymentGateway gateway) {
this.paymentGateway = gateway;
}
public void processOrder(Long customerId, Order order) {
Customer customer = customerRepo.findById(customerId);
paymentGateway.charge(customer, order.getTotal());
}
}
@Repository
public class CustomerRepository {
public Customer findById(Long id) { /* JDBC logic */ return null; }
}
Common Mistakes & Pitfalls
- Mixing the new keyword with @Autowired: Creating a service with
new OrderService()bypasses Spring entirely — Spring never injects dependencies into objects you instantiate yourself. Let Spring create the bean by marking it@Serviceor@Component. - Autowiring into static fields: Spring's injection works on instance members only. Static fields belong to the class, not to a Spring-managed instance, so
@Autowiredon a static field silently stays null. - Forgetting the stereotype on the injected class: If
CustomerRepositoryisn't annotated with@Repositoryor@Component, Spring never creates a bean for it, and@AutowiredthrowsNoSuchBeanDefinitionExceptionat startup.
Check your understanding
Answer these without looking back at the article.
- How does Spring resolve @Autowired when there are multiple implementations?
- What is @Autowired(required=false)?
- What is the difference between @Autowired and @Inject?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.