Spring Framework
Bean Annotation
Qualifier Annotation
Dependency Injection
Java Configuration

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.

Practice OOD

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.

java
1@Configuration
2public class AppConfig {
3
4    @Bean(name = "primaryDataSource")
5    public DataSource dataSource() {
6        return new DataSource();
7    }
8}

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.

java
1@Bean
2public DataSource dataSource() {
3    return new DataSource();
4}

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:

java
1@Component
2public class ReportService {
3
4    private final DataSource dataSource;
5
6    public ReportService(@Qualifier("reporting") DataSource dataSource) {
7        this.dataSource = dataSource;
8    }
9}

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.

java
1@Configuration
2public class AppConfig {
3
4    @Bean
5    @Qualifier("reporting")
6    public DataSource reportingDataSource() {
7        return new DataSource();
8    }
9
10    @Bean
11    @Qualifier("analytics")
12    public DataSource analyticsDataSource() {
13        return new DataSource();
14    }
15}

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:

java
1@Bean(name = "reporting")
2public DataSource reportingDataSource() {
3    return new DataSource();
4}

And then:

java
public ReportService(@Qualifier("reporting") DataSource dataSource) {
    this.dataSource = dataSource;
}

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.

If one bean should win by default when there are several candidates, @Primary is often cleaner than sprinkling qualifiers everywhere.

java
1@Bean
2@Primary
3public DataSource mainDataSource() {
4    return new DataSource();
5}

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 @Primary for the default choice and @Qualifier for explicit exceptions.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Practice OOD

All Rights Reserved.