diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/SimpleAutowireCandidateResolver.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/SimpleAutowireCandidateResolver.java index 12041b3686..a1f438195a 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/SimpleAutowireCandidateResolver.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/SimpleAutowireCandidateResolver.java @@ -66,10 +66,40 @@ public class SimpleAutowireCandidateResolver implements AutowireCandidateResolve * @see org.springframework.beans.factory.config.BeanDefinition#isAutowireCandidate() * @see AbstractBeanDefinition#isDefaultCandidate() */ - @SuppressWarnings("unchecked") public static Map resolveAutowireCandidates(ConfigurableListableBeanFactory lbf, Class type) { + return resolveAutowireCandidates(lbf, type, true, true); + } + + /** + * Resolve a map of all beans of the given type, also picking up beans defined in + * ancestor bean factories, with the specific condition that each bean actually + * has autowire candidate status. This matches simple injection point resolution + * as implemented by this {@link AutowireCandidateResolver} strategy, including + * beans which are not marked as default candidates but excluding beans which + * are not even marked as autowire candidates. + * @param lbf the bean factory + * @param type the type of bean to match + * @param includeNonSingletons whether to include prototype or scoped beans too + * or just singletons (also applies to FactoryBeans) + * @param allowEagerInit whether to initialize lazy-init singletons and + * objects created by FactoryBeans (or by factory methods with a + * "factory-bean" reference) for the type check. Note that FactoryBeans need to be + * eagerly initialized to determine their type: So be aware that passing in "true" + * for this flag will initialize FactoryBeans and "factory-bean" references. + * @return the Map of matching bean instances, or an empty Map if none + * @throws BeansException if a bean could not be created + * @since 6.2.5 + * @see BeanFactoryUtils#beansOfTypeIncludingAncestors(ListableBeanFactory, Class, boolean, boolean) + * @see org.springframework.beans.factory.config.BeanDefinition#isAutowireCandidate() + * @see AbstractBeanDefinition#isDefaultCandidate() + */ + @SuppressWarnings("unchecked") + public static Map resolveAutowireCandidates(ConfigurableListableBeanFactory lbf, Class type, + boolean includeNonSingletons, boolean allowEagerInit) { + Map candidates = new LinkedHashMap<>(); - for (String beanName : BeanFactoryUtils.beanNamesForTypeIncludingAncestors(lbf, type)) { + for (String beanName : BeanFactoryUtils.beanNamesForTypeIncludingAncestors(lbf, type, + includeNonSingletons, allowEagerInit)) { if (AutowireUtils.isAutowireCandidate(lbf, beanName)) { Object beanInstance = lbf.getBean(beanName); if (!(beanInstance instanceof NullBean)) {