Skip to content

Commit 50c2577

Browse files
philwebbjhoeller
authored andcommitted
Migrate DefaultListableBeanFactory to MergedAnnotations
Closes gh-22584
1 parent 857371b commit 50c2577

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@
7272
import org.springframework.beans.factory.config.NamedBeanHolder;
7373
import org.springframework.core.OrderComparator;
7474
import org.springframework.core.ResolvableType;
75-
import org.springframework.core.annotation.AnnotationUtils;
75+
import org.springframework.core.annotation.MergedAnnotation;
76+
import org.springframework.core.annotation.MergedAnnotations;
77+
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
7678
import org.springframework.lang.Nullable;
7779
import org.springframework.util.Assert;
7880
import org.springframework.util.ClassUtils;
@@ -666,22 +668,33 @@ public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> an
666668
@Nullable
667669
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
668670
throws NoSuchBeanDefinitionException {
671+
return findMergedAnnotationOnBean(beanName, annotationType).synthesize(
672+
MergedAnnotation::isPresent).orElse(null);
673+
}
669674

670-
A ann = null;
675+
private <A extends Annotation> MergedAnnotation<A> findMergedAnnotationOnBean(
676+
String beanName, Class<A> annotationType) {
671677
Class<?> beanType = getType(beanName);
672678
if (beanType != null) {
673-
ann = AnnotationUtils.findAnnotation(beanType, annotationType);
679+
MergedAnnotation<A> annotation = MergedAnnotations.from(beanType,
680+
SearchStrategy.EXHAUSTIVE).get(annotationType);
681+
if (annotation.isPresent()) {
682+
return annotation;
683+
}
674684
}
675-
if (ann == null && containsBeanDefinition(beanName)) {
685+
if (containsBeanDefinition(beanName)) {
676686
BeanDefinition bd = getMergedBeanDefinition(beanName);
677687
if (bd instanceof AbstractBeanDefinition) {
678688
AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
679689
if (abd.hasBeanClass()) {
680-
ann = AnnotationUtils.findAnnotation(abd.getBeanClass(), annotationType);
690+
Class<?> beanClass = abd.getBeanClass();
691+
if (beanClass != beanType) {
692+
return MergedAnnotations.from(beanClass, SearchStrategy.EXHAUSTIVE).get(annotationType);
693+
}
681694
}
682695
}
683696
}
684-
return ann;
697+
return MergedAnnotation.missing();
685698
}
686699

687700

0 commit comments

Comments
 (0)