BeanFactoryUtils returns all beans including beans defined in ancestor bean factories

pull/515/head
DerekYRC 3 years ago
parent 1a919e29d7
commit 3641bef931

@ -18,15 +18,14 @@
package com.tencent.cloud.common.util; package com.tencent.cloud.common.util;
import java.util.Arrays; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.Map;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory; 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. * the utils for bean factory.
@ -43,13 +42,7 @@ public final class BeanFactoryUtils {
.getName()); .getName());
} }
String[] beanNames = beanNamesForTypeIncludingAncestors((ListableBeanFactory) beanFactory, requiredType); Map<String, T> beanMap = beansOfTypeIncludingAncestors((ListableBeanFactory) beanFactory, requiredType);
if (beanNames.length == 0) { return new ArrayList<>(beanMap.values());
return Collections.emptyList();
}
return Arrays.stream(beanNames).map(
beanName -> beanFactory.getBean(beanName, requiredType)
).collect(Collectors.toList());
} }
} }

Loading…
Cancel
Save