mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-17 08:24:13 +00:00
Add dedicated unit test for getBean(String, ParameterizedTypeReference)
See gh-34687 See gh-37047
This commit is contained in:
+24
-3
@@ -19,6 +19,7 @@ package org.springframework.beans.factory;
|
||||
import java.io.Closeable;
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.net.MalformedURLException;
|
||||
import java.text.NumberFormat;
|
||||
import java.text.ParseException;
|
||||
@@ -1685,7 +1686,7 @@ class DefaultListableBeanFactoryTests {
|
||||
lbf.getBean(TestBean.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test // gh-34687
|
||||
void getBeanByNameWithTypeReference() {
|
||||
RootBeanDefinition bd1 = new RootBeanDefinition(StringTemplate.class);
|
||||
RootBeanDefinition bd2 = new RootBeanDefinition(NumberTemplate.class);
|
||||
@@ -1708,6 +1709,23 @@ class DefaultListableBeanFactoryTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test // gh-37047
|
||||
void getBeanByNameWithTypeReferenceMatchingGenericTypeOnAopProxy() {
|
||||
RootBeanDefinition bd = new RootBeanDefinition(ProxiedStringTemplate.class);
|
||||
bd.setTargetType(ResolvableType.forClass(ProxiedStringTemplate.class));
|
||||
lbf.registerBeanDefinition("proxiedTemplate", bd);
|
||||
|
||||
// Simulate a JDK dynamic AOP proxy (as created for proxyTargetClass=false)
|
||||
// which only exposes the raw ProxiedTemplate interface, erasing the
|
||||
// generic type information that is available on the target class.
|
||||
Object proxy = Proxy.newProxyInstance(getClass().getClassLoader(),
|
||||
new Class<?>[] {ProxiedTemplate.class}, (target, method, args) -> null);
|
||||
lbf.registerSingleton("proxiedTemplate", proxy);
|
||||
|
||||
ProxiedTemplate<String> template = lbf.getBean("proxiedTemplate", new ParameterizedTypeReference<>() {});
|
||||
assertThat(template).isSameAs(proxy);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getBeanByTypeWithPrimary() {
|
||||
RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
|
||||
@@ -3899,15 +3917,18 @@ class DefaultListableBeanFactoryTests {
|
||||
}
|
||||
|
||||
private static class Template<T> {
|
||||
|
||||
}
|
||||
|
||||
private static class StringTemplate extends Template<String> {
|
||||
|
||||
}
|
||||
|
||||
private static class NumberTemplate extends Template<Number> {
|
||||
}
|
||||
|
||||
private interface ProxiedTemplate<T> {
|
||||
}
|
||||
|
||||
private static class ProxiedStringTemplate implements ProxiedTemplate<String> {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user