When I was browsing the source code, I found that OnBeanCondition.class
on ConditionalOnSingleCandidate
annotation, match the singleAutowireCandidate
method only consider SearchStrategy
of SearchStrategy.ALL
. Line number is: 143.
the original source code was this:
else if (!hasSingleAutowireCandidate(context.getBeanFactory(),
matchResult.getNamesOfAllMatches(),
spec.getStrategy() == SearchStrategy.ALL)) {
return ConditionOutcome.noMatch(ConditionMessage
.forCondition(ConditionalOnSingleCandidate.class, spec)
.didNotFind("a primary bean from beans")
.items(Style.QUOTE, matchResult.getNamesOfAllMatches()));
}
I wonder why we don't consider two other strategies here。
Comment From: philwebb
It's not so clear from the formatting, but the spec.getStrategy() == SearchStrategy.ALL
is not part of the if
. It's actually the third argument to hasSingleAutowireCandidate
(named considerHierarchy
).
So all it's saying is if spec.getStrategy() == SearchStrategy.ALL
then we will search the full application context hierarchy, otherwise we just search the current application context.