Defensively catch NoSuchBeanDefinitionException on beanDefinitionNames traversal

Closes gh-22263
This commit is contained in:
Juergen Hoeller 2020-07-19 19:56:33 +02:00
parent 7b6924522a
commit 30bc5e09e7
1 changed files with 19 additions and 11 deletions

View File

@ -558,6 +558,9 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
// Register exception, in case the bean was accidentally unresolvable.
onSuppressedException(ex);
}
catch (NoSuchBeanDefinitionException ex) {
// Bean definition got removed while we were iterating -> ignore.
}
}
}
@ -649,7 +652,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
public String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType) {
List<String> result = new ArrayList<>();
for (String beanName : this.beanDefinitionNames) {
BeanDefinition beanDefinition = getBeanDefinition(beanName);
BeanDefinition beanDefinition = this.beanDefinitionMap.get(beanName);
if (!beanDefinition.isAbstract() && findAnnotationOnBean(beanName, annotationType) != null) {
result.add(beanName);
}
@ -1721,6 +1724,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
*/
private void checkBeanNotOfRequiredType(Class<?> type, DependencyDescriptor descriptor) {
for (String beanName : this.beanDefinitionNames) {
try {
RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
Class<?> targetType = mbd.getTargetType();
if (targetType != null && type.isAssignableFrom(targetType) &&
@ -1734,6 +1738,10 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
}
}
}
catch (NoSuchBeanDefinitionException ex) {
// Bean definition got removed while we were iterating -> ignore.
}
}
BeanFactory parent = getParentBeanFactory();
if (parent instanceof DefaultListableBeanFactory) {