Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk4 min

Spring Boot @Autowired: What It Does and When Injection Happens

@Autowired marks a dependency injection point in a Spring-managed bean. Learn when Spring supplies it, why a field can stay null, and when to use constructor injection instead.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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

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.

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.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
@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.

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.

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

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.

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

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:

  1. Confirm how the object is created. If your code uses new to create the class, Spring ordinarily does not inject its fields. Use a bean managed by the application context.
  2. 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.
  3. 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.
  4. 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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.