Fix: type matching for request-scope generic beans
Signed-off-by: currenjin <hyun0524e@naver.com>
This commit is contained in:
parent
ac0136b75c
commit
03cd38f1b1
|
@ -29,6 +29,7 @@ import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import java.util.function.UnaryOperator;
|
import java.util.function.UnaryOperator;
|
||||||
|
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
|
@ -583,6 +584,27 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
|
||||||
// Generics potentially only match on the target class, not on the proxy...
|
// Generics potentially only match on the target class, not on the proxy...
|
||||||
RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
|
RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
|
||||||
Class<?> targetType = mbd.getTargetType();
|
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)) {
|
if (targetType != null && targetType != ClassUtils.getUserClass(beanInstance)) {
|
||||||
// Check raw class match as well, making sure it's exposed on the proxy.
|
// Check raw class match as well, making sure it's exposed on the proxy.
|
||||||
Class<?> classToMatch = typeToMatch.resolve();
|
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.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();
|
||||||
|
|
||||||
|
RootBeanDefinition targetDef = new RootBeanDefinition(SomeGenericSupplier.class);
|
||||||
|
targetDef.setScope("request");
|
||||||
|
factory.registerBeanDefinition("scopedTarget.wordBean", targetDef);
|
||||||
|
|
||||||
|
RootBeanDefinition proxyDef = new RootBeanDefinition();
|
||||||
|
proxyDef.setScope("singleton");
|
||||||
|
proxyDef.setTargetType(ResolvableType.forClassWithGenerics(Supplier.class, String.class));
|
||||||
|
proxyDef.setAttribute("targetBeanName", "scopedTarget.wordBean");
|
||||||
|
factory.registerBeanDefinition("wordBean", proxyDef);
|
||||||
|
|
||||||
|
ResolvableType supplierType = ResolvableType.forClassWithGenerics(Supplier.class, String.class);
|
||||||
|
|
||||||
|
boolean isMatch = factory.isTypeMatch("wordBean", supplierType);
|
||||||
|
|
||||||
|
|
||||||
|
assertThat(isMatch).isTrue();
|
||||||
|
|
||||||
|
String[] names = factory.getBeanNamesForType(supplierType);
|
||||||
|
assertThat(names).contains("wordBean");
|
||||||
|
}
|
||||||
|
|
||||||
|
static class SomeGenericSupplier implements Supplier<String> {
|
||||||
|
@Override
|
||||||
|
public String get() {
|
||||||
|
return "value";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue