From 3641bef9315466faf740593b5152790f56db4954 Mon Sep 17 00:00:00 2001 From: DerekYRC <15521077528@163.com> Date: Thu, 18 Aug 2022 22:05:10 +0800 Subject: [PATCH] BeanFactoryUtils returns all beans including beans defined in ancestor bean factories --- .../cloud/common/util/BeanFactoryUtils.java | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/BeanFactoryUtils.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/BeanFactoryUtils.java index 9a315a341..e7729cf18 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/BeanFactoryUtils.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/BeanFactoryUtils.java @@ -18,15 +18,14 @@ package com.tencent.cloud.common.util; -import java.util.Arrays; -import java.util.Collections; +import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; +import java.util.Map; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.ListableBeanFactory; -import static org.springframework.beans.factory.BeanFactoryUtils.beanNamesForTypeIncludingAncestors; +import static org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors; /** * the utils for bean factory. @@ -43,13 +42,7 @@ public final class BeanFactoryUtils { .getName()); } - String[] beanNames = beanNamesForTypeIncludingAncestors((ListableBeanFactory) beanFactory, requiredType); - if (beanNames.length == 0) { - return Collections.emptyList(); - } - - return Arrays.stream(beanNames).map( - beanName -> beanFactory.getBean(beanName, requiredType) - ).collect(Collectors.toList()); + Map beanMap = beansOfTypeIncludingAncestors((ListableBeanFactory) beanFactory, requiredType); + return new ArrayList<>(beanMap.values()); } }