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());
}
}

View File

@@ -61,7 +61,7 @@ import org.springframework.aot.hint.RuntimeHintsRegistrar;
* @since 6.0
* @see org.springframework.aot.hint.RuntimeHints
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ImportRuntimeHints {

View File

@@ -16,12 +16,8 @@
package org.springframework.context.aot;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -36,12 +32,8 @@ import org.springframework.beans.factory.aot.AotServices;
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution;
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor;
import org.springframework.beans.factory.aot.BeanFactoryInitializationCode;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.log.LogMessage;
import org.springframework.lang.Nullable;
@@ -54,6 +46,8 @@ import org.springframework.lang.Nullable;
*
* @author Brian Clozel
* @author Sebastien Deleuze
* @author Juergen Hoeller
* @since 6.0
*/
class RuntimeHintsBeanFactoryInitializationAotProcessor implements BeanFactoryInitializationAotProcessor {
@@ -67,67 +61,26 @@ class RuntimeHintsBeanFactoryInitializationAotProcessor implements BeanFactoryIn
.collect(LinkedHashMap::new, (map, item) -> map.put(item.getClass(), item), Map::putAll);
extractFromBeanFactory(beanFactory).forEach(registrarClass ->
registrars.computeIfAbsent(registrarClass, BeanUtils::instantiateClass));
return new RuntimeHintsRegistrarContribution(registrars.values(),
beanFactory.getBeanClassLoader());
return new RuntimeHintsRegistrarContribution(registrars.values(), beanFactory.getBeanClassLoader());
}
private Set<Class<? extends RuntimeHintsRegistrar>> extractFromBeanFactory(ConfigurableListableBeanFactory beanFactory) {
Set<Class<? extends RuntimeHintsRegistrar>> registrarClasses = new LinkedHashSet<>();
for (String beanName : beanFactory
.getBeanNamesForAnnotation(ImportRuntimeHints.class)) {
findAnnotationsOnBean(beanFactory, beanName,
ImportRuntimeHints.class).forEach(annotation ->
registrarClasses.addAll(extractFromBeanDefinition(beanName, annotation)));
for (String beanName : beanFactory.getBeanDefinitionNames()) {
beanFactory.findAllAnnotationsOnBean(beanName, ImportRuntimeHints.class, true)
.forEach(annotation -> registrarClasses.addAll(extractFromBeanDefinition(beanName, annotation)));
}
return registrarClasses;
}
private <A extends Annotation> List<A> findAnnotationsOnBean(ConfigurableListableBeanFactory beanFactory,
String beanName, Class<A> annotationType) {
List<A> annotations = new ArrayList<>();
Class<?> beanType = beanFactory.getType(beanName, true);
if (beanType != null) {
MergedAnnotations.from(beanType, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
.stream(annotationType)
.filter(MergedAnnotation::isPresent)
.forEach(mergedAnnotation -> annotations.add(mergedAnnotation.synthesize()));
}
if (beanFactory.containsBeanDefinition(beanName)) {
BeanDefinition bd = beanFactory.getBeanDefinition(beanName);
if (bd instanceof RootBeanDefinition rbd) {
// Check raw bean class, e.g. in case of a proxy.
if (rbd.hasBeanClass() && rbd.getFactoryMethodName() == null) {
Class<?> beanClass = rbd.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 = rbd.getResolvedFactoryMethod();
if (factoryMethod != null) {
MergedAnnotations.from(factoryMethod, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
.stream(annotationType)
.filter(MergedAnnotation::isPresent)
.forEach(mergedAnnotation -> annotations.add(mergedAnnotation.synthesize()));
}
}
}
return annotations;
}
private Set<Class<? extends RuntimeHintsRegistrar>> extractFromBeanDefinition(String beanName,
ImportRuntimeHints annotation) {
Set<Class<? extends RuntimeHintsRegistrar>> registrars = new LinkedHashSet<>();
for (Class<? extends RuntimeHintsRegistrar> registrarClass : annotation.value()) {
if (logger.isTraceEnabled()) {
logger.trace(
LogMessage.format("Loaded [%s] registrar from annotated bean [%s]",
registrarClass.getCanonicalName(), beanName));
logger.trace(LogMessage.format("Loaded [%s] registrar from annotated bean [%s]",
registrarClass.getCanonicalName(), beanName));
}
registrars.add(registrarClass);
}
@@ -135,26 +88,24 @@ class RuntimeHintsBeanFactoryInitializationAotProcessor implements BeanFactoryIn
}
static class RuntimeHintsRegistrarContribution
implements BeanFactoryInitializationAotContribution {
static class RuntimeHintsRegistrarContribution implements BeanFactoryInitializationAotContribution {
private final Iterable<RuntimeHintsRegistrar> registrars;
@Nullable
private final ClassLoader beanClassLoader;
RuntimeHintsRegistrarContribution(Iterable<RuntimeHintsRegistrar> registrars,
@Nullable ClassLoader beanClassLoader) {
this.registrars = registrars;
this.beanClassLoader = beanClassLoader;
}
@Override
public void applyTo(GenerationContext generationContext,
BeanFactoryInitializationCode beanFactoryInitializationCode) {
RuntimeHints hints = generationContext.getRuntimeHints();
this.registrars.forEach(registrar -> {
if (logger.isTraceEnabled()) {
@@ -165,7 +116,6 @@ class RuntimeHintsBeanFactoryInitializationAotProcessor implements BeanFactoryIn
registrar.registerHints(hints, this.beanClassLoader);
});
}
}
}

View File

@@ -1318,6 +1318,15 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
return getBeanFactory().findAnnotationOnBean(beanName, annotationType, allowFactoryBeanInit);
}
@Override
public <A extends Annotation> Set<A> findAllAnnotationsOnBean(
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit)
throws NoSuchBeanDefinitionException {
assertBeanFactoryActive();
return getBeanFactory().findAllAnnotationsOnBean(beanName, annotationType, allowFactoryBeanInit);
}
//---------------------------------------------------------------------
// Implementation of HierarchicalBeanFactory interface

View File

@@ -829,8 +829,7 @@ public abstract class AnnotatedElementUtils {
}
private static <A extends Annotation> Comparator<MergedAnnotation<A>> highAggregateIndexesFirst() {
return Comparator.<MergedAnnotation<A>> comparingInt(
MergedAnnotation::getAggregateIndex).reversed();
return Comparator.<MergedAnnotation<A>> comparingInt(MergedAnnotation::getAggregateIndex).reversed();
}
@Nullable
@@ -840,8 +839,7 @@ public abstract class AnnotatedElementUtils {
if (!annotation.isPresent()) {
return null;
}
return annotation.asAnnotationAttributes(
Adapt.values(classValuesAsString, nestedAnnotationsAsMap));
return annotation.asAnnotationAttributes(Adapt.values(classValuesAsString, nestedAnnotationsAsMap));
}

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.
@@ -323,6 +323,14 @@ class StubWebApplicationContext implements WebApplicationContext {
return this.beanFactory.findAnnotationOnBean(beanName, annotationType, allowFactoryBeanInit);
}
@Override
public <A extends Annotation> Set<A> findAllAnnotationsOnBean(
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit)
throws NoSuchBeanDefinitionException {
return this.beanFactory.findAllAnnotationsOnBean(beanName, annotationType, allowFactoryBeanInit);
}
//---------------------------------------------------------------------
// Implementation of HierarchicalBeanFactory interface