Question
You have two implementations of the same interface and Your service injects all implementations:
What is the order of paymentServices?
A.
StripePaymentService
PaypalPaymentServiceB.
PaypalPaymentService
StripePaymentServiceC. Random order depending on JVM startup.
D. Spring throws an exception because multiple beans of the same type exist.
📢 Get actionable Java and Spring Boot insights every week, including practical code tips and real-world, use-case-based interview questions, to help you level up your backend skills—join 7900+ subscribers for hand-crafted, no-fluff content.
First 100 paid subscribers will get the annual membership at $50/year forever that is ~ $4/mo
Testimonials
Correct Answer
B.
PaypalPaymentService
StripePaymentServiceExplanation
When Spring injects a single bean:
@Autowired
PaymentService paymentService;multiple implementations cause:
NoUniqueBeanDefinitionExceptionunless we use @Primary or @Qualifier.
However, injecting a collection tells Spring:
“Inject every bean that implements this interface.”
@Autowired
List<PaymentService> paymentServices;Spring collects all matching beans and sorts them before injection.
The sorting order is:
@OrderOrderedinterfaceRegistration order (implementation detail—not guaranteed and shouldn’t be relied upon)
Since:
@Order(1)
PaypalPaymentServicecomes before
@Order(2)
StripePaymentServicethe injected list becomes:
PaypalPaymentService
StripePaymentServiceWhat are different options to control ordering?
Option 1: @Order
@Order(1)
@Service
class PaypalPaymentService implements PaymentService {
}Lower values indicate higher priority.
Option 2: Implement Ordered
@Service
class StripePaymentService implements PaymentService, Ordered {
@Override
public int getOrder() {
return 2;
}
}Option 3: Sort Explicitly
For business-critical workflows, it’s often better to sort based on business rules instead of relying on Spring’s ordering.
paymentServices.stream()
.sorted(comparing(PaymentService::priority))
.toList();Discuss Some Real-World Usage
Injecting a collection of strategy implementations is a common Spring pattern used for:
Payment routing
Fraud detection pipelines
Authentication providers
Validation chains
Rule engines
Message processors
Notification channels
For example:
List<FraudCheck> fraudChecks;Spring might inject:
VelocityCheck
BlacklistCheck
RiskScoreCheckApplication simply executes them in order.
What happens if
@Orderis removed?
Spring still injects all matching beans, but the order falls back to bean registration order. Since registration order is an implementation detail, we shouldn't depend on it.
Does
@Orderaffect which bean is injected here?
@Autowired
PaymentService paymentService;No.
@Order only affects collections, arrays, and ordered extension points. For a single bean injection, we should use @Primary or @Qualifier.
What if one payment strategy throws an exception?
A robust design should isolate failures so that one strategy doesn’t necessarily stop the rest of the pipeline.
Common approaches include:
Catching exceptions per strategy and continuing when appropriate
Logging failures and collecting results
Using retry or circuit breaker patterns for external calls
Returning a result object instead of throwing exceptions
Applying resilience libraries such as Resilience4j
Thats all for this week friends! Thanks for reading this far. If you liked it please share with your network.
Happy Coding 🚀
Suraj
Subscribe | Sponsor us | LinkedIn | Twitter | Youtube




