Skip to content

Commit

Permalink
Do not extract FactoryBean generic in case of targetType mismatch
Browse files Browse the repository at this point in the history
Closes gh-32489
  • Loading branch information
jhoeller committed Apr 8, 2024
1 parent 67a8100 commit 2232890
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -108,7 +108,8 @@ protected boolean checkGenericTypeMatch(BeanDefinitionHolder bdHolder, Dependenc
Class<?> resolvedClass = targetType.resolve();
if (resolvedClass != null && FactoryBean.class.isAssignableFrom(resolvedClass)) {
Class<?> typeToBeMatched = dependencyType.resolve();
if (typeToBeMatched != null && !FactoryBean.class.isAssignableFrom(typeToBeMatched)) {
if (typeToBeMatched != null && !FactoryBean.class.isAssignableFrom(typeToBeMatched) &&
!typeToBeMatched.isAssignableFrom(resolvedClass)) {
targetType = targetType.getGeneric();
if (descriptor.fallbackMatchAllowed()) {
// Matching the Class-based type determination for FactoryBean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.mockito.Mockito;

import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.config.TypedStringValue;
Expand Down Expand Up @@ -850,6 +851,23 @@ void genericMatchingWithUnresolvedOrderedStream() {
bf.getBean("store2", NumberStore.class), bf.getBean("store1", NumberStore.class));
}

@Test // gh-32489
void genericMatchingAgainstFactoryBeanClass() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.setAutowireCandidateResolver(new GenericTypeAwareAutowireCandidateResolver());

RootBeanDefinition bd = new RootBeanDefinition(MyFactoryBean.class);
// Replicate org.springframework.data.repository.config.RepositoryConfigurationDelegate#registerRepositoriesIn
// behavior of setting targetType, required to hit other branch in
// org.springframework.beans.factory.support.GenericTypeAwareAutowireCandidateResolver.checkGenericTypeMatch
bd.setTargetType(ResolvableType.forClassWithGenerics(MyFactoryBean.class, String.class));
bf.registerBeanDefinition("myFactoryBean", bd);
bf.registerBeanDefinition("myFactoryBeanHolder",
new RootBeanDefinition(MyFactoryBeanHolder.class, AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR, false));

assertThat(bf.getBean(MyFactoryBeanHolder.class).factoryBeans).contains(bf.getBean(MyFactoryBean.class));
}


/**
* Mimics and delegates to {@link Mockito#mock(Class)} -- created here to avoid factory
Expand Down Expand Up @@ -966,4 +984,32 @@ public static NumberStore<Float> newFloatStore() {
}
}


public interface MyGenericInterfaceForFactoryBeans<T> {
}


public static class MyFactoryBean<T extends CharSequence> implements FactoryBean<T>, MyGenericInterfaceForFactoryBeans<T> {

@Override
public T getObject() {
throw new UnsupportedOperationException();
}

@Override
public Class<?> getObjectType() {
return String.class;
}
}


public static class MyFactoryBeanHolder {

List<MyGenericInterfaceForFactoryBeans<?>> factoryBeans; // Requested type is not a FactoryBean type

public MyFactoryBeanHolder(List<MyGenericInterfaceForFactoryBeans<?>> factoryBeans) {
this.factoryBeans = factoryBeans;
}
}

}

0 comments on commit 2232890

Please sign in to comment.