The @Autowired annotation tells Spring to resolve a dependency from the application context and inject it automatically. You put it on a constructor, setter or field, and Spring finds a bean of the matching type, so your class does not create or look up its collaborators itself.
In current Spring, constructor injection is the recommended form, and when a class has a single constructor you do not even need @Autowired. This article explains how it works, the three injection styles and how to resolve conflicts.
Spring is an inversion of control container. Instead of writing new PaymentGateway() inside a service, you declare the dependency and let the container supply it. @Autowired marks an injection point. At startup Spring looks for a bean whose type matches, and injects it. If no bean matches, or several match without a clear choice, the context fails to start with a NoSuchBeanDefinitionException or NoUniqueBeanDefinitionException.
This keeps classes loosely coupled and makes testing easier, because you can pass a mock or a fake through the constructor.
Since Spring 4.3, a class with exactly one constructor needs no @Autowired. The fields can be final, the object can never exist half-initialised, and it works in plain unit tests without Spring.
Use setters for optional or reconfigurable dependencies.
It is short but not recommended in production code. The fields cannot be final, the dependencies are hidden, and the class cannot be built in a test without reflection or a Spring context.
| Style | Immutable fields | Easy to test | Recommended |
|---|---|---|---|
| Constructor | Yes | Yes | Yes, default choice |
| Setter | No | Yes | For optional dependencies |
| Field | No | No | Avoid |
When two beans share a type, Spring cannot choose. You can mark one as the default with @Primary, or pick one by name with @Qualifier.
Here CheckoutService gets PayPalGateway, while other injection points get the @Primary StripeGateway. As a fallback Spring also matches the parameter name to the bean name when the code is compiled with parameter names, which Spring Boot's build plugins do by default.
By default a missing dependency is an error. To make it optional use @Autowired(required = false), Optional<T> or @Nullable.
You can also inject all beans of a type as a List<Validator> or a Map<String, Validator>, which is handy for plugin-style designs. Order the list with @Order or Ordered.
NoSuchBeanDefinitionException: the class is not a bean. Check that it has a stereotype such as @Component and sits in a scanned package.NoUniqueBeanDefinitionException: use @Primary or @Qualifier.@Autowired on objects created with new: Spring only injects into beans it manages.Note that @Resource and @Inject are alternatives. @Inject comes from Jakarta Inject (jakarta.inject.Inject in Spring 6 and later) and behaves much like @Autowired, without a required flag.
No. If a class has a single constructor, Spring uses it automatically. With several constructors, annotate the one to use.
It hides dependencies, prevents final fields and makes unit tests harder. Constructor injection solves all three.
@Autowired matches by type first, then by name. @Resource matches by name first. @Autowired is Spring-specific, and @Resource is a Jakarta standard annotation.
Not directly. Spring does not inject into static fields. Inject into an instance member and read it from there.
@Autowired tells Spring where to inject a bean. Prefer constructor injection, which needs no annotation when there is one constructor, resolve conflicts with @Primary and @Qualifier, and mark optional dependencies explicitly. Avoid field injection in application code.