From be2d31d062cda878a235eeb2acbb190405d3f2d6 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sun, 19 Jul 2020 19:56:33 +0200 Subject: [PATCH] Defensively catch NoSuchBeanDefinitionException on beanDefinitionNames traversal Closes gh-22263 (cherry picked from commit 30bc5e09e72c340f92e224515f1198bd88ab6a3d) --- .../support/DefaultListableBeanFactory.java | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) 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 4c32655baab1..22052dd50b7d 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 @@ -550,6 +550,9 @@ private String[] doGetBeanNamesForType(ResolvableType type, boolean includeNonSi } onSuppressedException(ex); } + catch (NoSuchBeanDefinitionException ex) { + // Bean definition got removed while we were iterating -> ignore. + } } } @@ -638,7 +641,7 @@ public Map getBeansOfType( public String[] getBeanNamesForAnnotation(Class annotationType) { List 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); } @@ -1684,18 +1687,23 @@ private void raiseNoMatchingBeanFound( */ private void checkBeanNotOfRequiredType(Class type, DependencyDescriptor descriptor) { for (String beanName : this.beanDefinitionNames) { - RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); - Class targetType = mbd.getTargetType(); - if (targetType != null && type.isAssignableFrom(targetType) && - isAutowireCandidate(beanName, mbd, descriptor, getAutowireCandidateResolver())) { - // Probably a proxy interfering with target type match -> throw meaningful exception. - Object beanInstance = getSingleton(beanName, false); - Class beanType = (beanInstance != null && beanInstance.getClass() != NullBean.class ? - beanInstance.getClass() : predictBeanType(beanName, mbd)); - if (beanType != null && !type.isAssignableFrom(beanType)) { - throw new BeanNotOfRequiredTypeException(beanName, type, beanType); + try { + RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); + Class targetType = mbd.getTargetType(); + if (targetType != null && type.isAssignableFrom(targetType) && + isAutowireCandidate(beanName, mbd, descriptor, getAutowireCandidateResolver())) { + // Probably a proxy interfering with target type match -> throw meaningful exception. + Object beanInstance = getSingleton(beanName, false); + Class beanType = (beanInstance != null && beanInstance.getClass() != NullBean.class ? + beanInstance.getClass() : predictBeanType(beanName, mbd)); + 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();