Improve condition message produced by @ConditionalOnSingleCandidate
Closes gh-30073
This commit is contained in:
parent
480ccc6175
commit
e7705f4f71
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2020 the original author or authors.
|
* Copyright 2012-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -130,13 +130,25 @@ class OnBeanCondition extends FilteringSpringBootCondition implements Configurat
|
||||||
if (!matchResult.isAllMatched()) {
|
if (!matchResult.isAllMatched()) {
|
||||||
return ConditionOutcome.noMatch(spec.message().didNotFind("any beans").atAll());
|
return ConditionOutcome.noMatch(spec.message().didNotFind("any beans").atAll());
|
||||||
}
|
}
|
||||||
else if (!hasSingleAutowireCandidate(context.getBeanFactory(), matchResult.getNamesOfAllMatches(),
|
Set<String> allBeans = matchResult.getNamesOfAllMatches();
|
||||||
spec.getStrategy() == SearchStrategy.ALL)) {
|
if (allBeans.size() == 1) {
|
||||||
return ConditionOutcome.noMatch(spec.message().didNotFind("a primary bean from beans")
|
matchMessage = spec.message(matchMessage).found("a single bean").items(Style.QUOTE, allBeans);
|
||||||
.items(Style.QUOTE, matchResult.getNamesOfAllMatches()));
|
}
|
||||||
|
else {
|
||||||
|
List<String> primaryBeans = getPrimaryBeans(context.getBeanFactory(), allBeans,
|
||||||
|
spec.getStrategy() == SearchStrategy.ALL);
|
||||||
|
if (primaryBeans.isEmpty()) {
|
||||||
|
return ConditionOutcome.noMatch(
|
||||||
|
spec.message().didNotFind("a primary bean from beans").items(Style.QUOTE, allBeans));
|
||||||
|
}
|
||||||
|
if (primaryBeans.size() > 1) {
|
||||||
|
return ConditionOutcome
|
||||||
|
.noMatch(spec.message().found("multiple primary beans").items(Style.QUOTE, primaryBeans));
|
||||||
|
}
|
||||||
|
matchMessage = spec.message(matchMessage)
|
||||||
|
.found("a single primary bean '" + primaryBeans.get(0) + "' from beans")
|
||||||
|
.items(Style.QUOTE, allBeans);
|
||||||
}
|
}
|
||||||
matchMessage = spec.message(matchMessage).found("a primary bean from beans").items(Style.QUOTE,
|
|
||||||
matchResult.getNamesOfAllMatches());
|
|
||||||
}
|
}
|
||||||
if (metadata.isAnnotated(ConditionalOnMissingBean.class.getName())) {
|
if (metadata.isAnnotated(ConditionalOnMissingBean.class.getName())) {
|
||||||
Spec<ConditionalOnMissingBean> spec = new Spec<>(context, metadata, annotations,
|
Spec<ConditionalOnMissingBean> spec = new Spec<>(context, metadata, annotations,
|
||||||
|
|
@ -341,11 +353,6 @@ class OnBeanCondition extends FilteringSpringBootCondition implements Configurat
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean hasSingleAutowireCandidate(ConfigurableListableBeanFactory beanFactory, Set<String> beanNames,
|
|
||||||
boolean considerHierarchy) {
|
|
||||||
return (beanNames.size() == 1 || getPrimaryBeans(beanFactory, beanNames, considerHierarchy).size() == 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<String> getPrimaryBeans(ConfigurableListableBeanFactory beanFactory, Set<String> beanNames,
|
private List<String> getPrimaryBeans(ConfigurableListableBeanFactory beanFactory, Set<String> beanNames,
|
||||||
boolean considerHierarchy) {
|
boolean considerHierarchy) {
|
||||||
List<String> primaryBeans = new ArrayList<>();
|
List<String> primaryBeans = new ArrayList<>();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue