Skip to content

Treat the only one default candidate as effectively primary #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
* @author Phillip Webb
* @author Stephane Nicoll
* @author Sebastien Deleuze
* @author Yanming Zhou
* @since 16 April 2001
* @see #registerBeanDefinition
* @see #addBeanPostProcessor
Expand Down Expand Up @@ -1479,6 +1480,9 @@ else if (candidateNames.length > 1) {
if (candidateName == null) {
candidateName = determineHighestPriorityCandidate(candidates, requiredType.toClass());
}
if (candidateName == null) {
candidateName = determineEffectivelyPrimaryCandidate(candidates, requiredType.toClass());
}
if (candidateName != null) {
Object beanInstance = candidates.get(candidateName);
if (beanInstance == null) {
Expand Down Expand Up @@ -2046,6 +2050,34 @@ else if (candidatePriority < highestPriority) {
return highestPriorityBeanName;
}

/**
* Determine the candidate which is the only one default 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 only one default candidate,
* or {@code null} if none found
*/
@Nullable
protected String determineEffectivelyPrimaryCandidate(Map<String, Object> candidates, Class<?> requiredType) {
String candidateBeanName = null;
for (Map.Entry<String, Object> entry : candidates.entrySet()) {
String transformedBeanName = transformedBeanName(entry.getKey());
if (containsBeanDefinition(transformedBeanName)) {
RootBeanDefinition bd = getMergedLocalBeanDefinition(transformedBeanName);
if (bd.isAutowireCandidate() && bd.isDefaultCandidate()) {
if (candidateBeanName == null) {
candidateBeanName = entry.getKey();
}
else {
return null;
}
}
}
}
return candidateBeanName;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe filtering would highlight the main condition:

.filter(entry -> entry.getValue().isAutowireCandidate() && entry.getValue().isDefaultCandidate())

protected String determineEffectivelyPrimaryCandidate(Map<String, Object> candidates, Class<?> requiredType) {
    return candidates.entrySet().stream()
        .map(entry -> Map.entry(entry.getKey(), transformedBeanName(entry.getKey())))
        .filter(entry -> containsBeanDefinition(entry.getValue()))
        .map(entry -> Map.entry(entry.getKey(), getMergedLocalBeanDefinition(entry.getValue())))
        .filter(entry -> entry.getValue().isAutowireCandidate() && entry.getValue().isDefaultCandidate())
        .map(Map.Entry::getKey)
        .reduce((a, b) -> null) // Ensures only one candidate; otherwise, null
        .orElse(null);
}

Its although nice, if a method has only has one return statement.

}

/**
* Return whether the bean definition for the given bean name has been
* marked as a primary bean.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
* @author Chris Beams
* @author Phillip Webb
* @author Stephane Nicoll
* @author Yanming Zhou
*/
class DefaultListableBeanFactoryTests {

Expand Down Expand Up @@ -1664,6 +1665,18 @@ void getBeanByTypeWithPrimary() {
assertThat(lbf.containsSingleton("bd1")).isFalse();
}

@Test
void getBeanByTypeWithEffectivelyPrimary() {
RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
bd1.setDefaultCandidate(false);
RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
lbf.registerBeanDefinition("bd1", bd1);
lbf.registerBeanDefinition("bd2", bd2);

TestBean bean = lbf.getBean(TestBean.class);
assertThat(bean.getBeanName()).isEqualTo("bd2");
}

@Test
@SuppressWarnings("rawtypes")
void getFactoryBeanByTypeWithPrimary() {
Expand Down