In Spring, @Autowired marks a constructor, setter, method or field that the container should fill with a matching bean, which is how Spring wires your application's objects together. The practical question is where to put it, and the answer for new code is the constructor, because it gives you immutable, testable classes with visible dependencies.
This article compares the three injection styles with working code, and explains how Spring behaves when the dependency is missing, optional or ambiguous. It targets Spring Framework 6 and Spring Boot 3.
Constructor injection supplies all required dependencies when the object is created. The fields can be final, so the object is never half-initialised:
The example has no @Autowired at all. Since Spring Framework 4.3, a bean with exactly one constructor uses it automatically. Add @Autowired only when the class has several constructors, to choose the one Spring should call.
A unit test needs no Spring context:
Setter injection suits dependencies that can be absent or replaced after construction:
If no Clock bean exists, Spring skips the setter and the default stays. The drawback is that the object can be used before the setter runs, which is why setters are reserved for genuinely optional collaborators.
This is the shortest form and the most common one in old tutorials, but it has real costs:
final, so the object is mutable and may be partially initialised.@Autowired fields looks small but has too many responsibilities, and a constructor with ten parameters makes that obvious.Field injection still appears in test classes, where @Autowired on fields in a @SpringBootTest is normal.
| Aspect | Constructor | Setter | Field |
|---|---|---|---|
Immutable (final) fields | Yes | No | No |
| Plain unit test without Spring | Easy | Possible | Hard |
| Optional dependency | Via Optional or ObjectProvider | Natural | Via required = false |
| Visible number of dependencies | Yes | Partly | No |
| Circular dependency | Detected at startup | Possible | Possible |
Spring's own documentation recommends constructor injection for mandatory dependencies and setters for optional ones.
Spring matches by type first. If several beans fit, it prefers a @Primary bean, then a @Qualifier match, and finally a bean whose name equals the parameter name. That last fallback requires the compiler to keep parameter names, so build with the -parameters flag (Spring Boot's Maven and Gradle plugins configure this for you) and prefer an explicit @Qualifier over relying on names.
When no bean matches a required dependency, startup fails with NoSuchBeanDefinitionException, and with several matches and no tie-breaker it fails with NoUniqueBeanDefinitionException. The message names the type and the candidates, so the fix is usually clear.
You can keep constructor injection and still allow a missing bean:
ObjectProvider also resolves lazily, which helps with prototype beans and with breaking dependency cycles, though a redesign is a better cure.
It is optional with a single constructor. Many teams omit it to keep classes free of framework annotations, and Lombok's @RequiredArgsConstructor works the same way.
Constructor injection cannot create A before B when each needs the other. Spring Boot disallows cycles by default. Restructure the code or inject an ObjectProvider.
Yes. Any method annotated with @Autowired is called with resolved arguments after construction, which is useful for initialisation that needs several beans.
No. It is a supported, core annotation. What changed is the recommended style: constructor injection, with the annotation often implicit.
Use constructor injection for required dependencies, setter injection for optional ones, and avoid field injection in production classes. Let Spring resolve by type, add @Primary or @Qualifier for ambiguity, and use Optional or ObjectProvider when a bean may not exist.