Scrivio Interview library
InternalsFoundations5 min read · updated 12 November 2026

How does Spring choose which bean to inject into an @Autowired field of type PaymentGateway when there are two implementations?

Spring first collects every bean whose type matches PaymentGateway. If there is more than one, it narrows the list: a @Qualifier on the injection point filters the candidates, then a bean marked @Primary wins, then the bean with the highest @Priority, and as a last step a bean whose name matches the field or parameter name. If one bean is still not chosen, startup fails with NoUniqueBeanDefinitionException. Spring Framework 6.2 also added @Fallback, which marks a bean to use only when no other candidate is left.

Why this question is asked

Two implementations of one interface is a completely normal situation: a real gateway and a sandbox one, or two payment providers. Interviewers ask how Spring picks between them because the answer explains a whole family of startup errors and lets us choose the right tool, instead of adding @Primary until the error goes away. In this answer, we will recap what a candidate is, reproduce the ambiguity, then walk through each step Spring uses to narrow the choice, in order, with a small example for each.

1A Quick Recap: Candidates by Type

When Spring injects a dependency, it starts from the declared type of the field or parameter, here PaymentGateway. It asks the container for every bean definition that can produce a PaymentGateway: classes that implement it, @Bean methods that return it or a subtype, and FactoryBeans whose product matches. Each of those is a candidate, an eligible bean for this injection point.

If there is exactly one candidate, Spring injects it and the story ends. If there is none, injection fails with NoSuchBeanDefinitionException. The interesting case is two or more.

2Reproducing the Ambiguity

Here are two implementations and a service that asks for one of them.

Java
@Component class StripeGateway implements PaymentGateway { }
@Component class AdyenGateway implements PaymentGateway { }
@Service
class CheckoutService {
@Autowired PaymentGateway gateway;
}
// APPLICATION FAILED TO START
// Field gateway in CheckoutService required a single bean, but 2 were found:
// - adyenGateway
// - stripeGateway

Underneath that friendly Spring Boot message is a NoUniqueBeanDefinitionException. Spring found two candidates and none of its tie-breaking rules picked one.

3@Qualifier Filters the Candidates

A qualifier is a label on a bean and a matching label on the injection point. The most common form is @Qualifier with the bean name, and Spring applies it while collecting candidates, so non-matching beans never make the list.

Java
@Autowired @Qualifier("stripeGateway") PaymentGateway gateway; // only stripeGateway is a candidate -> injected

Notice that the qualifier is a hard filter, not a preference: if no bean matches it, injection fails, even if other PaymentGateway beans exist. A bean can also carry its own @Qualifier("cards") value, and custom qualifier annotations work the same way.

4Steps 2 and 3: @Primary, Then @Priority

If several candidates remain, Spring looks for exactly one marked @Primary. That bean wins. If two candidates of the same type are both primary, Spring cannot decide and fails.

Java
@Component @Primary class StripeGateway implements PaymentGateway { }
@Component class AdyenGateway implements PaymentGateway { }
// @Autowired PaymentGateway gateway; -> StripeGateway

If no bean is primary, Spring checks @Priority, the jakarta.annotation.Priority annotation on the bean class, and picks the candidate with the highest priority, which is the lowest number. Two candidates with the same priority fail again. In practice @Priority is rarely used for this; @Primary is the usual tool.

5Matching the Name

As a last step, Spring compares the name of the field or constructor parameter with the bean names. If one candidate's name, or one of its aliases, equals the injection point's name, that bean is chosen.

Java
@Autowired PaymentGateway adyenGateway; // field name matches bean name -> AdyenGateway
CheckoutService(PaymentGateway stripeGateway) { } // works only if parameter names are compiled in (-parameters)
Step throughHow Spring narrows several PaymentGateway candidates
  1. Collect beans of type PaymentGatewayhonouring any @Qualifier on the injection point
  2. Exactly one candidate?
    YesInject itNoNarrow further
  3. Exactly one @Primary?
    YesInject itNoCheck @Fallback
  4. Exactly one bean not marked @Fallback (6.2+)?
    YesInject itNoCheck @Priority
  5. A single highest @Priority?
    YesInject itNoTry the name
  6. A bean named like the field or parameter?
    YesInject itNoNoUniqueBeanDefinitionException

6 steps in order. Play them, or step through them one at a time.

Qualifiers filter; the other steps only break ties. Check your version for the exact position of the name step.

This looks convenient, but it is fragile. Renaming a field changes which bean we get, without any error, and for constructor parameters the names must be kept in the compiled class with the -parameters compiler flag, which Spring Boot's build plugins set but custom builds may not. An explicit @Qualifier says the same thing more robustly. Spring Framework 6.2 also refined where name matching sits relative to the other steps (check your version if you depend on the exact order).

6@Fallback in Spring Framework 6.2

@Fallback is the opposite of @Primary. Instead of marking the bean that should win, we mark the beans that should lose. A fallback bean is injected only when no regular candidate exists, which is exactly what a library default needs: a NoOpPaymentGateway that is used in local development until the application defines a real one.

Java
@Bean @Fallback PaymentGateway noOpGateway() { return new NoOpPaymentGateway(); }
// Only noOpGateway defined -> NoOpPaymentGateway
// noOpGateway and StripeGateway both -> StripeGateway, the fallback steps aside

Spring checks this right after looking for @Primary: if no candidate is primary but exactly one candidate is not a fallback, that one wins. So when the application adds StripeGateway, there are two candidates, but one is a fallback, so StripeGateway is injected without anyone touching the default.

7Follow-up questions

Does @Qualifier also work as a bean name if the bean has no qualifier value?

Yes. If no bean carries that qualifier value, Spring also accepts a bean whose name matches it, which is why @Qualifier("stripeGateway") works on a plain @Component named stripeGateway. Explicit qualifier values on beans are useful when the name might change or when several beans should share one label.

What if we inject List<PaymentGateway> instead?

Then there is no ambiguity at all: Spring injects every candidate in a list, ordered by @Order, the Ordered interface or @Priority. @Primary does not remove the other beans from the list. This is the right shape when the code needs to choose at runtime, for example by country.

Is @Primary safe to use in shared libraries?

Usually not. A library's @Primary silently overrides the application's own choice for that type and can collide with another library's @Primary. Libraries should contribute defaults with @ConditionalOnMissingBean in auto-configuration, or with @Fallback, so the application stays in control.

8Conclusion

In this answer, we saw that Spring first collects every bean of the requested type, with any @Qualifier filtering that list. With several candidates left, it prefers a single @Primary bean, then the single highest @Priority, then a bean whose name matches the field or parameter name, and since Spring Framework 6.2 it sets aside beans marked @Fallback. If none of this produces one bean, startup fails with NoUniqueBeanDefinitionException. Qualifiers are the robust way to choose at an injection point, @Primary sets an application-wide default, and name matching is best avoided.

What to say in the interviewAbout 90 seconds aloud

Open with

Spring starts from the declared type, PaymentGateway, and collects every bean definition that can produce one: implementing classes, @Bean methods returning it, and FactoryBean products. If the injection point has a @Qualifier, that filters the candidates while they are collected, so only beans with that qualifier value, or that bean name, remain.

Then, if they want more

With exactly one candidate, it is injected. With several, Spring breaks the tie in steps. First, if exactly one candidate is marked @Primary, it wins. If not, it looks at the jakarta Priority annotation and takes the highest priority, which is the lowest number.

As a last step it matches the field or parameter name against the bean names, so a field called stripeGateway gets the stripeGateway bean; for constructor parameters this needs the -parameters compiler flag.

Spring Framework 6.2 added @Fallback, which works the other way round: a fallback bean is only used when no regular candidate exists, which is ideal for library defaults. If after all that there is still no single bean, startup fails with NoUniqueBeanDefinitionException, which Boot reports as required a single bean, but two were found.

In my own code I prefer an explicit @Qualifier or a custom qualifier annotation at the injection point, @Primary only for an application-wide default, and I avoid relying on names.

About this answer

Last updated
12 November 2026
Versions it names
Spring Framework 6
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How does Spring choose which bean to inject into an @Autowired field of type...": https://scrivio-interview-library.netlify.app/spring-boot/dependency-injection/94d2308b9d/