11/*
2- * Copyright 2002-2012 the original author or authors.
2+ * Copyright 2002-2013 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.
5555import org .springframework .core .MethodParameter ;
5656import org .springframework .core .Ordered ;
5757import 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 ;
5960import org .springframework .util .Assert ;
6061import org .springframework .util .ClassUtils ;
6162import org .springframework .util .ReflectionUtils ;
@@ -235,7 +236,7 @@ public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, Strin
235236 Constructor <?> requiredConstructor = null ;
236237 Constructor <?> defaultConstructor = null ;
237238 for (Constructor <?> candidate : rawCandidates ) {
238- Annotation annotation = findAutowiredAnnotation (candidate );
239+ AnnotationAttributes annotation = findAutowiredAnnotation (candidate );
239240 if (annotation != null ) {
240241 if (requiredConstructor != null ) {
241242 throw new BeanCreationException ("Invalid autowire-marked constructor: " + candidate +
@@ -333,7 +334,7 @@ private InjectionMetadata buildAutowiringMetadata(Class<?> clazz) {
333334 do {
334335 LinkedList <InjectionMetadata .InjectedElement > currElements = new LinkedList <InjectionMetadata .InjectedElement >();
335336 for (Field field : targetClass .getDeclaredFields ()) {
336- Annotation annotation = findAutowiredAnnotation (field );
337+ AnnotationAttributes annotation = findAutowiredAnnotation (field );
337338 if (annotation != null ) {
338339 if (Modifier .isStatic (field .getModifiers ())) {
339340 if (logger .isWarnEnabled ()) {
@@ -347,7 +348,7 @@ private InjectionMetadata buildAutowiringMetadata(Class<?> clazz) {
347348 }
348349 for (Method method : targetClass .getDeclaredMethods ()) {
349350 Method bridgedMethod = BridgeMethodResolver .findBridgedMethod (method );
350- Annotation annotation = BridgeMethodResolver .isVisibilityBridgeMethodPair (method , bridgedMethod ) ?
351+ AnnotationAttributes annotation = BridgeMethodResolver .isVisibilityBridgeMethodPair (method , bridgedMethod ) ?
351352 findAutowiredAnnotation (bridgedMethod ) : findAutowiredAnnotation (method );
352353 if (annotation != null && method .equals (ClassUtils .getMostSpecificMethod (method , clazz ))) {
353354 if (Modifier .isStatic (method .getModifiers ())) {
@@ -374,16 +375,29 @@ private InjectionMetadata buildAutowiringMetadata(Class<?> clazz) {
374375 return new InjectionMetadata (clazz , elements );
375376 }
376377
377- private Annotation findAutowiredAnnotation (AccessibleObject ao ) {
378+ private AnnotationAttributes findAutowiredAnnotation (AccessibleObject ao ) {
378379 for (Class <? extends Annotation > type : this .autowiredAnnotationTypes ) {
379- Annotation annotation = AnnotationUtils . getAnnotation (ao , type );
380+ AnnotationAttributes annotation = AnnotatedElementUtils . getAnnotationAttributes (ao , type . getName () );
380381 if (annotation != null ) {
381382 return annotation ;
382383 }
383384 }
384385 return null ;
385386 }
386387
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+
387401 /**
388402 * Obtain all beans of the given type as autowire candidates.
389403 * @param type the type of the bean
@@ -398,31 +412,6 @@ protected <T> Map<String, T> findAutowireCandidates(Class<T> type) throws BeansE
398412 return BeanFactoryUtils .beansOfTypeIncludingAncestors (this .beanFactory , type );
399413 }
400414
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-
426415 /**
427416 * Register the specified bean as dependent on the autowired beans.
428417 */
0 commit comments