|
|
|
@ -236,8 +236,8 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean |
|
|
|
Constructor<?> requiredConstructor = null; |
|
|
|
Constructor<?> requiredConstructor = null; |
|
|
|
Constructor<?> defaultConstructor = null; |
|
|
|
Constructor<?> defaultConstructor = null; |
|
|
|
for (Constructor<?> candidate : rawCandidates) { |
|
|
|
for (Constructor<?> candidate : rawCandidates) { |
|
|
|
AnnotationAttributes annotation = findAutowiredAnnotation(candidate); |
|
|
|
AnnotationAttributes ann = findAutowiredAnnotation(candidate); |
|
|
|
if (annotation != null) { |
|
|
|
if (ann != null) { |
|
|
|
if (requiredConstructor != null) { |
|
|
|
if (requiredConstructor != null) { |
|
|
|
throw new BeanCreationException(beanName, |
|
|
|
throw new BeanCreationException(beanName, |
|
|
|
"Invalid autowire-marked constructor: " + candidate + |
|
|
|
"Invalid autowire-marked constructor: " + candidate + |
|
|
|
@ -248,7 +248,7 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean |
|
|
|
throw new IllegalStateException( |
|
|
|
throw new IllegalStateException( |
|
|
|
"Autowired annotation requires at least one argument: " + candidate); |
|
|
|
"Autowired annotation requires at least one argument: " + candidate); |
|
|
|
} |
|
|
|
} |
|
|
|
boolean required = determineRequiredStatus(annotation); |
|
|
|
boolean required = determineRequiredStatus(ann); |
|
|
|
if (required) { |
|
|
|
if (required) { |
|
|
|
if (!candidates.isEmpty()) { |
|
|
|
if (!candidates.isEmpty()) { |
|
|
|
throw new BeanCreationException(beanName, |
|
|
|
throw new BeanCreationException(beanName, |
|
|
|
@ -322,9 +322,9 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz) { |
|
|
|
private InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz) { |
|
|
|
// Quick check on the concurrent map first, with minimal locking.
|
|
|
|
|
|
|
|
// Fall back to class name as cache key, for backwards compatibility with custom callers.
|
|
|
|
// Fall back to class name as cache key, for backwards compatibility with custom callers.
|
|
|
|
String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName()); |
|
|
|
String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName()); |
|
|
|
|
|
|
|
// Quick check on the concurrent map first, with minimal locking.
|
|
|
|
InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey); |
|
|
|
InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey); |
|
|
|
if (InjectionMetadata.needsRefresh(metadata, clazz)) { |
|
|
|
if (InjectionMetadata.needsRefresh(metadata, clazz)) { |
|
|
|
synchronized (this.injectionMetadataCache) { |
|
|
|
synchronized (this.injectionMetadataCache) { |
|
|
|
@ -345,15 +345,15 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean |
|
|
|
do { |
|
|
|
do { |
|
|
|
LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<InjectionMetadata.InjectedElement>(); |
|
|
|
LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<InjectionMetadata.InjectedElement>(); |
|
|
|
for (Field field : targetClass.getDeclaredFields()) { |
|
|
|
for (Field field : targetClass.getDeclaredFields()) { |
|
|
|
AnnotationAttributes annotation = findAutowiredAnnotation(field); |
|
|
|
AnnotationAttributes ann = findAutowiredAnnotation(field); |
|
|
|
if (annotation != null) { |
|
|
|
if (ann != null) { |
|
|
|
if (Modifier.isStatic(field.getModifiers())) { |
|
|
|
if (Modifier.isStatic(field.getModifiers())) { |
|
|
|
if (logger.isWarnEnabled()) { |
|
|
|
if (logger.isWarnEnabled()) { |
|
|
|
logger.warn("Autowired annotation is not supported on static fields: " + field); |
|
|
|
logger.warn("Autowired annotation is not supported on static fields: " + field); |
|
|
|
} |
|
|
|
} |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
boolean required = determineRequiredStatus(annotation); |
|
|
|
boolean required = determineRequiredStatus(ann); |
|
|
|
currElements.add(new AutowiredFieldElement(field, required)); |
|
|
|
currElements.add(new AutowiredFieldElement(field, required)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -390,9 +390,9 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean |
|
|
|
|
|
|
|
|
|
|
|
private AnnotationAttributes findAutowiredAnnotation(AccessibleObject ao) { |
|
|
|
private AnnotationAttributes findAutowiredAnnotation(AccessibleObject ao) { |
|
|
|
for (Class<? extends Annotation> type : this.autowiredAnnotationTypes) { |
|
|
|
for (Class<? extends Annotation> type : this.autowiredAnnotationTypes) { |
|
|
|
AnnotationAttributes annotation = AnnotatedElementUtils.getAnnotationAttributes(ao, type.getName()); |
|
|
|
AnnotationAttributes ann = AnnotatedElementUtils.getAnnotationAttributes(ao, type.getName()); |
|
|
|
if (annotation != null) { |
|
|
|
if (ann != null) { |
|
|
|
return annotation; |
|
|
|
return ann; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
@ -403,12 +403,12 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean |
|
|
|
* <p>A 'required' dependency means that autowiring should fail when no beans |
|
|
|
* <p>A 'required' dependency means that autowiring should fail when no beans |
|
|
|
* are found. Otherwise, the autowiring process will simply bypass the field |
|
|
|
* are found. Otherwise, the autowiring process will simply bypass the field |
|
|
|
* or method when no beans are found. |
|
|
|
* or method when no beans are found. |
|
|
|
* @param annotation the Autowired annotation |
|
|
|
* @param ann the Autowired annotation |
|
|
|
* @return whether the annotation indicates that a dependency is required |
|
|
|
* @return whether the annotation indicates that a dependency is required |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected boolean determineRequiredStatus(AnnotationAttributes annotation) { |
|
|
|
protected boolean determineRequiredStatus(AnnotationAttributes ann) { |
|
|
|
return (!annotation.containsKey(this.requiredParameterName) || |
|
|
|
return (!ann.containsKey(this.requiredParameterName) || |
|
|
|
this.requiredParameterValue == annotation.getBoolean(this.requiredParameterName)); |
|
|
|
this.requiredParameterValue == ann.getBoolean(this.requiredParameterName)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
|