This commit is contained in:
Hyunjin-Jeong 2025-04-23 23:38:29 +02:00 committed by GitHub
commit c9dce6bd17
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 68 additions and 0 deletions

View File

@ -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();

View File

@ -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";
}
}
}