1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2013 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
55
55
import org .springframework .core .MethodParameter ;
56
56
import org .springframework .core .Ordered ;
57
57
import org .springframework .core .PriorityOrdered ;
58
- import org .springframework .core .annotation .AnnotationUtils ;
58
+ import org .springframework .core .annotation .AnnotatedElementUtils ;
59
+ import org .springframework .core .annotation .AnnotationAttributes ;
59
60
import org .springframework .util .Assert ;
60
61
import org .springframework .util .ClassUtils ;
61
62
import org .springframework .util .ReflectionUtils ;
@@ -235,7 +236,7 @@ public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, Strin
235
236
Constructor <?> requiredConstructor = null ;
236
237
Constructor <?> defaultConstructor = null ;
237
238
for (Constructor <?> candidate : rawCandidates ) {
238
- Annotation annotation = findAutowiredAnnotation (candidate );
239
+ AnnotationAttributes annotation = findAutowiredAnnotation (candidate );
239
240
if (annotation != null ) {
240
241
if (requiredConstructor != null ) {
241
242
throw new BeanCreationException ("Invalid autowire-marked constructor: " + candidate +
@@ -333,7 +334,7 @@ private InjectionMetadata buildAutowiringMetadata(Class<?> clazz) {
333
334
do {
334
335
LinkedList <InjectionMetadata .InjectedElement > currElements = new LinkedList <InjectionMetadata .InjectedElement >();
335
336
for (Field field : targetClass .getDeclaredFields ()) {
336
- Annotation annotation = findAutowiredAnnotation (field );
337
+ AnnotationAttributes annotation = findAutowiredAnnotation (field );
337
338
if (annotation != null ) {
338
339
if (Modifier .isStatic (field .getModifiers ())) {
339
340
if (logger .isWarnEnabled ()) {
@@ -347,7 +348,7 @@ private InjectionMetadata buildAutowiringMetadata(Class<?> clazz) {
347
348
}
348
349
for (Method method : targetClass .getDeclaredMethods ()) {
349
350
Method bridgedMethod = BridgeMethodResolver .findBridgedMethod (method );
350
- Annotation annotation = BridgeMethodResolver .isVisibilityBridgeMethodPair (method , bridgedMethod ) ?
351
+ AnnotationAttributes annotation = BridgeMethodResolver .isVisibilityBridgeMethodPair (method , bridgedMethod ) ?
351
352
findAutowiredAnnotation (bridgedMethod ) : findAutowiredAnnotation (method );
352
353
if (annotation != null && method .equals (ClassUtils .getMostSpecificMethod (method , clazz ))) {
353
354
if (Modifier .isStatic (method .getModifiers ())) {
@@ -374,16 +375,29 @@ private InjectionMetadata buildAutowiringMetadata(Class<?> clazz) {
374
375
return new InjectionMetadata (clazz , elements );
375
376
}
376
377
377
- private Annotation findAutowiredAnnotation (AccessibleObject ao ) {
378
+ private AnnotationAttributes findAutowiredAnnotation (AccessibleObject ao ) {
378
379
for (Class <? extends Annotation > type : this .autowiredAnnotationTypes ) {
379
- Annotation annotation = AnnotationUtils . getAnnotation (ao , type );
380
+ AnnotationAttributes annotation = AnnotatedElementUtils . getAnnotationAttributes (ao , type . getName () );
380
381
if (annotation != null ) {
381
382
return annotation ;
382
383
}
383
384
}
384
385
return null ;
385
386
}
386
387
388
+ /**
389
+ * Determine if the annotated field or method requires its dependency.
390
+ * <p>A 'required' dependency means that autowiring should fail when no beans
391
+ * are found. Otherwise, the autowiring process will simply bypass the field
392
+ * or method when no beans are found.
393
+ * @param annotation the Autowired annotation
394
+ * @return whether the annotation indicates that a dependency is required
395
+ */
396
+ protected boolean determineRequiredStatus (AnnotationAttributes annotation ) {
397
+ return (!annotation .containsKey (this .requiredParameterName ) ||
398
+ this .requiredParameterValue == annotation .getBoolean (this .requiredParameterName ));
399
+ }
400
+
387
401
/**
388
402
* Obtain all beans of the given type as autowire candidates.
389
403
* @param type the type of the bean
@@ -398,31 +412,6 @@ protected <T> Map<String, T> findAutowireCandidates(Class<T> type) throws BeansE
398
412
return BeanFactoryUtils .beansOfTypeIncludingAncestors (this .beanFactory , type );
399
413
}
400
414
401
- /**
402
- * Determine if the annotated field or method requires its dependency.
403
- * <p>A 'required' dependency means that autowiring should fail when no beans
404
- * are found. Otherwise, the autowiring process will simply bypass the field
405
- * or method when no beans are found.
406
- * @param annotation the Autowired annotation
407
- * @return whether the annotation indicates that a dependency is required
408
- */
409
- protected boolean determineRequiredStatus (Annotation annotation ) {
410
- try {
411
- Method method = ReflectionUtils .findMethod (annotation .annotationType (), this .requiredParameterName );
412
- if (method == null ) {
413
- // annotations like @Inject and @Value don't have a method (attribute) named "required"
414
- // -> default to required status
415
- return true ;
416
- }
417
- return (this .requiredParameterValue == (Boolean ) ReflectionUtils .invokeMethod (method , annotation ));
418
- }
419
- catch (Exception ex ) {
420
- // an exception was thrown during reflective invocation of the required attribute
421
- // -> default to required status
422
- return true ;
423
- }
424
- }
425
-
426
415
/**
427
416
* Register the specified bean as dependent on the autowired beans.
428
417
*/
0 commit comments