Provide findAnnotationOnBean variant with allowFactoryBeanInit flag
Closes gh-27796
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2020 the original author or authors.
|
* Copyright 2002-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -353,9 +353,31 @@ public interface ListableBeanFactory extends BeanFactory {
|
|||||||
* @since 3.0
|
* @since 3.0
|
||||||
* @see #getBeanNamesForAnnotation
|
* @see #getBeanNamesForAnnotation
|
||||||
* @see #getBeansWithAnnotation
|
* @see #getBeansWithAnnotation
|
||||||
|
* @see #getType(String)
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
<A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
|
<A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
|
||||||
throws NoSuchBeanDefinitionException;
|
throws NoSuchBeanDefinitionException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find an {@link Annotation} of {@code annotationType} on the specified bean,
|
||||||
|
* traversing its interfaces and super classes 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 annotation of the given type if found, or {@code null} otherwise
|
||||||
|
* @throws NoSuchBeanDefinitionException if there is no bean with the given name
|
||||||
|
* @since 5.3.14
|
||||||
|
* @see #getBeanNamesForAnnotation
|
||||||
|
* @see #getBeansWithAnnotation
|
||||||
|
* @see #getType(String, boolean)
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
<A extends Annotation> A findAnnotationOnBean(
|
||||||
|
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit)
|
||||||
|
throws NoSuchBeanDefinitionException;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2020 the original author or authors.
|
* Copyright 2002-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -730,14 +730,23 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
|||||||
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
|
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
|
||||||
throws NoSuchBeanDefinitionException {
|
throws NoSuchBeanDefinitionException {
|
||||||
|
|
||||||
return findMergedAnnotationOnBean(beanName, annotationType)
|
return findAnnotationOnBean(beanName, annotationType, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Nullable
|
||||||
|
public <A extends Annotation> A findAnnotationOnBean(
|
||||||
|
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit)
|
||||||
|
throws NoSuchBeanDefinitionException {
|
||||||
|
|
||||||
|
return findMergedAnnotationOnBean(beanName, annotationType, allowFactoryBeanInit)
|
||||||
.synthesize(MergedAnnotation::isPresent).orElse(null);
|
.synthesize(MergedAnnotation::isPresent).orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private <A extends Annotation> MergedAnnotation<A> findMergedAnnotationOnBean(
|
private <A extends Annotation> MergedAnnotation<A> findMergedAnnotationOnBean(
|
||||||
String beanName, Class<A> annotationType) {
|
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit) {
|
||||||
|
|
||||||
Class<?> beanType = getType(beanName);
|
Class<?> beanType = getType(beanName, allowFactoryBeanInit);
|
||||||
if (beanType != null) {
|
if (beanType != null) {
|
||||||
MergedAnnotation<A> annotation =
|
MergedAnnotation<A> annotation =
|
||||||
MergedAnnotations.from(beanType, SearchStrategy.TYPE_HIERARCHY).get(annotationType);
|
MergedAnnotations.from(beanType, SearchStrategy.TYPE_HIERARCHY).get(annotationType);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2020 the original author or authors.
|
* Copyright 2002-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -459,7 +459,16 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
|
|||||||
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
|
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
|
||||||
throws NoSuchBeanDefinitionException {
|
throws NoSuchBeanDefinitionException {
|
||||||
|
|
||||||
Class<?> beanType = getType(beanName);
|
return findAnnotationOnBean(beanName, annotationType, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Nullable
|
||||||
|
public <A extends Annotation> A findAnnotationOnBean(
|
||||||
|
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit)
|
||||||
|
throws NoSuchBeanDefinitionException {
|
||||||
|
|
||||||
|
Class<?> beanType = getType(beanName, allowFactoryBeanInit);
|
||||||
return (beanType != null ? AnnotatedElementUtils.findMergedAnnotation(beanType, annotationType) : null);
|
return (beanType != null ? AnnotatedElementUtils.findMergedAnnotation(beanType, annotationType) : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1331,6 +1331,16 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
|
|||||||
return getBeanFactory().findAnnotationOnBean(beanName, annotationType);
|
return getBeanFactory().findAnnotationOnBean(beanName, annotationType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Nullable
|
||||||
|
public <A extends Annotation> A findAnnotationOnBean(
|
||||||
|
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit)
|
||||||
|
throws NoSuchBeanDefinitionException {
|
||||||
|
|
||||||
|
assertBeanFactoryActive();
|
||||||
|
return getBeanFactory().findAnnotationOnBean(beanName, annotationType, allowFactoryBeanInit);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
// Implementation of HierarchicalBeanFactory interface
|
// Implementation of HierarchicalBeanFactory interface
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2020 the original author or authors.
|
* Copyright 2002-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -314,6 +314,15 @@ class StubWebApplicationContext implements WebApplicationContext {
|
|||||||
return this.beanFactory.findAnnotationOnBean(beanName, annotationType);
|
return this.beanFactory.findAnnotationOnBean(beanName, annotationType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Nullable
|
||||||
|
public <A extends Annotation> A findAnnotationOnBean(
|
||||||
|
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit)
|
||||||
|
throws NoSuchBeanDefinitionException {
|
||||||
|
|
||||||
|
return this.beanFactory.findAnnotationOnBean(beanName, annotationType, allowFactoryBeanInit);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
// Implementation of HierarchicalBeanFactory interface
|
// Implementation of HierarchicalBeanFactory interface
|
||||||
|
|||||||
Reference in New Issue
Block a user