Introduce findAllAnnotationsOnBean variant on ListableBeanFactory

Closes gh-29446
This commit is contained in:
Juergen Hoeller
2022-11-08 13:29:33 +01:00
parent e1010a179f
commit 86d45578d9
8 changed files with 109 additions and 80 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ package org.springframework.beans.factory;
import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.BeansException;
import org.springframework.core.ResolvableType;
@@ -338,6 +339,8 @@ public interface ListableBeanFactory extends BeanFactory {
* @throws BeansException if a bean could not be created
* @since 3.0
* @see #findAnnotationOnBean
* @see #findAllAnnotationsOnBean(String, Class, boolean)
* @see #findAllAnnotationsOnBean(String, Class, boolean)
*/
Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException;
@@ -380,4 +383,24 @@ public interface ListableBeanFactory extends BeanFactory {
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit)
throws NoSuchBeanDefinitionException;
/**
* Find all {@link Annotation} instances of {@code annotationType} on the specified
* bean, traversing its interfaces and superclasses if no annotation can be found on
* the given class itself, as well as checking the bean's factory method (if any).
* @param beanName the name of the bean to look for annotations on
* @param annotationType the type of annotation to look for
* (at class, interface or factory method level of the specified bean)
* @param allowFactoryBeanInit whether a {@code FactoryBean} may get initialized
* just for the purpose of determining its object type
* @return the set of annotations of the given type found (potentially empty)
* @throws NoSuchBeanDefinitionException if there is no bean with the given name
* @since 6.0
* @see #getBeanNamesForAnnotation
* @see #findAnnotationOnBean(String, Class, boolean)
* @see #getType(String, boolean)
*/
<A extends Annotation> Set<A> findAllAnnotationsOnBean(
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit)
throws NoSuchBeanDefinitionException;
}

View File

@@ -729,19 +729,12 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit)
throws NoSuchBeanDefinitionException {
return findMergedAnnotationOnBean(beanName, annotationType, allowFactoryBeanInit)
.synthesize(MergedAnnotation::isPresent).orElse(null);
}
private <A extends Annotation> MergedAnnotation<A> findMergedAnnotationOnBean(
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit) {
Class<?> beanType = getType(beanName, allowFactoryBeanInit);
if (beanType != null) {
MergedAnnotation<A> annotation =
MergedAnnotations.from(beanType, SearchStrategy.TYPE_HIERARCHY).get(annotationType);
if (annotation.isPresent()) {
return annotation;
return annotation.synthesize();
}
}
if (containsBeanDefinition(beanName)) {
@@ -753,7 +746,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
MergedAnnotation<A> annotation =
MergedAnnotations.from(beanClass, SearchStrategy.TYPE_HIERARCHY).get(annotationType);
if (annotation.isPresent()) {
return annotation;
return annotation.synthesize();
}
}
}
@@ -763,11 +756,48 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
MergedAnnotation<A> annotation =
MergedAnnotations.from(factoryMethod, SearchStrategy.TYPE_HIERARCHY).get(annotationType);
if (annotation.isPresent()) {
return annotation;
return annotation.synthesize();
}
}
}
return MergedAnnotation.missing();
return null;
}
@Override
public <A extends Annotation> Set<A> findAllAnnotationsOnBean(
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit)
throws NoSuchBeanDefinitionException {
Set<A> annotations = new LinkedHashSet<>();
Class<?> beanType = getType(beanName, allowFactoryBeanInit);
if (beanType != null) {
MergedAnnotations.from(beanType, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
.stream(annotationType)
.filter(MergedAnnotation::isPresent)
.forEach(mergedAnnotation -> annotations.add(mergedAnnotation.synthesize()));
}
if (containsBeanDefinition(beanName)) {
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
// Check raw bean class, e.g. in case of a proxy.
if (bd.hasBeanClass() && bd.getFactoryMethodName() == null) {
Class<?> beanClass = bd.getBeanClass();
if (beanClass != beanType) {
MergedAnnotations.from(beanClass, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
.stream(annotationType)
.filter(MergedAnnotation::isPresent)
.forEach(mergedAnnotation -> annotations.add(mergedAnnotation.synthesize()));
}
}
// Check annotations declared on factory method, if any.
Method factoryMethod = bd.getResolvedFactoryMethod();
if (factoryMethod != null) {
MergedAnnotations.from(factoryMethod, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
.stream(annotationType)
.filter(MergedAnnotation::isPresent)
.forEach(mergedAnnotation -> annotations.add(mergedAnnotation.synthesize()));
}
}
return annotations;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,9 +19,11 @@ package org.springframework.beans.factory.support;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
import org.springframework.beans.BeansException;
@@ -472,4 +474,13 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
return (beanType != null ? AnnotatedElementUtils.findMergedAnnotation(beanType, annotationType) : null);
}
@Override
public <A extends Annotation> Set<A> findAllAnnotationsOnBean(
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException {
Class<?> beanType = getType(beanName, allowFactoryBeanInit);
return (beanType != null ?
AnnotatedElementUtils.findAllMergedAnnotations(beanType, annotationType) : Collections.emptySet());
}
}