Skip to content

Commit cba2b6e

Browse files
committed
Check FactoryBean targetType for generic type as well
Closes gh-30987
1 parent 34747ba commit cba2b6e

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,11 @@ protected ResolvableType getTypeForFactoryBean(String beanName, RootBeanDefiniti
908908
// static factory method signature or from class inheritance hierarchy...
909909
return getTypeForFactoryBeanFromMethod(mbd.getBeanClass(), factoryMethodName);
910910
}
911+
912+
result = getFactoryBeanGeneric(mbd.targetType);
913+
if (result.resolve() != null) {
914+
return result;
915+
}
911916
result = getFactoryBeanGeneric(beanType);
912917
if (result.resolve() != null) {
913918
return result;

spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,6 +1980,16 @@ void getBeanNamesForTypeWithPrototypeScopedFactoryBean() {
19801980
assertBeanNamesForType(FactoryBean.class, false, false);
19811981
}
19821982

1983+
@Test // gh-30987
1984+
void getBeanNamesForTypeWithFactoryBeanDefinedAsTargetType() {
1985+
RootBeanDefinition beanDefinition = new RootBeanDefinition(TestRepositoryFactoryBean.class);
1986+
beanDefinition.setTargetType(ResolvableType.forClassWithGenerics(TestRepositoryFactoryBean.class,
1987+
CityRepository.class, Object.class, Object.class));
1988+
lbf.registerBeanDefinition("factoryBean", beanDefinition);
1989+
assertBeanNamesForType(TestRepositoryFactoryBean.class, true, false, "&factoryBean");
1990+
assertBeanNamesForType(CityRepository.class, true, false, "factoryBean");
1991+
}
1992+
19831993
/**
19841994
* Verifies that a dependency on a {@link FactoryBean} can <strong>not</strong>
19851995
* be autowired <em>by name</em>, as &amp; is an illegal character in
@@ -3068,6 +3078,25 @@ public T call() {
30683078
}
30693079

30703080

3081+
public static class TestRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable>
3082+
extends RepositoryFactoryBeanSupport<T, S, ID> {
3083+
3084+
@Override
3085+
public T getObject() throws Exception {
3086+
throw new IllegalArgumentException("Should not be called");
3087+
}
3088+
3089+
@Override
3090+
public Class<?> getObjectType() {
3091+
throw new IllegalArgumentException("Should not be called");
3092+
}
3093+
}
3094+
3095+
public record City(String name) {}
3096+
3097+
public static class CityRepository implements Repository<City, Long> {}
3098+
3099+
30713100
public static class LazyInitFactory implements FactoryBean<Object> {
30723101

30733102
public boolean initialized = false;

0 commit comments

Comments
 (0)