Skip to content

Commit 500b48d

Browse files
committed
Fix BeanFactory.getBean(String, ParameterizedTypeReference) to respect AOP proxy
Before this commit, the implementation uses `ResolvableType::isInstance` which doesn't take JDK proxy into account, it fails if `proxyTargetClass = false`: ``` Bean named 'userDao' is expected to be of type 'org.springframework.cache.config.ExpressionCachingIntegrationTests$BaseDao<org.springframework.cache.config.ExpressionCachingIntegrationTests$User>' but was actually of type 'org.springframework.cache.config.$Proxy53' org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'userDao' is expected to be of type 'org.springframework.cache.config.ExpressionCachingIntegrationTests$BaseDao<org.springframework.cache.config.ExpressionCachingIntegrationTests$User>' but was actually of type 'org.springframework.cache.config.$Proxy53' at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:212) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1312) at org.springframework.cache.config.ExpressionCachingIntegrationTests.expressionIsCacheBasedOnActualMethod(ExpressionCachingIntegrationTests.java:42) ``` See GH-34687 Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
1 parent 99b991b commit 500b48d

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
208208
public <T> T getBean(String name, ParameterizedTypeReference<T> typeReference) throws BeansException {
209209
Object bean = getBean(name);
210210
Type requiredType = typeReference.getType();
211-
if (!ResolvableType.forType(requiredType).isInstance(bean)) {
211+
if (!isTypeMatch(name, ResolvableType.forType(requiredType), true)) {
212212
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
213213
}
214214
return (T) bean;

spring-context/src/test/java/org/springframework/cache/config/ExpressionCachingIntegrationTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,21 @@
2727
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2828
import org.springframework.context.annotation.Bean;
2929
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.core.ParameterizedTypeReference;
3031

3132
/**
3233
* @author Stephane Nicoll
34+
* @author Yanming Zhou
3335
*/
3436
class ExpressionCachingIntegrationTests {
3537

3638
@Test // SPR-11692
37-
@SuppressWarnings("unchecked")
3839
void expressionIsCacheBasedOnActualMethod() {
3940
ConfigurableApplicationContext context =
4041
new AnnotationConfigApplicationContext(SharedConfig.class, Spr11692Config.class);
4142

42-
BaseDao<User> userDao = (BaseDao<User>) context.getBean("userDao");
43-
BaseDao<Order> orderDao = (BaseDao<Order>) context.getBean("orderDao");
43+
BaseDao<User> userDao = context.getBean("userDao", new ParameterizedTypeReference<>() {});
44+
BaseDao<Order> orderDao = context.getBean("orderDao", new ParameterizedTypeReference<>() {});
4445

4546
userDao.persist(new User("1"));
4647
orderDao.persist(new Order("2"));

0 commit comments

Comments
 (0)