From 50c257794f7845829ac9ce78a102ef94e7e28a2e Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 8 Mar 2019 17:09:13 -0800 Subject: [PATCH] Migrate DefaultListableBeanFactory to MergedAnnotations Closes gh-22584 --- .../support/DefaultListableBeanFactory.java | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 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 416a40bd19..9e02bf1e5d 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 @@ -72,7 +72,9 @@ import org.springframework.beans.factory.config.DependencyDescriptor; import org.springframework.beans.factory.config.NamedBeanHolder; import org.springframework.core.OrderComparator; import org.springframework.core.ResolvableType; -import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.core.annotation.MergedAnnotation; +import org.springframework.core.annotation.MergedAnnotations; +import org.springframework.core.annotation.MergedAnnotations.SearchStrategy; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; @@ -666,22 +668,33 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto @Nullable public A findAnnotationOnBean(String beanName, Class annotationType) throws NoSuchBeanDefinitionException { + return findMergedAnnotationOnBean(beanName, annotationType).synthesize( + MergedAnnotation::isPresent).orElse(null); + } - A ann = null; + private MergedAnnotation findMergedAnnotationOnBean( + String beanName, Class annotationType) { Class beanType = getType(beanName); if (beanType != null) { - ann = AnnotationUtils.findAnnotation(beanType, annotationType); + MergedAnnotation annotation = MergedAnnotations.from(beanType, + SearchStrategy.EXHAUSTIVE).get(annotationType); + if (annotation.isPresent()) { + return annotation; + } } - if (ann == null && containsBeanDefinition(beanName)) { + if (containsBeanDefinition(beanName)) { BeanDefinition bd = getMergedBeanDefinition(beanName); if (bd instanceof AbstractBeanDefinition) { AbstractBeanDefinition abd = (AbstractBeanDefinition) bd; if (abd.hasBeanClass()) { - ann = AnnotationUtils.findAnnotation(abd.getBeanClass(), annotationType); + Class beanClass = abd.getBeanClass(); + if (beanClass != beanType) { + return MergedAnnotations.from(beanClass, SearchStrategy.EXHAUSTIVE).get(annotationType); + } } } } - return ann; + return MergedAnnotation.missing(); }