Skip to content

Commit ca84559

Browse files
committed
Provide findAnnotationOnBean variant with allowFactoryBeanInit flag
Closes gh-27796
1 parent 1af21bb commit ca84559

File tree

5 files changed

+67
-8
lines changed

5 files changed

+67
-8
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/ListableBeanFactory.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -353,9 +353,31 @@ <T> Map<String, T> getBeansOfType(@Nullable Class<T> type, boolean includeNonSin
353353
* @since 3.0
354354
* @see #getBeanNamesForAnnotation
355355
* @see #getBeansWithAnnotation
356+
* @see #getType(String)
356357
*/
357358
@Nullable
358359
<A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
359360
throws NoSuchBeanDefinitionException;
360361

362+
/**
363+
* Find an {@link Annotation} of {@code annotationType} on the specified bean,
364+
* traversing its interfaces and super classes if no annotation can be found on
365+
* the given class itself, as well as checking the bean's factory method (if any).
366+
* @param beanName the name of the bean to look for annotations on
367+
* @param annotationType the type of annotation to look for
368+
* (at class, interface or factory method level of the specified bean)
369+
* @param allowFactoryBeanInit whether a {@code FactoryBean} may get initialized
370+
* just for the purpose of determining its object type
371+
* @return the annotation of the given type if found, or {@code null} otherwise
372+
* @throws NoSuchBeanDefinitionException if there is no bean with the given name
373+
* @since 5.3.14
374+
* @see #getBeanNamesForAnnotation
375+
* @see #getBeansWithAnnotation
376+
* @see #getType(String, boolean)
377+
*/
378+
@Nullable
379+
<A extends Annotation> A findAnnotationOnBean(
380+
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit)
381+
throws NoSuchBeanDefinitionException;
382+
361383
}

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -730,14 +730,23 @@ public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> an
730730
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
731731
throws NoSuchBeanDefinitionException {
732732

733-
return findMergedAnnotationOnBean(beanName, annotationType)
733+
return findAnnotationOnBean(beanName, annotationType, true);
734+
}
735+
736+
@Override
737+
@Nullable
738+
public <A extends Annotation> A findAnnotationOnBean(
739+
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit)
740+
throws NoSuchBeanDefinitionException {
741+
742+
return findMergedAnnotationOnBean(beanName, annotationType, allowFactoryBeanInit)
734743
.synthesize(MergedAnnotation::isPresent).orElse(null);
735744
}
736745

737746
private <A extends Annotation> MergedAnnotation<A> findMergedAnnotationOnBean(
738-
String beanName, Class<A> annotationType) {
747+
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit) {
739748

740-
Class<?> beanType = getType(beanName);
749+
Class<?> beanType = getType(beanName, allowFactoryBeanInit);
741750
if (beanType != null) {
742751
MergedAnnotation<A> annotation =
743752
MergedAnnotations.from(beanType, SearchStrategy.TYPE_HIERARCHY).get(annotationType);

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -459,7 +459,16 @@ public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> an
459459
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
460460
throws NoSuchBeanDefinitionException {
461461

462-
Class<?> beanType = getType(beanName);
462+
return findAnnotationOnBean(beanName, annotationType, true);
463+
}
464+
465+
@Override
466+
@Nullable
467+
public <A extends Annotation> A findAnnotationOnBean(
468+
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit)
469+
throws NoSuchBeanDefinitionException {
470+
471+
Class<?> beanType = getType(beanName, allowFactoryBeanInit);
463472
return (beanType != null ? AnnotatedElementUtils.findMergedAnnotation(beanType, annotationType) : null);
464473
}
465474

spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,16 @@ public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> a
13311331
return getBeanFactory().findAnnotationOnBean(beanName, annotationType);
13321332
}
13331333

1334+
@Override
1335+
@Nullable
1336+
public <A extends Annotation> A findAnnotationOnBean(
1337+
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit)
1338+
throws NoSuchBeanDefinitionException {
1339+
1340+
assertBeanFactoryActive();
1341+
return getBeanFactory().findAnnotationOnBean(beanName, annotationType, allowFactoryBeanInit);
1342+
}
1343+
13341344

13351345
//---------------------------------------------------------------------
13361346
// Implementation of HierarchicalBeanFactory interface

spring-test/src/main/java/org/springframework/test/web/servlet/setup/StubWebApplicationContext.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -314,6 +314,15 @@ public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> a
314314
return this.beanFactory.findAnnotationOnBean(beanName, annotationType);
315315
}
316316

317+
@Override
318+
@Nullable
319+
public <A extends Annotation> A findAnnotationOnBean(
320+
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit)
321+
throws NoSuchBeanDefinitionException {
322+
323+
return this.beanFactory.findAnnotationOnBean(beanName, annotationType, allowFactoryBeanInit);
324+
}
325+
317326

318327
//---------------------------------------------------------------------
319328
// Implementation of HierarchicalBeanFactory interface

0 commit comments

Comments
 (0)