BeanFactoryUtils returns all beans including beans defined in ancestor bean factories (#516)

pull/525/head
DerekYRC 2 years ago committed by GitHub
parent e1e3ab0060
commit bd3dd3f92c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -17,4 +17,5 @@
- [Fix the code analysis error.](https://github.com/Tencent/spring-cloud-tencent/pull/499) - [Fix the code analysis error.](https://github.com/Tencent/spring-cloud-tencent/pull/499)
- [Optimize router label resolver spi](https://github.com/Tencent/spring-cloud-tencent/pull/503) - [Optimize router label resolver spi](https://github.com/Tencent/spring-cloud-tencent/pull/503)
- [Fix typo & Code optimization](https://github.com/Tencent/spring-cloud-tencent/pull/511) - [Fix typo & Code optimization](https://github.com/Tencent/spring-cloud-tencent/pull/511)
- [BeanFactoryUtils returns all beans including beans defined in ancestor bean factories](https://github.com/Tencent/spring-cloud-tencent/pull/516)
- [Feature: Add router actuate endpoint](https://github.com/Tencent/spring-cloud-tencent/pull/519) - [Feature: Add router actuate endpoint](https://github.com/Tencent/spring-cloud-tencent/pull/519)

@ -18,17 +18,17 @@
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.support.DefaultListableBeanFactory; import org.springframework.beans.factory.ListableBeanFactory;
import static org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors;
/** /**
* the utils for bean factory. * the utils for bean factory.
*
* @author lepdou 2022-05-23 * @author lepdou 2022-05-23
*/ */
public final class BeanFactoryUtils { public final class BeanFactoryUtils {
@ -37,19 +37,12 @@ public final class BeanFactoryUtils {
} }
public static <T> List<T> getBeans(BeanFactory beanFactory, Class<T> requiredType) { public static <T> List<T> getBeans(BeanFactory beanFactory, Class<T> requiredType) {
if (!(beanFactory instanceof DefaultListableBeanFactory)) { if (!(beanFactory instanceof ListableBeanFactory)) {
throw new RuntimeException("bean factory not support get list bean. factory type = " + beanFactory.getClass() throw new RuntimeException("bean factory not support get list bean. factory type = " + beanFactory.getClass()
.getName()); .getName());
} }
String[] beanNames = ((DefaultListableBeanFactory) beanFactory).getBeanNamesForType(requiredType); Map<String, T> beanMap = beansOfTypeIncludingAncestors((ListableBeanFactory) beanFactory, requiredType);
return new ArrayList<>(beanMap.values());
if (beanNames.length == 0) {
return Collections.emptyList();
}
return Arrays.stream(beanNames).map(
beanName -> beanFactory.getBean(beanName, requiredType)
).collect(Collectors.toList());
} }
} }

@ -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 {
}
}
Loading…
Cancel
Save