Ignore non-autowire candidates in type-based matching
Closes gh-41526
This commit is contained in:
parent
25c76957e5
commit
8183c5b23d
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2022 the original author or authors.
|
* Copyright 2012-2024 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.
|
||||||
|
|
@ -24,6 +24,8 @@ import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
|
import org.springframework.beans.factory.config.BeanDefinition;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Conditional;
|
import org.springframework.context.annotation.Conditional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -66,22 +68,31 @@ public @interface ConditionalOnBean {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The class types of beans that should be checked. The condition matches when beans
|
* The class types of beans that should be checked. The condition matches when beans
|
||||||
* of all classes specified are contained in the {@link BeanFactory}.
|
* of all classes specified are contained in the {@link BeanFactory}. Beans that are
|
||||||
|
* not autowire candidates are ignored.
|
||||||
* @return the class types of beans to check
|
* @return the class types of beans to check
|
||||||
|
* @see Bean#autowireCandidate()
|
||||||
|
* @see BeanDefinition#isAutowireCandidate
|
||||||
*/
|
*/
|
||||||
Class<?>[] value() default {};
|
Class<?>[] value() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The class type names of beans that should be checked. The condition matches when
|
* The class type names of beans that should be checked. The condition matches when
|
||||||
* beans of all classes specified are contained in the {@link BeanFactory}.
|
* beans of all classes specified are contained in the {@link BeanFactory}. Beans that
|
||||||
|
* are not autowire candidates are ignored.
|
||||||
* @return the class type names of beans to check
|
* @return the class type names of beans to check
|
||||||
|
* @see Bean#autowireCandidate()
|
||||||
|
* @see BeanDefinition#isAutowireCandidate
|
||||||
*/
|
*/
|
||||||
String[] type() default {};
|
String[] type() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The annotation type decorating a bean that should be checked. The condition matches
|
* The annotation type decorating a bean that should be checked. The condition matches
|
||||||
* when all the annotations specified are defined on beans in the {@link BeanFactory}.
|
* when all the annotations specified are defined on beans in the {@link BeanFactory}.
|
||||||
|
* Beans that are not autowire candidates are ignored.
|
||||||
* @return the class-level annotation types to check
|
* @return the class-level annotation types to check
|
||||||
|
* @see Bean#autowireCandidate()
|
||||||
|
* @see BeanDefinition#isAutowireCandidate
|
||||||
*/
|
*/
|
||||||
Class<? extends Annotation>[] annotation() default {};
|
Class<? extends Annotation>[] annotation() default {};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2021 the original author or authors.
|
* Copyright 2012-2024 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.
|
||||||
|
|
@ -24,6 +24,8 @@ import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
|
import org.springframework.beans.factory.config.BeanDefinition;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Conditional;
|
import org.springframework.context.annotation.Conditional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -67,15 +69,21 @@ public @interface ConditionalOnMissingBean {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The class types of beans that should be checked. The condition matches when no bean
|
* The class types of beans that should be checked. The condition matches when no bean
|
||||||
* of each class specified is contained in the {@link BeanFactory}.
|
* of each class specified is contained in the {@link BeanFactory}. Beans that are not
|
||||||
|
* autowire candidates are ignored.
|
||||||
* @return the class types of beans to check
|
* @return the class types of beans to check
|
||||||
|
* @see Bean#autowireCandidate()
|
||||||
|
* @see BeanDefinition#isAutowireCandidate
|
||||||
*/
|
*/
|
||||||
Class<?>[] value() default {};
|
Class<?>[] value() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The class type names of beans that should be checked. The condition matches when no
|
* The class type names of beans that should be checked. The condition matches when no
|
||||||
* bean of each class specified is contained in the {@link BeanFactory}.
|
* bean of each class specified is contained in the {@link BeanFactory}. Beans that
|
||||||
|
* are not autowire candidates are ignored.
|
||||||
* @return the class type names of beans to check
|
* @return the class type names of beans to check
|
||||||
|
* @see Bean#autowireCandidate()
|
||||||
|
* @see BeanDefinition#isAutowireCandidate
|
||||||
*/
|
*/
|
||||||
String[] type() default {};
|
String[] type() default {};
|
||||||
|
|
||||||
|
|
@ -97,8 +105,10 @@ public @interface ConditionalOnMissingBean {
|
||||||
/**
|
/**
|
||||||
* The annotation type decorating a bean that should be checked. The condition matches
|
* The annotation type decorating a bean that should be checked. The condition matches
|
||||||
* when each annotation specified is missing from all beans in the
|
* when each annotation specified is missing from all beans in the
|
||||||
* {@link BeanFactory}.
|
* {@link BeanFactory}. Beans that are not autowire candidates are ignored.
|
||||||
* @return the class-level annotation types to check
|
* @return the class-level annotation types to check
|
||||||
|
* @see Bean#autowireCandidate()
|
||||||
|
* @see BeanDefinition#isAutowireCandidate
|
||||||
*/
|
*/
|
||||||
Class<? extends Annotation>[] annotation() default {};
|
Class<? extends Annotation>[] annotation() default {};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2021 the original author or authors.
|
* Copyright 2012-2024 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.
|
||||||
|
|
@ -23,6 +23,8 @@ import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
|
import org.springframework.beans.factory.config.BeanDefinition;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Conditional;
|
import org.springframework.context.annotation.Conditional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -51,22 +53,28 @@ public @interface ConditionalOnSingleCandidate {
|
||||||
/**
|
/**
|
||||||
* The class type of bean that should be checked. The condition matches if a bean of
|
* The class type of bean that should be checked. The condition matches if a bean of
|
||||||
* the class specified is contained in the {@link BeanFactory} and a primary candidate
|
* the class specified is contained in the {@link BeanFactory} and a primary candidate
|
||||||
* exists in case of multiple instances.
|
* exists in case of multiple instances. Beans that are not autowire candidates are
|
||||||
|
* ignored.
|
||||||
* <p>
|
* <p>
|
||||||
* This attribute may <strong>not</strong> be used in conjunction with
|
* This attribute may <strong>not</strong> be used in conjunction with
|
||||||
* {@link #type()}, but it may be used instead of {@link #type()}.
|
* {@link #type()}, but it may be used instead of {@link #type()}.
|
||||||
* @return the class type of the bean to check
|
* @return the class type of the bean to check
|
||||||
|
* @see Bean#autowireCandidate()
|
||||||
|
* @see BeanDefinition#isAutowireCandidate
|
||||||
*/
|
*/
|
||||||
Class<?> value() default Object.class;
|
Class<?> value() default Object.class;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The class type name of bean that should be checked. The condition matches if a bean
|
* The class type name of bean that should be checked. The condition matches if a bean
|
||||||
* of the class specified is contained in the {@link BeanFactory} and a primary
|
* of the class specified is contained in the {@link BeanFactory} and a primary
|
||||||
* candidate exists in case of multiple instances.
|
* candidate exists in case of multiple instances. Beans that are not autowire
|
||||||
|
* candidates are ignored.
|
||||||
* <p>
|
* <p>
|
||||||
* This attribute may <strong>not</strong> be used in conjunction with
|
* This attribute may <strong>not</strong> be used in conjunction with
|
||||||
* {@link #value()}, but it may be used instead of {@link #value()}.
|
* {@link #value()}, but it may be used instead of {@link #value()}.
|
||||||
* @return the class type name of the bean to check
|
* @return the class type name of the bean to check
|
||||||
|
* @see Bean#autowireCandidate()
|
||||||
|
* @see BeanDefinition#isAutowireCandidate
|
||||||
*/
|
*/
|
||||||
String type() default "";
|
String type() default "";
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,18 +24,21 @@ import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.function.BiPredicate;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
import org.springframework.aop.scope.ScopedProxyUtils;
|
import org.springframework.aop.scope.ScopedProxyUtils;
|
||||||
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;
|
||||||
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||||
import org.springframework.beans.factory.config.BeanDefinition;
|
import org.springframework.beans.factory.config.BeanDefinition;
|
||||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
import org.springframework.beans.factory.config.SingletonBeanRegistry;
|
import org.springframework.beans.factory.config.SingletonBeanRegistry;
|
||||||
|
|
@ -211,26 +214,30 @@ class OnBeanCondition extends FilteringSpringBootCondition implements Configurat
|
||||||
Set<String> beansIgnoredByType = getNamesOfBeansIgnoredByType(classLoader, beanFactory, considerHierarchy,
|
Set<String> beansIgnoredByType = getNamesOfBeansIgnoredByType(classLoader, beanFactory, considerHierarchy,
|
||||||
spec.getIgnoredTypes(), parameterizedContainers);
|
spec.getIgnoredTypes(), parameterizedContainers);
|
||||||
for (String type : spec.getTypes()) {
|
for (String type : spec.getTypes()) {
|
||||||
Collection<String> typeMatches = getBeanNamesForType(classLoader, considerHierarchy, beanFactory, type,
|
Map<String, BeanDefinition> typeMatchedDefinitions = getBeanDefinitionsForType(classLoader,
|
||||||
parameterizedContainers);
|
considerHierarchy, beanFactory, type, parameterizedContainers);
|
||||||
typeMatches
|
Set<String> typeMatchedNames = matchedNamesFrom(typeMatchedDefinitions,
|
||||||
.removeIf((match) -> beansIgnoredByType.contains(match) || ScopedProxyUtils.isScopedTarget(match));
|
(name, definition) -> !beansIgnoredByType.contains(name) && !ScopedProxyUtils.isScopedTarget(name)
|
||||||
if (typeMatches.isEmpty()) {
|
&& (definition == null || definition.isAutowireCandidate()));
|
||||||
|
if (typeMatchedNames.isEmpty()) {
|
||||||
result.recordUnmatchedType(type);
|
result.recordUnmatchedType(type);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
result.recordMatchedType(type, typeMatches);
|
result.recordMatchedType(type, typeMatchedNames);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (String annotation : spec.getAnnotations()) {
|
for (String annotation : spec.getAnnotations()) {
|
||||||
Set<String> annotationMatches = getBeanNamesForAnnotation(classLoader, beanFactory, annotation,
|
Map<String, BeanDefinition> annotationMatchedDefinitions = getBeanDefinitionsForAnnotation(classLoader,
|
||||||
considerHierarchy);
|
beanFactory, annotation, considerHierarchy);
|
||||||
annotationMatches.removeAll(beansIgnoredByType);
|
Set<String> annotationMatchedNames = matchedNamesFrom(annotationMatchedDefinitions,
|
||||||
if (annotationMatches.isEmpty()) {
|
(name, definition) -> !beansIgnoredByType.contains(name)
|
||||||
|
&& (definition == null || definition.isAutowireCandidate()));
|
||||||
|
if (annotationMatchedNames.isEmpty()) {
|
||||||
result.recordUnmatchedAnnotation(annotation);
|
result.recordUnmatchedAnnotation(annotation);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
result.recordMatchedAnnotation(annotation, annotationMatches);
|
result.recordMatchedAnnotation(annotation, annotationMatchedNames);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (String beanName : spec.getNames()) {
|
for (String beanName : spec.getNames()) {
|
||||||
|
|
@ -244,63 +251,76 @@ class OnBeanCondition extends FilteringSpringBootCondition implements Configurat
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Set<String> matchedNamesFrom(Map<String, BeanDefinition> namedDefinitions,
|
||||||
|
BiPredicate<String, BeanDefinition> filter) {
|
||||||
|
Set<String> matchedNames = new LinkedHashSet<>(namedDefinitions.size());
|
||||||
|
for (Entry<String, BeanDefinition> namedDefinition : namedDefinitions.entrySet()) {
|
||||||
|
if (filter.test(namedDefinition.getKey(), namedDefinition.getValue())) {
|
||||||
|
matchedNames.add(namedDefinition.getKey());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return matchedNames;
|
||||||
|
}
|
||||||
|
|
||||||
private Set<String> getNamesOfBeansIgnoredByType(ClassLoader classLoader, ListableBeanFactory beanFactory,
|
private Set<String> getNamesOfBeansIgnoredByType(ClassLoader classLoader, ListableBeanFactory beanFactory,
|
||||||
boolean considerHierarchy, Set<String> ignoredTypes, Set<Class<?>> parameterizedContainers) {
|
boolean considerHierarchy, Set<String> ignoredTypes, Set<Class<?>> parameterizedContainers) {
|
||||||
Set<String> result = null;
|
Set<String> result = null;
|
||||||
for (String ignoredType : ignoredTypes) {
|
for (String ignoredType : ignoredTypes) {
|
||||||
Collection<String> ignoredNames = getBeanNamesForType(classLoader, considerHierarchy, beanFactory,
|
Collection<String> ignoredNames = getBeanDefinitionsForType(classLoader, considerHierarchy, beanFactory,
|
||||||
ignoredType, parameterizedContainers);
|
ignoredType, parameterizedContainers)
|
||||||
|
.keySet();
|
||||||
result = addAll(result, ignoredNames);
|
result = addAll(result, ignoredNames);
|
||||||
}
|
}
|
||||||
return (result != null) ? result : Collections.emptySet();
|
return (result != null) ? result : Collections.emptySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Set<String> getBeanNamesForType(ClassLoader classLoader, boolean considerHierarchy,
|
private Map<String, BeanDefinition> getBeanDefinitionsForType(ClassLoader classLoader, boolean considerHierarchy,
|
||||||
ListableBeanFactory beanFactory, String type, Set<Class<?>> parameterizedContainers) throws LinkageError {
|
ListableBeanFactory beanFactory, String type, Set<Class<?>> parameterizedContainers) throws LinkageError {
|
||||||
try {
|
try {
|
||||||
return getBeanNamesForType(beanFactory, considerHierarchy, resolve(type, classLoader),
|
return getBeanDefinitionsForType(beanFactory, considerHierarchy, resolve(type, classLoader),
|
||||||
parameterizedContainers);
|
parameterizedContainers);
|
||||||
}
|
}
|
||||||
catch (ClassNotFoundException | NoClassDefFoundError ex) {
|
catch (ClassNotFoundException | NoClassDefFoundError ex) {
|
||||||
return Collections.emptySet();
|
return Collections.emptyMap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Set<String> getBeanNamesForType(ListableBeanFactory beanFactory, boolean considerHierarchy, Class<?> type,
|
private Map<String, BeanDefinition> getBeanDefinitionsForType(ListableBeanFactory beanFactory,
|
||||||
Set<Class<?>> parameterizedContainers) {
|
boolean considerHierarchy, Class<?> type, Set<Class<?>> parameterizedContainers) {
|
||||||
Set<String> result = collectBeanNamesForType(beanFactory, considerHierarchy, type, parameterizedContainers,
|
Map<String, BeanDefinition> result = collectBeanDefinitionsForType(beanFactory, considerHierarchy, type,
|
||||||
null);
|
parameterizedContainers, null);
|
||||||
return (result != null) ? result : Collections.emptySet();
|
return (result != null) ? result : Collections.emptyMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Set<String> collectBeanNamesForType(ListableBeanFactory beanFactory, boolean considerHierarchy,
|
private Map<String, BeanDefinition> collectBeanDefinitionsForType(ListableBeanFactory beanFactory,
|
||||||
Class<?> type, Set<Class<?>> parameterizedContainers, Set<String> result) {
|
boolean considerHierarchy, Class<?> type, Set<Class<?>> parameterizedContainers,
|
||||||
result = addAll(result, beanFactory.getBeanNamesForType(type, true, false));
|
Map<String, BeanDefinition> result) {
|
||||||
|
result = putAll(result, beanFactory.getBeanNamesForType(type, true, false), beanFactory);
|
||||||
for (Class<?> container : parameterizedContainers) {
|
for (Class<?> container : parameterizedContainers) {
|
||||||
ResolvableType generic = ResolvableType.forClassWithGenerics(container, type);
|
ResolvableType generic = ResolvableType.forClassWithGenerics(container, type);
|
||||||
result = addAll(result, beanFactory.getBeanNamesForType(generic, true, false));
|
result = putAll(result, beanFactory.getBeanNamesForType(generic, true, false), beanFactory);
|
||||||
}
|
}
|
||||||
if (considerHierarchy && beanFactory instanceof HierarchicalBeanFactory hierarchicalBeanFactory) {
|
if (considerHierarchy && beanFactory instanceof HierarchicalBeanFactory hierarchicalBeanFactory) {
|
||||||
BeanFactory parent = hierarchicalBeanFactory.getParentBeanFactory();
|
BeanFactory parent = hierarchicalBeanFactory.getParentBeanFactory();
|
||||||
if (parent instanceof ListableBeanFactory listableBeanFactory) {
|
if (parent instanceof ListableBeanFactory listableBeanFactory) {
|
||||||
result = collectBeanNamesForType(listableBeanFactory, considerHierarchy, type, parameterizedContainers,
|
result = collectBeanDefinitionsForType(listableBeanFactory, considerHierarchy, type,
|
||||||
result);
|
parameterizedContainers, result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Set<String> getBeanNamesForAnnotation(ClassLoader classLoader, ConfigurableListableBeanFactory beanFactory,
|
private Map<String, BeanDefinition> getBeanDefinitionsForAnnotation(ClassLoader classLoader,
|
||||||
String type, boolean considerHierarchy) throws LinkageError {
|
ConfigurableListableBeanFactory beanFactory, String type, boolean considerHierarchy) throws LinkageError {
|
||||||
Set<String> result = null;
|
Map<String, BeanDefinition> result = null;
|
||||||
try {
|
try {
|
||||||
result = collectBeanNamesForAnnotation(beanFactory, resolveAnnotationType(classLoader, type),
|
result = collectBeanDefinitionsForAnnotation(beanFactory, resolveAnnotationType(classLoader, type),
|
||||||
considerHierarchy, result);
|
considerHierarchy, result);
|
||||||
}
|
}
|
||||||
catch (ClassNotFoundException ex) {
|
catch (ClassNotFoundException ex) {
|
||||||
// Continue
|
// Continue
|
||||||
}
|
}
|
||||||
return (result != null) ? result : Collections.emptySet();
|
return (result != null) ? result : Collections.emptyMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
|
|
@ -309,13 +329,14 @@ class OnBeanCondition extends FilteringSpringBootCondition implements Configurat
|
||||||
return (Class<? extends Annotation>) resolve(type, classLoader);
|
return (Class<? extends Annotation>) resolve(type, classLoader);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Set<String> collectBeanNamesForAnnotation(ListableBeanFactory beanFactory,
|
private Map<String, BeanDefinition> collectBeanDefinitionsForAnnotation(ListableBeanFactory beanFactory,
|
||||||
Class<? extends Annotation> annotationType, boolean considerHierarchy, Set<String> result) {
|
Class<? extends Annotation> annotationType, boolean considerHierarchy, Map<String, BeanDefinition> result) {
|
||||||
result = addAll(result, getBeanNamesForAnnotation(beanFactory, annotationType));
|
result = putAll(result, getBeanNamesForAnnotation(beanFactory, annotationType), beanFactory);
|
||||||
if (considerHierarchy) {
|
if (considerHierarchy) {
|
||||||
BeanFactory parent = ((HierarchicalBeanFactory) beanFactory).getParentBeanFactory();
|
BeanFactory parent = ((HierarchicalBeanFactory) beanFactory).getParentBeanFactory();
|
||||||
if (parent instanceof ListableBeanFactory listableBeanFactory) {
|
if (parent instanceof ListableBeanFactory listableBeanFactory) {
|
||||||
result = collectBeanNamesForAnnotation(listableBeanFactory, annotationType, considerHierarchy, result);
|
result = collectBeanDefinitionsForAnnotation(listableBeanFactory, annotationType, considerHierarchy,
|
||||||
|
result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|
@ -453,12 +474,27 @@ class OnBeanCondition extends FilteringSpringBootCondition implements Configurat
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Set<String> addAll(Set<String> result, String[] additional) {
|
private static Map<String, BeanDefinition> putAll(Map<String, BeanDefinition> result, String[] beanNames,
|
||||||
if (ObjectUtils.isEmpty(additional)) {
|
ListableBeanFactory beanFactory) {
|
||||||
|
if (ObjectUtils.isEmpty(beanNames)) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
result = (result != null) ? result : new LinkedHashSet<>();
|
if (result == null) {
|
||||||
Collections.addAll(result, additional);
|
result = new LinkedHashMap<>();
|
||||||
|
}
|
||||||
|
for (String beanName : beanNames) {
|
||||||
|
if (beanFactory instanceof ConfigurableListableBeanFactory clbf) {
|
||||||
|
try {
|
||||||
|
result.put(beanName, clbf.getBeanDefinition(beanName));
|
||||||
|
}
|
||||||
|
catch (NoSuchBeanDefinitionException ex) {
|
||||||
|
result.put(beanName, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result.put(beanName, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2023 the original author or authors.
|
* Copyright 2012-2024 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.
|
||||||
|
|
@ -43,7 +43,6 @@ import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||||
import org.springframework.context.annotation.ImportResource;
|
import org.springframework.context.annotation.ImportResource;
|
||||||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
||||||
import org.springframework.core.type.AnnotationMetadata;
|
import org.springframework.core.type.AnnotationMetadata;
|
||||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
@ -242,6 +241,26 @@ class ConditionalOnBeanTests {
|
||||||
.satisfies(exampleBeanRequirement("customExampleBean", "conditionalCustomExampleBean")));
|
.satisfies(exampleBeanRequirement("customExampleBean", "conditionalCustomExampleBean")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void conditionalOnBeanTypeIgnoresNotAutowireCandidateBean() {
|
||||||
|
this.contextRunner
|
||||||
|
.withUserConfiguration(NotAutowireCandidateConfiguration.class, OnBeanClassConfiguration.class)
|
||||||
|
.run((context) -> assertThat(context).doesNotHaveBean("bar"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void conditionalOnBeanNameMatchesNotAutowireCandidateBean() {
|
||||||
|
this.contextRunner.withUserConfiguration(NotAutowireCandidateConfiguration.class, OnBeanNameConfiguration.class)
|
||||||
|
.run((context) -> assertThat(context).hasBean("bar"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void conditionalOnAnnotatedBeanIgnoresNotAutowireCandidateBean() {
|
||||||
|
this.contextRunner
|
||||||
|
.withUserConfiguration(AnnotatedNotAutowireCandidateConfig.class, OnAnnotationConfiguration.class)
|
||||||
|
.run((context) -> assertThat(context).doesNotHaveBean("bar"));
|
||||||
|
}
|
||||||
|
|
||||||
private Consumer<ConfigurableApplicationContext> exampleBeanRequirement(String... names) {
|
private Consumer<ConfigurableApplicationContext> exampleBeanRequirement(String... names) {
|
||||||
return (context) -> {
|
return (context) -> {
|
||||||
String[] beans = context.getBeanNamesForType(ExampleBean.class);
|
String[] beans = context.getBeanNamesForType(ExampleBean.class);
|
||||||
|
|
@ -273,7 +292,7 @@ class ConditionalOnBeanTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
@ConditionalOnBean(annotation = EnableScheduling.class)
|
@ConditionalOnBean(annotation = TestAnnotation.class)
|
||||||
static class OnAnnotationConfiguration {
|
static class OnAnnotationConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
@ -317,7 +336,7 @@ class ConditionalOnBeanTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
@EnableScheduling
|
@TestAnnotation
|
||||||
static class FooConfiguration {
|
static class FooConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
@ -327,6 +346,16 @@ class ConditionalOnBeanTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
static class NotAutowireCandidateConfiguration {
|
||||||
|
|
||||||
|
@Bean(autowireCandidate = false)
|
||||||
|
String foo() {
|
||||||
|
return "foo";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
@ImportResource("org/springframework/boot/autoconfigure/condition/foo.xml")
|
@ImportResource("org/springframework/boot/autoconfigure/condition/foo.xml")
|
||||||
static class XmlConfiguration {
|
static class XmlConfiguration {
|
||||||
|
|
@ -530,6 +559,16 @@ class ConditionalOnBeanTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
static class AnnotatedNotAutowireCandidateConfig {
|
||||||
|
|
||||||
|
@Bean(autowireCandidate = false)
|
||||||
|
ExampleBean exampleBean() {
|
||||||
|
return new ExampleBean("value");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@TestAnnotation
|
@TestAnnotation
|
||||||
static class ExampleBean {
|
static class ExampleBean {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2023 the original author or authors.
|
* Copyright 2012-2024 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.
|
||||||
|
|
@ -46,7 +46,6 @@ import org.springframework.context.annotation.Import;
|
||||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||||
import org.springframework.context.annotation.ImportResource;
|
import org.springframework.context.annotation.ImportResource;
|
||||||
import org.springframework.core.type.AnnotationMetadata;
|
import org.springframework.core.type.AnnotationMetadata;
|
||||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
|
@ -345,6 +344,25 @@ class ConditionalOnMissingBeanTests {
|
||||||
.run((context) -> assertThat(context).satisfies(exampleBeanRequirement("customExampleBean")));
|
.run((context) -> assertThat(context).satisfies(exampleBeanRequirement("customExampleBean")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void typeBasedMatchingIgnoresBeanThatIsNotAutowireCandidate() {
|
||||||
|
this.contextRunner.withUserConfiguration(NotAutowireCandidateConfig.class, OnBeanTypeConfiguration.class)
|
||||||
|
.run((context) -> assertThat(context).hasBean("bar"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void nameBasedMatchingConsidersBeanThatIsNotAutowireCandidate() {
|
||||||
|
this.contextRunner.withUserConfiguration(NotAutowireCandidateConfig.class, OnBeanNameConfiguration.class)
|
||||||
|
.run((context) -> assertThat(context).doesNotHaveBean("bar"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void annotationBasedMatchingIgnoresBeanThatIsNotAutowireCandidateBean() {
|
||||||
|
this.contextRunner
|
||||||
|
.withUserConfiguration(AnnotatedNotAutowireCandidateConfig.class, OnAnnotationConfiguration.class)
|
||||||
|
.run((context) -> assertThat(context).hasBean("bar"));
|
||||||
|
}
|
||||||
|
|
||||||
private Consumer<ConfigurableApplicationContext> exampleBeanRequirement(String... names) {
|
private Consumer<ConfigurableApplicationContext> exampleBeanRequirement(String... names) {
|
||||||
return (context) -> {
|
return (context) -> {
|
||||||
String[] beans = context.getBeanNamesForType(ExampleBean.class);
|
String[] beans = context.getBeanNamesForType(ExampleBean.class);
|
||||||
|
|
@ -375,6 +393,17 @@ class ConditionalOnMissingBeanTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@ConditionalOnMissingBean(type = "java.lang.String")
|
||||||
|
static class OnBeanTypeConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
String bar() {
|
||||||
|
return "bar";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
@ConditionalOnMissingBean(name = "foo", value = Date.class)
|
@ConditionalOnMissingBean(name = "foo", value = Date.class)
|
||||||
@ConditionalOnBean(name = "foo", value = Date.class)
|
@ConditionalOnBean(name = "foo", value = Date.class)
|
||||||
|
|
@ -536,7 +565,7 @@ class ConditionalOnMissingBeanTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
@ConditionalOnMissingBean(annotation = EnableScheduling.class)
|
@ConditionalOnMissingBean(annotation = TestAnnotation.class)
|
||||||
static class OnAnnotationConfiguration {
|
static class OnAnnotationConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
@ -558,7 +587,7 @@ class ConditionalOnMissingBeanTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
@EnableScheduling
|
@TestAnnotation
|
||||||
static class FooConfiguration {
|
static class FooConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
@ -568,6 +597,16 @@ class ConditionalOnMissingBeanTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
static class NotAutowireCandidateConfig {
|
||||||
|
|
||||||
|
@Bean(autowireCandidate = false)
|
||||||
|
String foo() {
|
||||||
|
return "foo";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
@ConditionalOnMissingBean(name = "foo")
|
@ConditionalOnMissingBean(name = "foo")
|
||||||
static class HierarchyConsidered {
|
static class HierarchyConsidered {
|
||||||
|
|
@ -731,6 +770,16 @@ class ConditionalOnMissingBeanTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
static class AnnotatedNotAutowireCandidateConfig {
|
||||||
|
|
||||||
|
@Bean(autowireCandidate = false)
|
||||||
|
ExampleBean exampleBean() {
|
||||||
|
return new ExampleBean("value");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@TestAnnotation
|
@TestAnnotation
|
||||||
static class ExampleBean {
|
static class ExampleBean {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -173,6 +173,17 @@ class ConditionalOnSingleCandidateTests {
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void singleCandidateMultipleCandidatesOneAutowireCandidate() {
|
||||||
|
this.contextRunner
|
||||||
|
.withUserConfiguration(AlphaConfiguration.class, BravoNonAutowireConfiguration.class,
|
||||||
|
OnBeanSingleCandidateConfiguration.class)
|
||||||
|
.run((context) -> {
|
||||||
|
assertThat(context).hasBean("consumer");
|
||||||
|
assertThat(context.getBean("consumer")).isEqualTo("alpha");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
@ConditionalOnSingleCandidate(String.class)
|
@ConditionalOnSingleCandidate(String.class)
|
||||||
static class OnBeanSingleCandidateConfiguration {
|
static class OnBeanSingleCandidateConfiguration {
|
||||||
|
|
@ -282,4 +293,14 @@ class ConditionalOnSingleCandidateTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
static class BravoNonAutowireConfiguration {
|
||||||
|
|
||||||
|
@Bean(autowireCandidate = false)
|
||||||
|
String bravo() {
|
||||||
|
return "bravo";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue