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 4d4ef662c5..fe819043a7 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 @@ -505,6 +505,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto } @Override + @SuppressWarnings("unchecked") public Map getBeansOfType(@Nullable Class type, boolean includeNonSingletons, boolean allowEagerInit) throws BeansException { @@ -512,7 +513,8 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto Map result = new LinkedHashMap<>(beanNames.length); for (String beanName : beanNames) { try { - result.put(beanName, getBean(beanName, type)); + Object beanInstance = getBean(beanName); + result.put(beanName, (beanInstance instanceof NullBean ? null : (T) beanInstance)); } catch (BeanCreationException ex) { Throwable rootCause = ex.getMostSpecificCause(); diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java index da27d15171..a374b1e65f 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java @@ -701,6 +701,11 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertEquals(2, bean.getNestedTestBeans().size()); assertNull(bean.getNestedTestBeans().get(0)); assertSame(ntb2, bean.getNestedTestBeans().get(1)); + + Map map = bf.getBeansOfType(NestedTestBean.class); + assertNull(map.get("nestedTestBean1")); + assertSame(ntb2, map.get("nestedTestBean2")); + bf.destroySingletons(); }