diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java index acde40facf8..e40b9b52a78 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java @@ -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 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 { - } private static class StringTemplate extends Template { - } private static class NumberTemplate extends Template { + } + private interface ProxiedTemplate { + } + + private static class ProxiedStringTemplate implements ProxiedTemplate { } }