skip to content

What does @Autowired do, and what does it mean that Spring resolves it "by type"?

level: juniorimportance: must knowfreq 90%

answer

  1. inject by declared type
  2. interface param -> impl bean
  3. constructor > setter > field
  4. single constructor = annotation optional (4.3+)
  5. one candidate needed

basics

~10 s

@Autowired tells Spring to inject a dependency automatically. "By type" means Spring looks in its container for a bean whose type matches the field, constructor parameter, or setter parameter, and wires that bean in.

solid answer

~40 s

@Autowired marks an injection point — a constructor, setter, or field — that the Spring container should fill from its own beans. Resolution is primarily by type: Spring finds a bean assignable to the required type and injects it. It works with the declared type, so an interface parameter matches whichever bean implements it. Constructor injection is preferred (and since Spring 4.3 a single constructor is autowired without the annotation). If exactly one candidate matches, injection succeeds. Zero matching candidates or more than one both cause problems — a missing required bean fails startup, and multiple candidates are ambiguous unless narrowed. @Autowired is handled by an infrastructure bean post-processor, not by reflection you write yourself, so it happens automatically during bean creation.

code

java · 17 lines
java
public interface GreetingService { String greet(String name); }

@Service
class EnglishGreetingService implements GreetingService {
    public String greet(String name) { return "Hello, " + name; }
}

@Service
class Welcomer {
    private final GreetingService greetingService;

    // Single constructor -> @Autowired optional since Spring 4.3.
    // Resolved by type: the one GreetingService bean is injected.
    Welcomer(GreetingService greetingService) {
        this.greetingService = greetingService;
    }
}

go deeper

for a junior

Know it injects a matching bean by type and prefer constructor injection.

for a middle

Explain the single-constructor 4.3 rule and interface-to-impl matching.

for a senior

Tie 'by type' to the container search and name the two failure exceptions.

for a principal

Frame @Autowired as declarative wiring handled by an infrastructure post-processor and reason about type/generics matching.

### What @Autowired is `@Autowired` (package `org.springframework.beans.factory.annotation`) marks an **injection point** where the Spring IoC container should supply a collaborating bean. You can put it on a **constructor**, a **setter (or any method)**, or a **field**. It is Spring's own annotation (distinct from JSR-330 `@Inject`, covered by a sibling topic). ### "By type" resolution When Spring creates a bean, for each `@Autowired` point it takes the **declared type** of the target (field type / parameter type) and searches the `ApplicationContext` for a bean **assignable** to that type. Because it matches by type, you normally autowire an **interface** and Spring injects whatever concrete bean implements it: ```java @Autowired PaymentGateway gateway; // matches the single PaymentGateway bean ``` If the container holds exactly one bean of that type, it is injected. Generics are respected too: `@Autowired Repository<Order>` will not match `Repository<Customer>`. ### Constructor vs setter vs field - **Constructor injection** is recommended: dependencies are final, the object is fully initialized, and it is testable without Spring. Since **Spring 4.3**, if a class has a **single constructor**, `@Autowired` is optional there. - **Setter / method injection** allows optional or reconfigurable dependencies. - **Field injection** is concise but discouraged (hard to test, hides required deps). ### Who performs it You don't call anything. The infrastructure bean `AutowiredAnnotationBeanPostProcessor` scans each bean during creation and fills the injection points. It is registered automatically by `<context:annotation-config>`, component scanning, or a Spring Boot auto-config. ### The two failure modes - **No candidate**: a required `@Autowired` with zero matching beans fails **at startup** with `NoSuchBeanDefinitionException`. - **Multiple candidates**: two+ beans of the type are **ambiguous**; without a tie-breaker Spring throws `NoUniqueBeanDefinitionException`. ### When to use Use `@Autowired` (via constructor) for required collaborators between your own beans. It keeps wiring declarative rather than manual `new`.

  • Where can you put @Autowired?
    On a constructor, on a setter or any config method, or directly on a field. Constructor injection is preferred; field injection is discouraged because it hides dependencies and hampers testing.
  • Do you still need @Autowired on a constructor?
    Not if the class has exactly one constructor — since Spring 4.3 Spring autowires the sole constructor implicitly. With multiple constructors you must annotate one to pick it.

saying these in an interview costs you the question

  • Thinking @Autowired matches by field/bean name first rather than by type
  • Believing you must always write @Autowired on every constructor
  • Claiming @Autowired works via your own reflection code rather than a bean post-processor

context