Skip to content

Commit b047a50

Browse files
committed
Migrate AutowiredAnnotationBeanPostProcessor to MergedAnnotations
Closes gh-22582
1 parent 20bb7ac commit b047a50

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@
5757
import org.springframework.core.MethodParameter;
5858
import org.springframework.core.Ordered;
5959
import org.springframework.core.PriorityOrdered;
60-
import org.springframework.core.annotation.AnnotatedElementUtils;
6160
import org.springframework.core.annotation.AnnotationAttributes;
6261
import org.springframework.core.annotation.AnnotationUtils;
62+
import org.springframework.core.annotation.MergedAnnotation;
63+
import org.springframework.core.annotation.MergedAnnotations;
64+
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
6365
import org.springframework.lang.Nullable;
6466
import org.springframework.util.Assert;
6567
import org.springframework.util.ClassUtils;
@@ -304,7 +306,7 @@ public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, final
304306
else if (primaryConstructor != null) {
305307
continue;
306308
}
307-
AnnotationAttributes ann = findAutowiredAnnotation(candidate);
309+
MergedAnnotation<?> ann = findAutowiredAnnotation(candidate);
308310
if (ann == null) {
309311
Class<?> userClass = ClassUtils.getUserClass(beanClass);
310312
if (userClass != beanClass) {
@@ -453,7 +455,7 @@ private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) {
453455
final List<InjectionMetadata.InjectedElement> currElements = new ArrayList<>();
454456

455457
ReflectionUtils.doWithLocalFields(targetClass, field -> {
456-
AnnotationAttributes ann = findAutowiredAnnotation(field);
458+
MergedAnnotation<?> ann = findAutowiredAnnotation(field);
457459
if (ann != null) {
458460
if (Modifier.isStatic(field.getModifiers())) {
459461
if (logger.isInfoEnabled()) {
@@ -471,7 +473,7 @@ private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) {
471473
if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
472474
return;
473475
}
474-
AnnotationAttributes ann = findAutowiredAnnotation(bridgedMethod);
476+
MergedAnnotation<?> ann = findAutowiredAnnotation(bridgedMethod);
475477
if (ann != null && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
476478
if (Modifier.isStatic(method.getModifiers())) {
477479
if (logger.isInfoEnabled()) {
@@ -500,13 +502,12 @@ private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) {
500502
}
501503

502504
@Nullable
503-
private AnnotationAttributes findAutowiredAnnotation(AccessibleObject ao) {
504-
if (ao.getAnnotations().length > 0) { // autowiring annotations have to be local
505-
for (Class<? extends Annotation> type : this.autowiredAnnotationTypes) {
506-
AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(ao, type);
507-
if (attributes != null) {
508-
return attributes;
509-
}
505+
private MergedAnnotation<?> findAutowiredAnnotation(AccessibleObject ao) {
506+
MergedAnnotations annotations = MergedAnnotations.from(ao, SearchStrategy.INHERITED_ANNOTATIONS);
507+
for (Class<? extends Annotation> type : this.autowiredAnnotationTypes) {
508+
MergedAnnotation<?> annotation = annotations.get(type);
509+
if (annotation.isPresent()) {
510+
return annotation;
510511
}
511512
}
512513
return null;
@@ -520,6 +521,21 @@ private AnnotationAttributes findAutowiredAnnotation(AccessibleObject ao) {
520521
* @param ann the Autowired annotation
521522
* @return whether the annotation indicates that a dependency is required
522523
*/
524+
protected boolean determineRequiredStatus(MergedAnnotation<?> ann) {
525+
return determineRequiredStatus(
526+
ann.asMap(mergedAnnotation -> new AnnotationAttributes()));
527+
}
528+
529+
/**
530+
* Determine if the annotated field or method requires its dependency.
531+
* <p>A 'required' dependency means that autowiring should fail when no beans
532+
* are found. Otherwise, the autowiring process will simply bypass the field
533+
* or method when no beans are found.
534+
* @param ann the Autowired annotation
535+
* @return whether the annotation indicates that a dependency is required
536+
* @deprecated since 5.2 in favor of {@link #determineRequiredStatus(MergedAnnotation)}
537+
*/
538+
@Deprecated
523539
protected boolean determineRequiredStatus(AnnotationAttributes ann) {
524540
return (!ann.containsKey(this.requiredParameterName) ||
525541
this.requiredParameterValue == ann.getBoolean(this.requiredParameterName));

0 commit comments

Comments
 (0)