|
|
|
@ -24,12 +24,10 @@ import java.util.Collection; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.LinkedHashSet; |
|
|
|
import java.util.LinkedHashSet; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
import java.util.ListIterator; |
|
|
|
|
|
|
|
import java.util.Set; |
|
|
|
import java.util.Set; |
|
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.logging.Log; |
|
|
|
import org.apache.commons.logging.Log; |
|
|
|
import org.apache.commons.logging.LogFactory; |
|
|
|
import org.apache.commons.logging.LogFactory; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.BeanFactory; |
|
|
|
import org.springframework.beans.factory.BeanFactory; |
|
|
|
import org.springframework.beans.factory.HierarchicalBeanFactory; |
|
|
|
import org.springframework.beans.factory.HierarchicalBeanFactory; |
|
|
|
import org.springframework.beans.factory.ListableBeanFactory; |
|
|
|
import org.springframework.beans.factory.ListableBeanFactory; |
|
|
|
@ -100,15 +98,13 @@ public class OnBeanCondition extends SpringBootCondition implements |
|
|
|
return ConditionOutcome.noMatch("@ConditionalOnSingleCandidate " + spec |
|
|
|
return ConditionOutcome.noMatch("@ConditionalOnSingleCandidate " + spec |
|
|
|
+ " found no beans"); |
|
|
|
+ " found no beans"); |
|
|
|
} |
|
|
|
} |
|
|
|
else if (hasSingleAutowireCandidate(context.getBeanFactory(), matching)) { |
|
|
|
else if (!hasSingleAutowireCandidate(context.getBeanFactory(), matching)) { |
|
|
|
matchMessage.append("@ConditionalOnSingleCandidate " + spec + " found a primary " + |
|
|
|
|
|
|
|
"candidate amongst the following " + matching); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
return ConditionOutcome.noMatch("@ConditionalOnSingleCandidate " + spec |
|
|
|
return ConditionOutcome.noMatch("@ConditionalOnSingleCandidate " + spec |
|
|
|
+ " found no primary candidate amongst the" |
|
|
|
+ " found no primary candidate amongst the" + " following " |
|
|
|
+ " following " + matching); |
|
|
|
+ matching); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
matchMessage.append("@ConditionalOnSingleCandidate " + spec + " found " |
|
|
|
|
|
|
|
+ "a primary candidate amongst the following " + matching); |
|
|
|
} |
|
|
|
} |
|
|
|
if (metadata.isAnnotated(ConditionalOnMissingBean.class.getName())) { |
|
|
|
if (metadata.isAnnotated(ConditionalOnMissingBean.class.getName())) { |
|
|
|
BeanSearchSpec spec = new BeanSearchSpec(context, metadata, |
|
|
|
BeanSearchSpec spec = new BeanSearchSpec(context, metadata, |
|
|
|
@ -221,23 +217,21 @@ public class OnBeanCondition extends SpringBootCondition implements |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean hasSingleAutowireCandidate(ConfigurableListableBeanFactory beanFactory, |
|
|
|
private boolean hasSingleAutowireCandidate( |
|
|
|
List<String> beans) { |
|
|
|
ConfigurableListableBeanFactory beanFactory, List<String> beanNames) { |
|
|
|
|
|
|
|
return (beanNames.size() == 1 || getPrimaryBeans(beanFactory, beanNames).size() == 1); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (beans.size() == 1) { |
|
|
|
private List<String> getPrimaryBeans(ConfigurableListableBeanFactory beanFactory, |
|
|
|
return true; |
|
|
|
List<String> beanNames) { |
|
|
|
} |
|
|
|
List<String> primaryBeans = new ArrayList<String>(); |
|
|
|
boolean primaryFound = false; |
|
|
|
for (String beanName : beanNames) { |
|
|
|
for (String bean : beans) { |
|
|
|
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName); |
|
|
|
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(bean); |
|
|
|
|
|
|
|
if (beanDefinition != null && beanDefinition.isPrimary()) { |
|
|
|
if (beanDefinition != null && beanDefinition.isPrimary()) { |
|
|
|
if (primaryFound) { |
|
|
|
primaryBeans.add(beanName); |
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
primaryFound = true; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return primaryFound; |
|
|
|
return primaryBeans; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static class BeanSearchSpec { |
|
|
|
private static class BeanSearchSpec { |
|
|
|
@ -288,8 +282,7 @@ public class OnBeanCondition extends SpringBootCondition implements |
|
|
|
return "@" + ClassUtils.getShortName(this.annotationType); |
|
|
|
return "@" + ClassUtils.getShortName(this.annotationType); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings({ "unchecked", "rawtypes" }) |
|
|
|
protected void collect(MultiValueMap<String, Object> attributes, String key, |
|
|
|
private void collect(MultiValueMap<String, Object> attributes, String key, |
|
|
|
|
|
|
|
List<String> destination) { |
|
|
|
List<String> destination) { |
|
|
|
List<?> values = attributes.get(key); |
|
|
|
List<?> values = attributes.get(key); |
|
|
|
if (values != null) { |
|
|
|
if (values != null) { |
|
|
|
@ -388,18 +381,17 @@ public class OnBeanCondition extends SpringBootCondition implements |
|
|
|
super(context, metadata, annotationType); |
|
|
|
super(context, metadata, annotationType); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
protected void collect(MultiValueMap<String, Object> attributes, String key, |
|
|
|
|
|
|
|
List<String> destination) { |
|
|
|
|
|
|
|
super.collect(attributes, key, destination); |
|
|
|
|
|
|
|
destination.removeAll(Arrays.asList("", Object.class.getName())); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void validate() { |
|
|
|
protected void validate() { |
|
|
|
List<String> types = getTypes(); |
|
|
|
Assert.isTrue(getTypes().size() == 1, annotationName() + " annotations must " |
|
|
|
ListIterator<String> it = types.listIterator(); |
|
|
|
+ "specify only one type (got " + getTypes() + ")"); |
|
|
|
while (it.hasNext()) { |
|
|
|
|
|
|
|
String value = it.next(); |
|
|
|
|
|
|
|
if (!StringUtils.hasText(value) || Object.class.getName().equals(value)) { |
|
|
|
|
|
|
|
it.remove(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Assert.isTrue(types.size() == 1, annotationName() + " annotations must " |
|
|
|
|
|
|
|
+ "specify only one type (got " + types + ")"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|