Without one of them Spring cannot choose and startup fails with NoUniqueBeanDefinitionException.
@Bean @Primary
PaymentGateway razorpay() { ... }
@Bean
PaymentGateway stripe() { ... }
@Service
class Checkout {
Checkout(PaymentGateway gateway) { } // razorpay
Checkout(@Qualifier("stripe") PaymentGateway gateway) { } // stripe
}
Without @Primary:
NoUniqueBeanDefinitionException: expected single matching bean but found 2:
razorpay,stripe
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