Spring: @Primary and @Qualifier resolve two beans of one type

Without one of them Spring cannot choose and startup fails with NoUniqueBeanDefinitionException.

Code
@Bean @Primary
PaymentGateway razorpay() { ... }

@Bean
PaymentGateway stripe() { ... }

@Service
class Checkout {
    Checkout(PaymentGateway gateway) { }                      // razorpay
    Checkout(@Qualifier("stripe") PaymentGateway gateway) { } // stripe
}
Output
Without @Primary:
NoUniqueBeanDefinitionException: expected single matching bean but found 2:
razorpay,stripe
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11