The @Qualifier annotation tells Spring exactly which bean to inject when more than one bean of the same type exists. Spring autowires by type first, so two beans implementing the same interface cause a NoUniqueBeanDefinitionException. Adding @Qualifier("beanName") at the injection point resolves the ambiguity by name or by a custom qualifier label.
It is used together with @Autowired, constructor injection, or @Bean method parameters.
Consider one interface with two implementations:
Injecting NotificationSender without more information fails at startup:
Constructor injection is the recommended style. Put @Qualifier on the parameter:
The default bean name for a @Component is the class name with a lowercase first letter, so SmsSender becomes smsSender. You can set it explicitly with @Component("sms") or @Bean("sms").
With field injection the annotation goes next to @Autowired, but field injection makes testing harder:
An injection point can then ask for @Qualifier("fast"). The qualifier value is a label, which is independent of the method name. If a bean has no explicit qualifier, its bean name acts as the fallback qualifier.
String names are easy to mistype and are not checked by the compiler. A meta-annotation avoids that:
Renaming the bean no longer breaks injection points, and the IDE can find every usage.
| Approach | Use when |
|---|---|
@Qualifier | A specific consumer needs a specific bean |
@Primary | One bean is the sensible default almost everywhere |
Inject List<T> or Map<String, T> | The consumer wants every implementation |
| Parameter name matching | Works as a fallback, but breaks when parameters are renamed or compiled without parameter names |
@Primary marks a default, and a @Qualifier at an injection point overrides it. You can combine both: mark the common implementation @Primary and qualify the exceptions.
NoSuchBeanDefinitionException.@Qualifier on a method annotated with @Bean and expecting it to rename the bean. It adds a label only; use @Bean("name") to rename.javax.inject.@Named (now jakarta.inject.@Named) and @Qualifier without understanding both work, but one style per codebase is clearer.@Qualifier on a field is not copied to the generated constructor parameter unless you add lombok.copyableAnnotations += org.springframework.beans.factory.annotation.Qualifier to lombok.config.@Primary sets a default bean for all injection points of a type. @Qualifier selects a specific bean at one injection point and takes precedence over @Primary.
Yes. Spring applies it to constructor parameters of a single-constructor bean without @Autowired, and to @Bean method parameters.
Spring throws NoUniqueBeanDefinitionException unless one bean is @Primary or the injection point name matches a bean name.
Yes. Declare a List<NotificationSender> or Map<String, NotificationSender> parameter.
@Qualifier resolves ambiguity when several beans share a type by naming the bean you want. Prefer constructor injection, use custom qualifier annotations for type safety, and combine it with @Primary for sensible defaults.