Tuesday, August 20, 2024

What is @Qualifier and @primary annotations in spring boot?

In Spring Boot and the broader Spring Framework, @Qualifier and @Primary annotations are used to handle dependency injection when multiple beans of the same type are available. They help Spring determine which bean should be injected where multiple candidates exist.

@Qualifier

The @Qualifier annotation is used to provide more specific information about which bean should be injected when multiple beans of the same type are available in the Spring context. It helps resolve ambiguity by explicitly specifying the desired bean.

How it works:

·       Define Beans with Qualifiers: You define multiple beans of the same type and annotate them with @Qualifier to differentiate them.

·       Inject Beans with Qualifiers: You use @Qualifier on the injection point to specify which bean you want to inject.

Example:

@Component

@Qualifier("beanA")

public class BeanA implements MyService {

    // Implementation details

}

@Component

@Qualifier("beanB")

public class BeanB implements MyService {

    // Implementation details

}

 

@Service

public class MyServiceConsumer {

     private final MyService myService;

     @Autowired

    public MyServiceConsumer(@Qualifier("beanA") MyService myService) {

        this.myService = myService;

    }

     // Use myService

}

In this example:

  • BeanA and BeanB are two implementations of MyService.
  • @Qualifier("beanA") specifies that BeanA should be injected into MyServiceConsumer.
@Primary

The @Primary annotation is used to indicate that a particular bean should be the default choice when multiple beans of the same type are available. When a bean is marked as @Primary, it will be used by default if no other qualifiers are specified.

How it works:

·       Define a Primary Bean: You annotate one of the beans with @Primary to make it the default choice.

·       Fallback: If no specific @Qualifier is used, the primary bean will be injected.

Example:

@Component

@Primary

public class BeanA implements MyService {

    // Implementation details

}

 @Component

public class BeanB implements MyService {

    // Implementation details

}

 

@Service

public class MyServiceConsumer {

     private final MyService myService;

     @Autowired

    public MyServiceConsumer(MyService myService) {

        this.myService = myService;

    }

     // Use myService

}

 

In this example:

  • BeanA is marked with @Primary, making it the default choice for injection.
  • If no @Qualifier is used in the MyServiceConsumer constructor, BeanA will be injected.

Summary

  • @Qualifier: Used to specify which bean to inject when there are multiple candidates of the same type. You provide a specific name or identifier to resolve ambiguity.
  • @Primary: Used to indicate which bean should be the default choice when multiple beans of the same type are available. This bean will be injected if no @Qualifier is specified.

These annotations provide flexibility in dependency injection, allowing you to manage and resolve conflicts in bean injection more effectively.


No comments:

Post a Comment