Merge ad0ddb71d4
into 838b4d67a5
This commit is contained in:
commit
c9dce6bd17
|
@ -29,6 +29,7 @@ import java.util.Set;
|
|||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
@ -580,6 +581,27 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
|
|||
// Generics potentially only match on the target class, not on the proxy...
|
||||
RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
|
||||
Class<?> targetType = mbd.getTargetType();
|
||||
|
||||
String scope = mbd.getScope();
|
||||
if (targetType == null && scope != null && !scope.isEmpty()) {
|
||||
String targetBeanName = "scopedTarget." + beanName;
|
||||
if (containsBeanDefinition(targetBeanName)) {
|
||||
RootBeanDefinition targetMbd = getMergedLocalBeanDefinition(targetBeanName);
|
||||
|
||||
ResolvableType targetResolvableType = targetMbd.targetType;
|
||||
if (targetResolvableType == null) {
|
||||
targetResolvableType = targetMbd.factoryMethodReturnType;
|
||||
if (targetResolvableType == null) {
|
||||
targetResolvableType = ResolvableType.forClass(targetMbd.getBeanClass());
|
||||
}
|
||||
}
|
||||
|
||||
if (typeToMatch.isAssignableFrom(targetResolvableType)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (targetType != null && targetType != ClassUtils.getUserClass(beanInstance)) {
|
||||
// Check raw class match as well, making sure it's exposed on the proxy.
|
||||
Class<?> classToMatch = typeToMatch.resolve();
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
package org.springframework.beans.factory.support;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.core.ResolvableType;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link AbstractBeanFactory#isTypeMatch} with scoped proxy beans that use generic types.
|
||||
*/
|
||||
class ScopedProxyGenericTypeMatchTests {
|
||||
|
||||
@Test
|
||||
void scopedProxyBeanTypeMatching() {
|
||||
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
|
||||
|
||||
String proxyBeanName = "wordBean-" + UUID.randomUUID();
|
||||
String targetBeanName = "scopedTarget." + proxyBeanName;
|
||||
|
||||
RootBeanDefinition targetDef = new RootBeanDefinition(SomeGenericSupplier.class);
|
||||
targetDef.setScope("request");
|
||||
factory.registerBeanDefinition(targetBeanName, targetDef);
|
||||
|
||||
RootBeanDefinition proxyDef = new RootBeanDefinition();
|
||||
proxyDef.setScope("singleton");
|
||||
proxyDef.setTargetType(ResolvableType.forClassWithGenerics(Supplier.class, String.class));
|
||||
proxyDef.setAttribute("targetBeanName", targetBeanName);
|
||||
factory.registerBeanDefinition(proxyBeanName, proxyDef);
|
||||
|
||||
ResolvableType supplierType = ResolvableType.forClassWithGenerics(Supplier.class, String.class);
|
||||
|
||||
assertThat(factory.isTypeMatch(proxyBeanName, supplierType)).isTrue();
|
||||
|
||||
assertThat(factory.getBeanNamesForType(supplierType)).contains(proxyBeanName);
|
||||
}
|
||||
|
||||
static class SomeGenericSupplier implements Supplier<String> {
|
||||
@Override
|
||||
public String get() {
|
||||
return "value";
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue