@Autowired tells Spring to supply a dependency for you. When Spring creates a bean, it looks at each member marked with @Autowired, finds a matching bean in the application context, and injects it, so your classes never call new on their collaborators or look them up manually.
It is the core annotation behind dependency injection in Spring Framework and Spring Boot. This article explains how it resolves beans, what happens when none or several match, and how to control the choice. For a comparison of constructor, setter and field injection, see the related answer on @Autowired in Spring.
The annotation lives in org.springframework.beans.factory.annotation. An AutowiredAnnotationBeanPostProcessor, registered automatically by component scanning and Spring Boot, inspects every bean for @Autowired members after the bean is instantiated.
Resolution follows these steps:
@Primary, then @Qualifier, then a bean name that equals the field or parameter name.NoSuchBeanDefinitionException. If several still match, it fails with NoUniqueBeanDefinitionException.Failing at startup is useful. A missing or ambiguous dependency is found when the application boots, not in production on the first request.
NotificationService depends on the MessageSender interface, not on EmailSender. Replacing the implementation, or using a fake one in a test, needs no change to the service.
Since Spring Framework 4.3, a class with a single constructor does not need @Autowired on it. Spring uses that constructor automatically, so you can remove the annotation from the example above.
| Injection point | Example | Notes |
|---|---|---|
| Constructor | @Autowired Service(Repo r) | Recommended, supports final fields |
| Setter or method | @Autowired void setRepo(Repo r) | Good for optional or reconfigurable dependencies |
| Field | @Autowired private Repo repo; | Short, but hard to test and hides dependencies |
Constructor injection makes dependencies explicit and lets you create the object in a plain unit test without a Spring container.
When two beans implement the same interface, type matching alone is ambiguous. Add a second implementation and Spring needs guidance:
@Primary marks the default candidate, which defaultSender receives. @Qualifier("sms") selects a specific bean by name. A qualifier wins over @Primary at the injection point where it is used.
By default @Autowired is required. You have several ways to make a dependency optional:
Spring can also inject every bean of a type:
A List gets all MessageSender beans, and a Map<String, MessageSender> gets them keyed by bean name. Use @Order to control list order.
@Inject (jakarta.inject.Inject) is the standard equivalent. It has no required attribute and works the same in most cases.@Resource (jakarta.annotation.Resource) resolves by name first, then by type.@Value injects configuration values, not beans.Spring Boot 3 and Spring Framework 6 use the jakarta.* packages. Older tutorials that import javax.inject or javax.annotation need updating.
NoSuchBeanDefinitionException: the class is not a bean. Add @Component, @Service or a @Bean method, and make sure it is inside a scanned package.NoUniqueBeanDefinitionException: more than one candidate. Add @Primary or @Qualifier.NullPointerException on an @Autowired field: the object was created with new, so Spring never injected it. Let Spring create it, or inject it into the class that needs it.spring.main.allow-circular-references.Not if the class has one constructor, since Spring Framework 4.3. With several constructors, mark the one Spring should use with @Autowired.
No. Spring injects instance members only, so a static field stays null. Inject through a non-static setter instead if you must.
@Bean defines a bean, @Autowired consumes one. A @Bean method can also take its dependencies as parameters, and Spring supplies them the same way.
Yes, because Spring uses reflection. It still makes testing harder, which is why constructor injection is preferred.
@Autowired lets the Spring container supply dependencies by type, with @Primary and @Qualifier resolving ambiguity and required = false, Optional or ObjectProvider for optional ones. Prefer constructor injection, depend on interfaces, and treat startup failures for missing or ambiguous beans as the feature working as intended.