Browse Source

Defensively catch NoSuchBeanDefinitionException on beanDefinitionNames traversal

Closes gh-22263

(cherry picked from commit 30bc5e09e7)
5.0.x
Juergen Hoeller 6 years ago
parent
commit
824ffce135
  1. 30
      spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

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

@ -454,6 +454,9 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
} }
onSuppressedException(ex); onSuppressedException(ex);
} }
catch (NoSuchBeanDefinitionException ex) {
// Bean definition got removed while we were iterating -> ignore.
}
} }
} }
@ -540,7 +543,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
public String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType) { public String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType) {
List<String> result = new ArrayList<>(); List<String> result = new ArrayList<>();
for (String beanName : this.beanDefinitionNames) { for (String beanName : this.beanDefinitionNames) {
BeanDefinition beanDefinition = getBeanDefinition(beanName); BeanDefinition beanDefinition = this.beanDefinitionMap.get(beanName);
if (!beanDefinition.isAbstract() && findAnnotationOnBean(beanName, annotationType) != null) { if (!beanDefinition.isAbstract() && findAnnotationOnBean(beanName, annotationType) != null) {
result.add(beanName); result.add(beanName);
} }
@ -1511,18 +1514,23 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
*/ */
private void checkBeanNotOfRequiredType(Class<?> type, DependencyDescriptor descriptor) { private void checkBeanNotOfRequiredType(Class<?> type, DependencyDescriptor descriptor) {
for (String beanName : this.beanDefinitionNames) { for (String beanName : this.beanDefinitionNames) {
RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); try {
Class<?> targetType = mbd.getTargetType(); RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
if (targetType != null && type.isAssignableFrom(targetType) && Class<?> targetType = mbd.getTargetType();
isAutowireCandidate(beanName, mbd, descriptor, getAutowireCandidateResolver())) { if (targetType != null && type.isAssignableFrom(targetType) &&
// Probably a proxy interfering with target type match -> throw meaningful exception. isAutowireCandidate(beanName, mbd, descriptor, getAutowireCandidateResolver())) {
Object beanInstance = getSingleton(beanName, false); // Probably a proxy interfering with target type match -> throw meaningful exception.
Class<?> beanType = (beanInstance != null && beanInstance.getClass() != NullBean.class ? Object beanInstance = getSingleton(beanName, false);
beanInstance.getClass() : predictBeanType(beanName, mbd)); Class<?> beanType = (beanInstance != null && beanInstance.getClass() != NullBean.class ?
if (beanType != null && !type.isAssignableFrom(beanType)) { beanInstance.getClass() : predictBeanType(beanName, mbd));
throw new BeanNotOfRequiredTypeException(beanName, type, beanType); if (beanType != null && !type.isAssignableFrom(beanType)) {
throw new BeanNotOfRequiredTypeException(beanName, type, beanType);
}
} }
} }
catch (NoSuchBeanDefinitionException ex) {
// Bean definition got removed while we were iterating -> ignore.
}
} }
BeanFactory parent = getParentBeanFactory(); BeanFactory parent = getParentBeanFactory();

Loading…
Cancel
Save