@Qualifier in Dependency Injection

`@Qualifier` selects a specific bean when multiple beans match the required dependency type. In a Java backend using Spring, it resolves ambiguity by adding a qualifier name to the injection point and matching it with a bean qualifier.

Dependency injection normally resolves a dependency by its type. If the container has multiple beans of that type, type-based resolution alone is ambiguous and the application needs another selection rule.

`@Qualifier` provides that additional information at the injection point. For example, if two `PaymentProcessor` beans exist, `@Qualifier("stripe")` tells Spring to inject the bean associated with that qualifier rather than choosing solely by type.

A qualifier is commonly placed on a constructor parameter, field, setter parameter, or method parameter. The target bean can expose the matching qualifier explicitly, for example with `@Component("stripe")`, or with a qualifier declaration such as `@Qualifier("stripe")` on a bean definition.

For junior interviews, remember that `@Qualifier` does not change the Java type of the dependency. It narrows the set of candidates that the dependency-injection container considers for that injection point. Constructor injection is generally preferred because the dependency is explicit and the object can be created with its required dependencies.

Key points

Interview questions

How does `@Qualifier` differ from `@Primary`?

`@Primary` establishes a preferred candidate when several beans match a type. `@Qualifier` provides a more specific selection at an injection point. If a particular consumer needs a specific implementation, a qualifier makes that intent explicit. Therefore, primary is useful for a default, while qualifiers are useful for deliberate per-injection selection.

Why would you use `@Qualifier` in Spring?

You use `@Qualifier` when Spring finds multiple beans that satisfy the same dependency type. The qualifier identifies which candidate should be injected at that particular injection point. This avoids an ambiguity error during dependency resolution. It is especially useful when an application intentionally has several implementations of the same interface.

Where can you use `@Qualifier`?

A common use is on a constructor parameter, such as `@Qualifier("stripe") PaymentProcessor processor`. It can also be used with other supported injection points, including fields and method parameters. Constructor injection is often preferred because dependencies are visible in the constructor and can be required when the object is created.

What happens if two implementations exist but neither is qualified or primary?

If both implementations are eligible for the same type-based injection and there is no other resolution rule that selects one, Spring cannot determine a unique candidate. The application context will fail during dependency resolution. Adding `@Qualifier` at the injection point is one direct way to select a specific bean. Alternatively, a suitable candidate can be marked as `@Primary` when a default is appropriate.

Does `@Qualifier` change the dependency's Java type?

No. The declared dependency can still be an interface such as `PaymentProcessor`. The qualifier supplies additional metadata that helps Spring choose among beans compatible with that type. This lets application code depend on the abstraction while the container selects a particular implementation.