Experiment 1 code

@Autowired private AbClient myAbClient; Several instances implement the AbClient interface. The most typical is @enableFeignClients Spring CloudCopy the code

Results: 2 org. Springframework. Beans. Factory. Support. DefaultListableBeanFactory# determinePrimaryCandidate

Based primarily on the Bean definition is org. Springframework. Beans. Factory. Support. DefaultListableBeanFactory# isPrimary to determine keep instance;

3 source

/** * Determine the primary candidate in the given set of beans. * @param candidates a Map of candidate names and candidate instances * (or candidate classes if not created yet) that match the required type * @param requiredType the target dependency type to match against * @return the name of the primary candidate, or {@code null} if none found * @see #isPrimary(String, Object) */ @Nullable protected String determinePrimaryCandidate(Map<String, Object> candidates, Class<? > requiredType) { String primaryBeanName = null; for (Map.Entry<String, Object> entry : candidates.entrySet()) { String candidateBeanName = entry.getKey(); Object beanInstance = entry.getValue(); If (isPrimary(candidateBeanName, beanInstance)) {if (primaryBeanName! = null) { boolean candidateLocal = containsBeanDefinition(candidateBeanName); boolean primaryLocal = containsBeanDefinition(primaryBeanName); if (candidateLocal && primaryLocal) { throw new NoUniqueBeanDefinitionException(requiredType, candidates.size(), "more than one 'primary' bean found among candidates: " + candidates.keySet()); } else if (candidateLocal) { primaryBeanName = candidateBeanName; } } else { primaryBeanName = candidateBeanName; } } } return primaryBeanName; }Copy the code