Check FactoryBean targetType for generic type as well

Closes gh-30987
This commit is contained in:
Juergen Hoeller 2023-08-03 18:10:07 +02:00
parent 34747baed0
commit cba2b6eaf4
2 changed files with 34 additions and 0 deletions

View File

@ -908,6 +908,11 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
// static factory method signature or from class inheritance hierarchy...
return getTypeForFactoryBeanFromMethod(mbd.getBeanClass(), factoryMethodName);
}
result = getFactoryBeanGeneric(mbd.targetType);
if (result.resolve() != null) {
return result;
}
result = getFactoryBeanGeneric(beanType);
if (result.resolve() != null) {
return result;

View File

@ -1980,6 +1980,16 @@ class DefaultListableBeanFactoryTests {
assertBeanNamesForType(FactoryBean.class, false, false);
}
@Test // gh-30987
void getBeanNamesForTypeWithFactoryBeanDefinedAsTargetType() {
RootBeanDefinition beanDefinition = new RootBeanDefinition(TestRepositoryFactoryBean.class);
beanDefinition.setTargetType(ResolvableType.forClassWithGenerics(TestRepositoryFactoryBean.class,
CityRepository.class, Object.class, Object.class));
lbf.registerBeanDefinition("factoryBean", beanDefinition);
assertBeanNamesForType(TestRepositoryFactoryBean.class, true, false, "&factoryBean");
assertBeanNamesForType(CityRepository.class, true, false, "factoryBean");
}
/**
* Verifies that a dependency on a {@link FactoryBean} can <strong>not</strong>
* be autowired <em>by name</em>, as &amp; is an illegal character in
@ -3068,6 +3078,25 @@ class DefaultListableBeanFactoryTests {
}
public static class TestRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable>
extends RepositoryFactoryBeanSupport<T, S, ID> {
@Override
public T getObject() throws Exception {
throw new IllegalArgumentException("Should not be called");
}
@Override
public Class<?> getObjectType() {
throw new IllegalArgumentException("Should not be called");
}
}
public record City(String name) {}
public static class CityRepository implements Repository<City, Long> {}
public static class LazyInitFactory implements FactoryBean<Object> {
public boolean initialized = false;