From 452bbdd49bb9f044f08a3a7bc203ad5279c581a6 Mon Sep 17 00:00:00 2001 From: DerekYRC <44155264+DerekYRC@users.noreply.github.com> Date: Sat, 20 Aug 2022 23:25:42 +0800 Subject: [PATCH] BeanFactoryUtils returns all beans including beans defined in ancestor bean factories (#517) --- CHANGELOG.md | 1 + .../cloud/common/util/BeanFactoryUtils.java | 23 +++++------- .../common/util/BeanFactoryUtilsTest.java | 36 +++++++++++++++++++ 3 files changed, 45 insertions(+), 15 deletions(-) create mode 100644 spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/util/BeanFactoryUtilsTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index a32dd53c..ec185458 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,4 +16,5 @@ - [Bugfix: update byte-buddy scope test to compile](https://github.com/Tencent/spring-cloud-tencent/pull/498) - [Fix the code analysis error.](https://github.com/Tencent/spring-cloud-tencent/pull/500) - [Fix typo & Code optimization](https://github.com/Tencent/spring-cloud-tencent/pull/512) +- [BeanFactoryUtils returns all beans including beans defined in ancestor bean factories](https://github.com/Tencent/spring-cloud-tencent/pull/517) - [fix:add spring-cloud-starter-bootstrap dependency for example](https://github.com/Tencent/spring-cloud-tencent/pull/521) 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 7bfd0c87..e7729cf1 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,17 +18,17 @@ 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.support.DefaultListableBeanFactory; +import org.springframework.beans.factory.ListableBeanFactory; + +import static org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors; /** * the utils for bean factory. - * * @author lepdou 2022-05-23 */ public final class BeanFactoryUtils { @@ -37,19 +37,12 @@ public final class BeanFactoryUtils { } public static List getBeans(BeanFactory beanFactory, Class requiredType) { - if (!(beanFactory instanceof DefaultListableBeanFactory)) { + if (!(beanFactory instanceof ListableBeanFactory)) { throw new RuntimeException("bean factory not support get list bean. factory type = " + beanFactory.getClass() .getName()); } - String[] beanNames = ((DefaultListableBeanFactory) beanFactory).getBeanNamesForType(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()); } } diff --git a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/util/BeanFactoryUtilsTest.java b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/util/BeanFactoryUtilsTest.java new file mode 100644 index 00000000..aa1b4b49 --- /dev/null +++ b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/util/BeanFactoryUtilsTest.java @@ -0,0 +1,36 @@ +package com.tencent.cloud.common.util; + +import org.junit.Assert; +import org.junit.Test; + +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.factory.support.RootBeanDefinition; + +/** + * Test for {@link BeanFactoryUtils}. + * + * @author Derek Yi 2022-08-18 + */ +public class BeanFactoryUtilsTest { + + @Test + public void testGetBeansIncludingAncestors() { + DefaultListableBeanFactory parentBeanFactory = new DefaultListableBeanFactory(); + parentBeanFactory.registerBeanDefinition("foo", new RootBeanDefinition(Foo.class)); + + DefaultListableBeanFactory childBeanFactory = new DefaultListableBeanFactory(parentBeanFactory); + + Assert.assertTrue(childBeanFactory.getBeansOfType(Foo.class).isEmpty()); + Assert.assertTrue(BeanFactoryUtils.getBeans(childBeanFactory, Foo.class).size() == 1); + + Assert.assertTrue(BeanFactoryUtils.getBeans(childBeanFactory, Bar.class).isEmpty()); + } + + static class Foo { + + } + + static class Bar { + + } +}