Cache singleton results for @Lazy injection points
Includes consistent use of unmodifiable collections. Closes gh-33841
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.context.annotation;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collection;
|
||||
@@ -27,7 +28,6 @@ import java.util.Set;
|
||||
|
||||
import org.springframework.aop.TargetSource;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver;
|
||||
import org.springframework.beans.factory.config.DependencyDescriptor;
|
||||
@@ -35,7 +35,6 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Complete implementation of the
|
||||
@@ -85,47 +84,13 @@ public class ContextAnnotationAutowireCandidateResolver extends QualifierAnnotat
|
||||
}
|
||||
|
||||
private Object buildLazyResolutionProxy(
|
||||
final DependencyDescriptor descriptor, @Nullable final String beanName, boolean classOnly) {
|
||||
DependencyDescriptor descriptor, @Nullable String beanName, boolean classOnly) {
|
||||
|
||||
BeanFactory beanFactory = getBeanFactory();
|
||||
Assert.state(beanFactory instanceof DefaultListableBeanFactory,
|
||||
"BeanFactory needs to be a DefaultListableBeanFactory");
|
||||
final DefaultListableBeanFactory dlbf = (DefaultListableBeanFactory) beanFactory;
|
||||
if (!(getBeanFactory() instanceof DefaultListableBeanFactory dlbf)) {
|
||||
throw new IllegalStateException("Lazy resolution only supported with DefaultListableBeanFactory");
|
||||
}
|
||||
|
||||
TargetSource ts = new TargetSource() {
|
||||
@Override
|
||||
public Class<?> getTargetClass() {
|
||||
return descriptor.getDependencyType();
|
||||
}
|
||||
@Override
|
||||
@SuppressWarnings("NullAway")
|
||||
public Object getTarget() {
|
||||
Set<String> autowiredBeanNames = (beanName != null ? new LinkedHashSet<>(1) : null);
|
||||
Object target = dlbf.doResolveDependency(descriptor, beanName, autowiredBeanNames, null);
|
||||
if (target == null) {
|
||||
Class<?> type = getTargetClass();
|
||||
if (Map.class == type) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
else if (List.class == type) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
else if (Set.class == type || Collection.class == type) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
throw new NoSuchBeanDefinitionException(descriptor.getResolvableType(),
|
||||
"Optional dependency not present for lazy injection point");
|
||||
}
|
||||
if (autowiredBeanNames != null) {
|
||||
for (String autowiredBeanName : autowiredBeanNames) {
|
||||
if (dlbf.containsBean(autowiredBeanName)) {
|
||||
dlbf.registerDependentBean(autowiredBeanName, beanName);
|
||||
}
|
||||
}
|
||||
}
|
||||
return target;
|
||||
}
|
||||
};
|
||||
TargetSource ts = new LazyDependencyTargetSource(dlbf, descriptor, beanName);
|
||||
|
||||
ProxyFactory pf = new ProxyFactory();
|
||||
pf.setTargetSource(ts);
|
||||
@@ -137,4 +102,96 @@ public class ContextAnnotationAutowireCandidateResolver extends QualifierAnnotat
|
||||
return (classOnly ? pf.getProxyClass(classLoader) : pf.getProxy(classLoader));
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static class LazyDependencyTargetSource implements TargetSource, Serializable {
|
||||
|
||||
private final DefaultListableBeanFactory beanFactory;
|
||||
|
||||
private final DependencyDescriptor descriptor;
|
||||
|
||||
@Nullable
|
||||
private final String beanName;
|
||||
|
||||
@Nullable
|
||||
private transient volatile Object cachedTarget;
|
||||
|
||||
public LazyDependencyTargetSource(DefaultListableBeanFactory beanFactory,
|
||||
DependencyDescriptor descriptor, @Nullable String beanName) {
|
||||
|
||||
this.beanFactory = beanFactory;
|
||||
this.descriptor = descriptor;
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getTargetClass() {
|
||||
return this.descriptor.getDependencyType();
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("NullAway")
|
||||
public Object getTarget() {
|
||||
Object cachedTarget = this.cachedTarget;
|
||||
if (cachedTarget != null) {
|
||||
return cachedTarget;
|
||||
}
|
||||
|
||||
Set<String> autowiredBeanNames = new LinkedHashSet<>(2);
|
||||
Object target = this.beanFactory.doResolveDependency(
|
||||
this.descriptor, this.beanName, autowiredBeanNames, null);
|
||||
|
||||
if (target == null) {
|
||||
Class<?> type = getTargetClass();
|
||||
if (Map.class == type) {
|
||||
target = Collections.emptyMap();
|
||||
}
|
||||
else if (List.class == type) {
|
||||
target = Collections.emptyList();
|
||||
}
|
||||
else if (Set.class == type || Collection.class == type) {
|
||||
target = Collections.emptySet();
|
||||
}
|
||||
else {
|
||||
throw new NoSuchBeanDefinitionException(this.descriptor.getResolvableType(),
|
||||
"Optional dependency not present for lazy injection point");
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (target instanceof Map<?, ?> map && Map.class == getTargetClass()) {
|
||||
target = Collections.unmodifiableMap(map);
|
||||
}
|
||||
else if (target instanceof List<?> list && List.class == getTargetClass()) {
|
||||
target = Collections.unmodifiableList(list);
|
||||
}
|
||||
else if (target instanceof Set<?> set && Set.class == getTargetClass()) {
|
||||
target = Collections.unmodifiableSet(set);
|
||||
}
|
||||
else if (target instanceof Collection<?> coll && Collection.class == getTargetClass()) {
|
||||
target = Collections.unmodifiableCollection(coll);
|
||||
}
|
||||
}
|
||||
|
||||
boolean cacheable = true;
|
||||
for (String autowiredBeanName : autowiredBeanNames) {
|
||||
if (!this.beanFactory.containsBean(autowiredBeanName)) {
|
||||
cacheable = false;
|
||||
}
|
||||
else {
|
||||
if (this.beanName != null) {
|
||||
this.beanFactory.registerDependentBean(autowiredBeanName, this.beanName);
|
||||
}
|
||||
if (!this.beanFactory.isSingleton(autowiredBeanName)) {
|
||||
cacheable = false;
|
||||
}
|
||||
}
|
||||
if (cacheable) {
|
||||
this.cachedTarget = target;
|
||||
}
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -45,7 +45,8 @@ import java.lang.annotation.Target;
|
||||
* <p>In addition to its role for component initialization, this annotation may also be placed
|
||||
* on injection points marked with {@link org.springframework.beans.factory.annotation.Autowired}
|
||||
* or {@link jakarta.inject.Inject}: In that context, it leads to the creation of a
|
||||
* lazy-resolution proxy for all affected dependencies, as an alternative to using
|
||||
* lazy-resolution proxy for the affected dependency, caching it on first access in case of
|
||||
* a singleton or re-resolving it on every access otherwise. This is an alternative to using
|
||||
* {@link org.springframework.beans.factory.ObjectFactory} or {@link jakarta.inject.Provider}.
|
||||
* Please note that such a lazy-resolution proxy will always be injected; if the target
|
||||
* dependency does not exist, you will only be able to find out through an exception on
|
||||
|
||||
Reference in New Issue
Block a user