Skip to content

Commit def6fbb

Browse files
committed
ListableBeanFactory.getBeansWithAnnotation does not include null beans
Issue: SPR-17034
1 parent c437a0d commit def6fbb

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -605,29 +605,32 @@ public <T> Map<String, T> getBeansOfType(@Nullable Class<T> type, boolean includ
605605

606606
@Override
607607
public String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType) {
608-
List<String> results = new ArrayList<>();
608+
List<String> result = new ArrayList<>();
609609
for (String beanName : this.beanDefinitionNames) {
610610
BeanDefinition beanDefinition = getBeanDefinition(beanName);
611611
if (!beanDefinition.isAbstract() && findAnnotationOnBean(beanName, annotationType) != null) {
612-
results.add(beanName);
612+
result.add(beanName);
613613
}
614614
}
615615
for (String beanName : this.manualSingletonNames) {
616-
if (!results.contains(beanName) && findAnnotationOnBean(beanName, annotationType) != null) {
617-
results.add(beanName);
616+
if (!result.contains(beanName) && findAnnotationOnBean(beanName, annotationType) != null) {
617+
result.add(beanName);
618618
}
619619
}
620-
return StringUtils.toStringArray(results);
620+
return StringUtils.toStringArray(result);
621621
}
622622

623623
@Override
624624
public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) {
625625
String[] beanNames = getBeanNamesForAnnotation(annotationType);
626-
Map<String, Object> results = new LinkedHashMap<>(beanNames.length);
626+
Map<String, Object> result = new LinkedHashMap<>(beanNames.length);
627627
for (String beanName : beanNames) {
628-
results.put(beanName, getBean(beanName));
628+
Object beanInstance = getBean(beanName);
629+
if (!(beanInstance instanceof NullBean)) {
630+
result.put(beanName, beanInstance);
631+
}
629632
}
630-
return results;
633+
return result;
631634
}
632635

633636
/**

0 commit comments

Comments
 (0)