@Autowired tells Spring to inject a matching bean into a constructor, setter, field or method of another bean, so you do not create or look up dependencies yourself. The container resolves the dependency by type, using qualifiers or names to break ties, and fails at startup if it cannot find exactly one candidate (unless the dependency is optional).
It is the core of annotation-based dependency injection in Spring Framework 6 and Spring Boot 3. In modern code you use it mostly on constructors, and for a class with a single constructor you can omit it entirely.
Spring keeps beans in an ApplicationContext. When it creates a bean, it inspects the class for injection points. Each place marked with @Autowired is resolved by asking the container for a bean whose type is assignable to the declared type. The result is passed in for you, so classes depend on abstractions and the wiring lives in the container.
OrderService never calls new StripeGateway(). You can swap the implementation or pass a mock in a unit test without touching OrderService.
@Autowired works in three places. They are not equally good.
| Style | Where | Final fields | Easy to test without Spring | Recommended |
|---|---|---|---|---|
| Constructor | constructor parameters | yes | yes | yes, default choice |
| Setter | setter method | no | yes | for optional dependencies |
| Field | private field | no | no (needs reflection) | avoid in production code |
Since Spring 4.3, a class with exactly one constructor does not need @Autowired at all. Spring uses that constructor automatically. With records or Lombok's @RequiredArgsConstructor the same rule applies.
If a class has several constructors, mark the one Spring should use with @Autowired.
Setter injection suits dependencies that are optional or can be changed after construction. Field injection is the shortest to write but hides dependencies, prevents final fields and forces tests to use reflection or a Spring context. Most teams and the Spring documentation prefer constructor injection.
Resolution follows a fixed order:
@Primary.@Qualifier, or fall back to matching the parameter or field name against a bean name.NoUniqueBeanDefinitionException. If there is none, throw NoSuchBeanDefinitionException.Collections are a special case. Injecting a List<PaymentGateway> or Map<String, PaymentGateway> gives you every matching bean, which is handy for strategy-style designs.
When two beans share a type, tell Spring which one you want.
@Primary sets the default choice. @Qualifier picks a specific bean at one injection point and overrides the default.
By default a missing dependency stops the application from starting, which is what you want for mandatory collaborators. For optional ones you have three choices:
@Autowired(required = false) leaves the field or parameter null when no bean exists.Optional<T> as the parameter type gives an empty Optional when no bean exists.@Nullable on the parameter does the same with a plain null.| Error | Cause | Fix |
|---|---|---|
NoSuchBeanDefinitionException | No bean of that type is registered, often because the class is missing a stereotype annotation or sits outside the component scan | Add @Component, @Service or @Bean, and keep the class under the @SpringBootApplication package |
NoUniqueBeanDefinitionException | Several candidates | Use @Primary, @Qualifier or a more specific type |
BeanCurrentlyInCreationException | Circular dependency; Spring Boot rejects circular references by default since 2.6 | Redesign, extract a third bean, or inject with @Lazy as a last resort |
Field is null | The object was created with new, so Spring never injected it | Let the container create the object |
No. When a class has a single constructor, Spring injects it without the annotation. You only need @Autowired to choose between several constructors.
@Inject is the standard Jakarta annotation (jakarta.inject.Inject in Spring 6) and behaves like @Autowired without the required attribute. @Resource resolves by name first, then by type.
It hides dependencies, blocks final fields, allows a half-built object and makes unit tests depend on reflection or a Spring context.
No. Spring injects into instance members only. Static injection is ignored and is a design smell.
@Autowired makes Spring supply a bean's collaborators by type, so classes stay decoupled and testable. Prefer constructor injection (where the annotation is optional for a single constructor), use @Primary and @Qualifier to resolve ambiguity, and reserve required = false or Optional for truly optional dependencies.