Skip to content

Commit

Permalink
Check FactoryBean targetType for generic type as well
Browse files Browse the repository at this point in the history
Closes gh-30987
  • Loading branch information
jhoeller committed Aug 3, 2023
1 parent 34747ba commit cba2b6e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Expand Up @@ -908,6 +908,11 @@ protected ResolvableType getTypeForFactoryBean(String beanName, RootBeanDefiniti
// 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;
Expand Down
Expand Up @@ -1980,6 +1980,16 @@ void getBeanNamesForTypeWithPrototypeScopedFactoryBean() {
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
Expand Down Expand Up @@ -3068,6 +3078,25 @@ public T call() {
}


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;
Expand Down

0 comments on commit cba2b6e

Please sign in to comment.