1. Overview

In this article, we’ll look at how we can name our Spring beans. By controlling the naming of our beans, we can tell Spring which beans we want to inject into a target bean.

2. Default Bean Naming Strategy

Let’s start with the default bean naming strategy employed by Spring.

Spring gets the class name and converts the first letter to lowercase. Then, this value becomes the name of the bean.

We’ll use the AuditService interface for the upcoming examples:

public interface AuditService {
}

Now, let’s see the default name generated for an AuditService implementation:

@Component
public class LegacyAuditService implements AuditService {
}

Here, we have the LegacyAuditService bean. Spring will register this bean under the name of legacyAuditService.

3. Naming Beans using @Component

Now, let’s look at how we can name our beans using the @Component annotation.

We can define the bean name using the value of @Component:

@Component("advanced")
public class AdvancedAuditService implements AuditService {
}

Here, Spring will register the AdvancedAuditService under the name of advanced.

4. Naming Beans using @Qualifier

Another way to name our Spring beans is by using the @Qualifier annotation.

Similar to @Component, we can use the value of @Qualifier to define a name. Moreover, we can use both approaches at the same time:

@Component("advanced")
@Qualifier("qualifiedAdvanced")
public class AdvancedAuditService implements AuditService {
}

Here, Spring will register the AdvancedAuditService bean under two different names: advanced and qualifiedAdvanced.

5. Using @Bean

There are also several approaches for naming the beans defined with the @Bean annotation.

5.1. Naming Beans with @Bean

Firstly, we can use the value of @Bean to name our beans:

public class SimpleAuditService implements AuditService{
}

Here is our SimpleAuditService class. Let’s look at the bean configuration:

@Configuration
public class AuditConfiguration {

    @Bean("simple")
    public AuditService theSimpleOne() {
        return new SimpleAuditService();
    }
}

Here Spring will register SimpleAuditService under the name of simple.

5.2. Naming Beans with @Qualifier and @Bean

Secondly, we can use the @Qualifier annotation with @Bean methods to name our beans. Moreover, we can also combine two approaches:

@Configuration
public class AuditConfiguration {

    @Bean("simple")
    @Qualifier("qualifiedSimple")
    public AuditService theSimpleOne() {
        return new SimpleAuditService();
    }
}

As a result, Spring will register the SimpleAuditService bean under two names: simple and qualifiedSimple.

5.3. Naming Beans with Method Name

Lastly, we can name our beans using the @Bean method name:

public class ObsoleteAuditService implements AuditService {
}

Here we have the ObsoleteAuditService class.

We’ll look at the configuration next:

package com.javabyexamples.spring.core.beannaming;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AuditConfiguration {

    @Bean
    public AuditService obsolete(){
       return new LegacyAuditService();
    }
}

Since we aren’t specifying a name in the @Bean annotation, Spring uses the method name, obsolete, as the name of the bean.

6. Summary

In this tutorial, we’ve investigated how we can name our beans using @Component, @Qualifier, @Bean and method names.

Check out the source code over on Github.

Leave a Reply

Close Menu