Spring Boot @Qualifier Example

1. Introduction

The @Qualifier annotation in Spring Boot is crucial when you need to specify which bean should be wired among multiple beans of the same interface or superclass. It disambiguates beans when Spring's autowiring process requires more precision.

Key Points:

1. @Qualifier is used when multiple beans of the same type are present in the Spring container to indicate which bean should be injected.

2. This annotation helps in preventing the confusion that may occur when Spring cannot decide which bean to autowire.

3. It is often used in conjunction with @Autowired to provide a specific bean name for the injection.

2. Implementation Steps

1. Define a common interface for your services.

2. Create multiple implementations of this interface, each with its own @Service or @Component annotation.

3. Define a component that needs a specific implementation of the interface.

4. Use the @Autowired and @Qualifier annotations to specify which implementation should be injected.

5. Create a main application class to run the application and test the qualified bean injection.

3. Implementation Example

Here is the complete code that demonstrates the usage of @Qualifier annotation:
// Step 1: Define a common interface for services
public interface GenericService {
    String serve();
}

// Step 2: Create the first implementation of the service
@Service("firstService")
public class FirstService implements GenericService {
    public String serve() {
        return "Service from FirstService";
    }
}

// Step 2: Create the second implementation of the service
@Service("secondService")
public class SecondService implements GenericService {
    public String serve() {
        return "Service from SecondService";
    }
}

// Step 3: Define the component that requires a specific implementation
@Component
public class ServiceUser {

    private final GenericService genericService;

    // Step 4: Use @Autowired and @Qualifier to inject a specific service implementation
    @Autowired
    public ServiceUser(@Qualifier("firstService") GenericService genericService) {
        this.genericService = genericService;
    }

    public void execute() {
        System.out.println(genericService.serve());
    }
}

// Step 5: Create the main application class
@SpringBootApplication
public class QualifierExampleApplication implements CommandLineRunner {

    // Automatically inject the ServiceUser component
    @Autowired
    private ServiceUser serviceUser;

    public static void main(String[] args) {
        SpringApplication.run(QualifierExampleApplication.class, args);
    }

    @Override
    public void run(String... args) {
        // Execute the service to demonstrate qualified injection
        serviceUser.execute();
    }
}

Output:

Service from FirstService

Explanation:

1. GenericService interface serves as a contract for service implementations.

2. FirstService and SecondService are concrete implementations of the GenericService interface, each annotated with @Service and qualified with different names.

3. ServiceUser is a component that needs one of the GenericService implementations injected into it.

4. The @Autowired annotation in conjunction with @Qualifier("firstService") specifies that FirstService should be the implementation injected into ServiceUser.

5. QualifierExampleApplication uses CommandLineRunner to execute serviceUser.execute(), demonstrating the injection of FirstService into ServiceUser.

6. The output confirms that FirstService has been correctly injected and its serve method is called, resulting in "Service from FirstService".


Comments