how do i make one bean inject as default fi there are other implementation?
- User
how do i make one bean inject as default fi there are other implementation?
- Assistant
In Spring Framework, when you have multiple beans implementing the same interface, Spring needs a way to determine which one to inject when that interface is autowired. To designate a default bean among multiple implementations, you can use the
@Primaryannotation. Additionally, you can use@Qualifierfor more granular control when injecting specific beans.Here's how you can achieve this:
1. Using
@PrimaryAnnotationThe
@Primaryannotation is used to indicate that a particular bean should be given preference when multiple candidates are qualified to autowire a single-valued dependency.Example:
Suppose you have an interface
PaymentServiceand two implementations:CreditCardPaymentServiceandPaypalPaymentService.// PaymentService.java public interface PaymentService { void processPayment(); }// CreditCardPaymentService.java import org.springframework.stereotype.Service; @Service @Primary // This bean will be the default public class CreditCardPaymentService implements PaymentService { @Override public void processPayment() { System.out.println("Processing payment with Credit Card."); } }// PaypalPaymentService.java import org.springframework.stereotype.Service; @Service public class PaypalPaymentService implements PaymentService { @Override public void processPayment() { System.out.println("Processing payment with PayPal."); } }// OrderController.java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @Controller public class OrderController { private final PaymentService paymentService; @Autowired public OrderController(PaymentService paymentService) { this.paymentService = paymentService; } public void makeOrder() { paymentService.processPayment(); } }Behavior:
Since
CreditCardPaymentServiceis annotated with@Primary, Spring will inject it by default whenPaymentServiceis autowired inOrderController. Thus, callingmakeOrder()will print:Processing payment with Credit Card.2. Using
@QualifierAnnotationIf you want to inject a specific bean when there are multiple implementations, you can use the
@Qualifierannotation to specify which bean to inject.Example:
Continuing from the previous example, suppose you want to use
PaypalPaymentServicein another controller.// PaymentController.java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; @Controller public class PaymentController { private final PaymentService paymentService; @Autowired public PaymentController(@Qualifier("paypalPaymentService") PaymentService paymentService) { this.paymentService = paymentService; } public void processPayment() { paymentService.processPayment(); } }Notes:
- The string inside
@Qualifiershould match the bean name. By default, Spring uses the class name with the first letter in lowercase (e.g.,paypalPaymentServiceforPaypalPaymentService). - You can also explicitly name your beans using
@Service("paypalService")and then use@Qualifier("paypalService").
3. Combining
@Primaryand@QualifierYou can use
@Primaryto set a default bean and@Qualifierto override the default when necessary. This provides flexibility in your dependency injection strategy.4. Alternative: Using Java Configuration
If you're using Java-based configuration instead of annotations, you can designate a primary bean using the
@Primaryannotation in your@Configurationclass.Example:
// AppConfig.java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; @Configuration public class AppConfig { @Bean @Primary public PaymentService creditCardPaymentService() { return new CreditCardPaymentService(); } @Bean public PaymentService paypalPaymentService() { return new PaypalPaymentService(); } }Summary
@Primary: Marks one bean as the default choice when multiple beans qualify for autowiring.@Qualifier: Specifies exactly which bean to inject when autowiring, overriding@Primaryif necessary.- Combination: Use
@Primaryfor a default bean and@Qualifierfor specific injections, providing both default behavior and flexibility.
By utilizing these annotations appropriately, you can manage multiple bean implementations effectively and ensure that the correct bean is injected based on your application's requirements.
- The string inside