ListableBeanFactory.getBeansWithAnnotation does not include null beans

Issue: SPR-17034
This commit is contained in:
Juergen Hoeller 2018-08-10 15:45:37 +02:00
parent c437a0d1c3
commit def6fbba89
1 changed files with 11 additions and 8 deletions

View File

@ -605,29 +605,32 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
@Override @Override
public String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType) { public String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType) {
List<String> results = new ArrayList<>(); List<String> result = new ArrayList<>();
for (String beanName : this.beanDefinitionNames) { for (String beanName : this.beanDefinitionNames) {
BeanDefinition beanDefinition = getBeanDefinition(beanName); BeanDefinition beanDefinition = getBeanDefinition(beanName);
if (!beanDefinition.isAbstract() && findAnnotationOnBean(beanName, annotationType) != null) { if (!beanDefinition.isAbstract() && findAnnotationOnBean(beanName, annotationType) != null) {
results.add(beanName); result.add(beanName);
} }
} }
for (String beanName : this.manualSingletonNames) { for (String beanName : this.manualSingletonNames) {
if (!results.contains(beanName) && findAnnotationOnBean(beanName, annotationType) != null) { if (!result.contains(beanName) && findAnnotationOnBean(beanName, annotationType) != null) {
results.add(beanName); result.add(beanName);
} }
} }
return StringUtils.toStringArray(results); return StringUtils.toStringArray(result);
} }
@Override @Override
public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) { public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) {
String[] beanNames = getBeanNamesForAnnotation(annotationType); String[] beanNames = getBeanNamesForAnnotation(annotationType);
Map<String, Object> results = new LinkedHashMap<>(beanNames.length); Map<String, Object> result = new LinkedHashMap<>(beanNames.length);
for (String beanName : beanNames) { for (String beanName : beanNames) {
results.put(beanName, getBean(beanName)); Object beanInstance = getBean(beanName);
if (!(beanInstance instanceof NullBean)) {
result.put(beanName, beanInstance);
}
} }
return results; return result;
} }
/** /**