Spring Beanname name vs Bean Qualifiername
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.
Introduction
@Bean(name = "...") and @Qualifier("...") are not the same thing in Spring. A bean name identifies the bean definition in the container, while a qualifier is extra metadata used during dependency injection to help Spring choose among multiple candidates of the same type.
What @Bean(name = "...") Does
@Bean declares a bean, and its name attribute sets the bean name explicitly.
In this example, the bean is registered in the application context under the name primaryDataSource.
If you omit the name attribute, Spring uses the method name by default.
Now the bean name is dataSource.
What @Qualifier("...") Does
A qualifier helps Spring resolve ambiguity when more than one bean matches by type.
At the injection point, it looks like this:
This tells Spring to inject the bean that matches the qualifier reporting.
@Qualifier on a Bean Definition
You can also place a qualifier on the bean definition itself.
Here, the bean names are still reportingDataSource and analyticsDataSource unless you set name explicitly. The qualifiers are separate metadata values used for autowiring decisions.
That is the key difference: qualifier is not just another way to set the bean name.
Why They Sometimes Look Similar
Spring can use bean names as a fallback qualifier in some autowiring scenarios, which makes the two concepts feel similar. For example, this often works:
And then:
That works because the bean name matches the qualifier string. But conceptually the bean name and qualifier are still different roles.
A Practical Comparison
Think of it this way:
- bean name answers “what is this bean called in the container?”
- qualifier answers “which matching bean should be injected here?”
In a simple app with one bean of a given type, you often need neither explicit naming nor qualifiers.
In a larger app with several beans of the same type, qualifiers become important.
@Primary Is Another Related Tool
If one bean should win by default when there are several candidates, @Primary is often cleaner than sprinkling qualifiers everywhere.
Then use @Qualifier only for the non-default cases.
Common Pitfalls
A common mistake is assuming @Bean(name = "x") and @Qualifier("x") are interchangeable. They are not.
Another pitfall is placing @Qualifier only on the injection point without qualifying any bean definition or otherwise having a matching bean name.
Developers also sometimes overuse bean names when qualifiers would communicate intent better, especially when several beans share the same type but serve different roles.
Finally, remember that ambiguity is resolved during injection, not bean creation. Naming a bean does not automatically solve every injection conflict unless Spring can use that name to match the injection point.
Summary
- '
@Bean(name = "x")sets the bean’s name in the Spring container.' - '
@Qualifier("x")helps choose among beans during autowiring.' - A qualifier is metadata, not just another spelling of the bean name.
- Bean names can sometimes match qualifier strings, which is why the two concepts are easy to confuse.
- Use
@Primaryfor the default choice and@Qualifierfor explicit exceptions.
Related reading
- Spring Boot - inject map from application.yml
- Spring boot - Service class calling another Service class
- Spring Boot 2 - Unsatisfied dependency on Feign client when autowired for service
- Spring Boot autowire beans from library project
- Spring Boot - Cannot determine embedded database driver class for database type NONE
- Spring Boot - Cannot determine embedded database driver class for database type NONE
- Spring Boot autowired does not work, classes in different package
- Spring boot autowiring an interface with multiple implementations

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.