Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Adding @Autowired tells Spring to supply a matching dependency at that injection point—but only if Spring manages the object. In a typical Spring Boot application, eligible components are registered as beans through component scanning, and Spring resolves their dependencies while creating them. The annotation does not create a bean or inject dependencies into every Java object.

What happens when Spring processes @Autowired?

Spring’s annotation-processing infrastructure detects @Autowired on a managed bean and resolves the requested dependency from the application context. For a field, injection happens after the bean is constructed and before its configuration methods run. An annotated method can have any name and multiple parameters; Spring resolves each parameter and calls the method.

Spring Boot’s usual application setup enables the infrastructure for annotation-based injection. In a non-Boot or XML-based configuration, the relevant annotation configuration or component-scanning setup must also be enabled. See the Spring Framework reference on @Autowired and the Spring Boot guide to beans and dependency injection.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Where can you put @Autowired?

Injection point When the dependency is available Typical fit
Constructor When the object is constructed Required dependencies
Field After construction, before configuration methods Supported, but hides the dependency from the constructor
Setter or other method When Spring calls the method during bean configuration Optional dependencies or cases where method-based configuration is useful

Constructor injection

For a class with a single constructor, Spring uses that constructor even when it has no @Autowired annotation. A typical required dependency can therefore be expressed as:

@Service
class BillingService {
    private final PaymentGateway gateway;

    BillingService(PaymentGateway gateway) {
        this.gateway = gateway;
    }
}

The dependency is explicit at construction, and the field can be final. If a class has multiple constructors, constructor-selection rules apply; marking the intended constructor with @Autowired can guide Spring. Consult the annotation’s API documentation for the rules applicable to the Framework version in use.

Field injection

Field injection is supported, but the field is populated only after the object has been constructed. For example:

@Service
class BillingService {
    @Autowired
    private PaymentGateway gateway;
}

This makes the dependency less visible in the class’s constructor and prevents assigning it as a final field. It also means an instance created outside Spring will not receive this injection automatically.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Setter and other method injection

Spring can inject dependencies through an annotated setter or another method with dependency parameters. Method injection can suit a genuinely optional collaborator or configuration that should be applied through a method. A method may take multiple dependencies, and Spring must resolve each required argument before calling it.

Why is an @Autowired field null?

The first question is whether Spring created and manages the object. An instance created directly with new is outside Spring’s ordinary bean lifecycle, so Spring does not automatically process its injection annotations. Injection also depends on annotation processing being enabled in the relevant application context.

  • The class is not a bean: Register it as a component within the applicable component-scan scope, or declare it with a @Bean method.
  • The instance was created manually: Obtain the managed bean from Spring rather than constructing a separate instance with new.
  • The dependency is in another context or outside the scan boundary: Check that the bean is declared and visible in the same application context as the injection point.
  • The declared type is too broad: When a factory method declares a broad return type, Spring may not recognize a specific implementation as a match. Use sufficiently specific type metadata.

For a concrete diagnosis, check the application’s Spring Boot and Framework versions, component-scan boundaries, bean declarations, and injection-point types.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What if Spring cannot resolve the dependency?

No matching bean

Required injection is the default. If Spring cannot find a suitable bean, bean creation or application-context startup commonly fails. Confirm that an implementation is registered, included in component scanning or declared with @Bean, and visible in the relevant context.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More than one matching bean

If several beans match a single dependency, resolve the ambiguity with a qualifier or designate a primary candidate. If the intent is to receive every matching bean, Spring can inject an array or collection; it can also inject a map keyed by bean name when the map’s key type is String.

Optional dependencies

@Autowired(required = false) is supported on fields and methods. For a non-required method, Spring skips the method if any argument cannot be resolved. Spring also documents Optional, nullable parameter annotations, and Kotlin nullability as ways to express optionality. Constructor arguments have distinct resolution rules, so do not assume that setting required = false makes a constructor dependency optional in the same way as a field or method.

Which injection style should you choose?

Spring generally recommends constructor injection for required collaborators: it makes them explicit, supports immutable components, and ensures they are supplied as the object is created. The Spring Framework dependency-injection guide describes setter injection as a better fit for optional dependencies that can have reasonable defaults. Use field injection when you have a specific reason for it, rather than assuming it is required by Spring Boot.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.