In Spring Boot, @Autowired marks a dependency injection point: Spring resolves a matching bean from the application context and supplies it to a Spring-managed object. It does not create a bean or inject dependencies into every object in your program. If you add it to an object created with new, Spring ordinarily will not process that object.
What Spring does when it encounters @Autowired
Spring’s annotation-processing infrastructure examines managed beans and resolves dependencies for annotated constructors, fields, or methods. In a standard Boot application, @SpringBootApplication includes component scanning, which discovers eligible components within its scan scope; those components can then be registered as beans. Annotation processing applies to beans in the relevant application context, not arbitrary Java objects. See the Spring Boot guide to beans and dependency injection and the Spring Framework reference for @Autowired.
As an Amazon Associate I earn from qualifying purchases.
For a field injection point, Spring first constructs the bean, then populates the field before invoking configuration methods. For an annotated method, Spring resolves each argument from the context and calls the method; the method may be a setter or another method with dependency arguments. The annotation is interpreted by Spring’s bean post-processing infrastructure, including AutowiredAnnotationBeanPostProcessor.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Where you can use it
| Injection point | When the dependency is supplied | What it means for the class |
|---|---|---|
| Constructor | While Spring creates the bean | Required collaborators are explicit at construction; fields can be final. |
| Field | After construction and before configuration methods | The dependency is not supplied through the constructor, so the object is not fully wired immediately after construction. |
| Setter or other method | When Spring invokes the annotated method | Can suit an optional dependency or one that may be reconfigured later. |
Spring supports all three locations. The choice affects when a collaborator becomes available and how clearly the class communicates whether that collaborator is required.
#1 Best Overall
Why a constructor often needs no @Autowired
If a Spring-managed class has just one constructor, Spring uses it for dependency injection even when it has no @Autowired annotation. That makes a single constructor a concise way to declare required collaborators:
@Service
class BillingService {
private final PaymentGateway gateway;
BillingService(PaymentGateway gateway) {
this.gateway = gateway;
}
}
Spring supplies a matching PaymentGateway bean when it creates BillingService. With multiple constructors, constructor-selection rules matter; marking the intended constructor with @Autowired guides Spring’s choice. For the exact rules, see the Framework reference.
Rank #2
Constructor injection is generally recommended for required dependencies because it makes them visible at the object boundary, supports immutable components, and ensures they are supplied during construction. Spring Boot likewise recommends constructor injection and component scanning in its dependency-injection guidance. A field injection example is valid, but not required:
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstall@Service
class BillingService {
@Autowired
private PaymentGateway gateway;
}
This form relies on Spring populating the field after the instance has been constructed. It can obscure a required dependency from callers and complicate creating a fully initialized instance outside Spring.
Rank #3
What happens if Spring cannot resolve the dependency?
By default, @Autowired injection is required. If Spring cannot find a suitable bean for a required injection point, bean creation or application-context startup commonly fails. If more than one candidate matches, Spring needs a way to select one.
- No matching bean: Check that an implementation is registered as a component within the component-scan scope, declared with
@Bean, and available in the same application context as the injection point. - Several matching beans: Use a qualifier to identify the intended candidate, or designate a primary candidate. Spring can also inject collections or arrays of matching beans; it can inject a map keyed by bean name when the map’s key type is
String. - Factory method type is too broad: If a bean is declared by a factory method, its declared return type needs to expose a sufficiently specific type for Spring to match it to the injection point.
- The object was made with
new: Spring does not ordinarily apply bean post-processing to an instance it does not manage. Register or obtain the object through the Spring context if it needs container-managed injection.
These resolution behaviors and supported injection forms are described in the Spring Framework @Autowired reference.
Rank #4
How to express an optional dependency
Do not mark a genuinely required collaborator optional merely to get past a startup error. For supported field and method injection points, @Autowired(required = false) makes the injection non-required; a non-required method is skipped if Spring cannot resolve any of its arguments. Spring also documents Optional, nullable parameter annotations, and Kotlin nullability for expressing optionality. Constructor arguments have distinct resolution semantics, so choose an explicit optional form rather than assuming the field or method rule applies unchanged.
Recommended Free Tools
Setter or other method injection can be reasonable when the dependency is truly optional or when later reconfiguration is useful. Spring’s dependency-injection guide recommends constructor injection for required collaborators and describes setter injection as appropriate for optional dependencies with reasonable defaults.
Why an @Autowired field might be null
A null field most often means the instance was not processed as a Spring bean, or that the injection setup does not include the expected bean. Check these points:
- Confirm how the object is created. If your code uses
newto create the class, Spring ordinarily does not inject its fields. Use a bean managed by the application context. - Check registration and scan scope. Verify that the class is an eligible Spring component or is declared with
@Bean, and that it falls within the application’s component-scan scope. - Check the dependency and context. Ensure a matching implementation is registered in the same application context, and resolve multiple candidates with a qualifier or primary designation.
- Check configuration and versions. Boot’s standard application setup enables the usual annotation-processing infrastructure. In non-Boot or XML-based configurations, annotation configuration or component scanning must enable the relevant processors. For version-specific behavior, check the documentation matching your Spring Boot and Spring Framework versions.
Version scope
The linked Boot reference is the current unversioned documentation; the linked Framework reference identifies Spring Framework 7.0.9. The behavior described here is the general dependency-injection model, not a claim that every configuration detail is identical across every Spring generation. When diagnosing a particular application, use documentation for its Boot and Framework versions and inspect its scan boundaries, bean declarations, qualifiers, and injection-point types.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




