From 0ce9516aeff0f4bd8dd37770d161671e23a54304 Mon Sep 17 00:00:00 2001 From: Fabian Gonzalez Date: Mon, 13 Jun 2022 18:37:58 -0300 Subject: [PATCH] Avoid eager instantiation of non-singleton FactoryBean in getBeanNamesForType Closes gh-28616 --- .../beans/factory/support/DefaultListableBeanFactory.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java index b019ef5361f3..53fb4d1d36ee 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java @@ -585,7 +585,8 @@ private String[] doGetBeanNamesForType(ResolvableType type, boolean includeNonSi if (!matchFound) { // In case of FactoryBean, try to match FactoryBean instance itself next. beanName = FACTORY_BEAN_PREFIX + beanName; - matchFound = isTypeMatch(beanName, type, allowFactoryBeanInit); + matchFound = (includeNonSingletons || isSingleton(beanName, mbd, dbd)) && isTypeMatch(beanName, type, allowFactoryBeanInit); + } } if (matchFound) {