57
57
import org .springframework .core .MethodParameter ;
58
58
import org .springframework .core .Ordered ;
59
59
import org .springframework .core .PriorityOrdered ;
60
- import org .springframework .core .annotation .AnnotatedElementUtils ;
61
60
import org .springframework .core .annotation .AnnotationAttributes ;
62
61
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 ;
63
65
import org .springframework .lang .Nullable ;
64
66
import org .springframework .util .Assert ;
65
67
import org .springframework .util .ClassUtils ;
@@ -304,7 +306,7 @@ public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, final
304
306
else if (primaryConstructor != null ) {
305
307
continue ;
306
308
}
307
- AnnotationAttributes ann = findAutowiredAnnotation (candidate );
309
+ MergedAnnotation <?> ann = findAutowiredAnnotation (candidate );
308
310
if (ann == null ) {
309
311
Class <?> userClass = ClassUtils .getUserClass (beanClass );
310
312
if (userClass != beanClass ) {
@@ -453,7 +455,7 @@ private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) {
453
455
final List <InjectionMetadata .InjectedElement > currElements = new ArrayList <>();
454
456
455
457
ReflectionUtils .doWithLocalFields (targetClass , field -> {
456
- AnnotationAttributes ann = findAutowiredAnnotation (field );
458
+ MergedAnnotation <?> ann = findAutowiredAnnotation (field );
457
459
if (ann != null ) {
458
460
if (Modifier .isStatic (field .getModifiers ())) {
459
461
if (logger .isInfoEnabled ()) {
@@ -471,7 +473,7 @@ private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) {
471
473
if (!BridgeMethodResolver .isVisibilityBridgeMethodPair (method , bridgedMethod )) {
472
474
return ;
473
475
}
474
- AnnotationAttributes ann = findAutowiredAnnotation (bridgedMethod );
476
+ MergedAnnotation <?> ann = findAutowiredAnnotation (bridgedMethod );
475
477
if (ann != null && method .equals (ClassUtils .getMostSpecificMethod (method , clazz ))) {
476
478
if (Modifier .isStatic (method .getModifiers ())) {
477
479
if (logger .isInfoEnabled ()) {
@@ -500,13 +502,12 @@ private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) {
500
502
}
501
503
502
504
@ 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 ;
510
511
}
511
512
}
512
513
return null ;
@@ -520,6 +521,21 @@ private AnnotationAttributes findAutowiredAnnotation(AccessibleObject ao) {
520
521
* @param ann the Autowired annotation
521
522
* @return whether the annotation indicates that a dependency is required
522
523
*/
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
523
539
protected boolean determineRequiredStatus (AnnotationAttributes ann ) {
524
540
return (!ann .containsKey (this .requiredParameterName ) ||
525
541
this .requiredParameterValue == ann .getBoolean (this .requiredParameterName ));
0 commit comments