@Lazy falls back to empty map/list/set for non-required collection dependency

Issue: SPR-15858
This commit is contained in:
Juergen Hoeller
2017-08-17 12:28:55 +02:00
parent 61cdc842e0
commit ec1eafc46f
2 changed files with 53 additions and 3 deletions

View File

@@ -18,6 +18,11 @@ package org.springframework.context.annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.ProxyFactory;
@@ -83,6 +88,16 @@ public class ContextAnnotationAutowireCandidateResolver extends QualifierAnnotat
public Object getTarget() {
Object target = beanFactory.doResolveDependency(descriptor, beanName, null, null);
if (target == null) {
Class<?> type = getTargetClass();
if (Map.class == type) {
return Collections.EMPTY_MAP;
}
else if (List.class == type) {
return Collections.EMPTY_LIST;
}
else if (Set.class == type || Collection.class == type) {
return Collections.EMPTY_SET;
}
throw new NoSuchBeanDefinitionException(descriptor.getResolvableType(),
"Optional dependency not present for lazy injection point");
}