Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getBeanNamesForType(ResolvableType) doesn't match generic factory method return type for yet-to-be-created bean [SPR-15011] #19578

Closed
spring-projects-issues opened this issue Dec 13, 2016 · 1 comment
Assignees
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Dec 13, 2016

Phil Webb opened SPR-15011 and commented

Originally raised as a @SpyBean issue with Spring Boot. When calling beanFactory.getBeanNamesForType from a BeanFactoryPostProcessor the predictBeanType method does not consider generics. This means given a bean definition Foo<String> calling ResolvableType.forClassWithGenerics(Foo.class, String.class) will not work.

Here's a minimal test case:

import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.Order;

public class GenericBeanTests {

	@Test
	public void test() {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
		context.register(Config.class);
		context.register(TestPostProcessor.class);
		context.refresh();
		context.close();
	}

	@Order(Ordered.LOWEST_PRECEDENCE)
	public static class TestPostProcessor implements BeanFactoryPostProcessor {

		@Override
		public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
			assertTrue(beanFactory.getBeanNamesForType(Foo.class).length > 0);
			assertTrue(beanFactory.getBeanNamesForType(ResolvableType.forClass(Foo.class)).length > 0);
			assertTrue(beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(Foo.class, String.class)).length > 0);
		}

	}

	@Configuration
	public static class Config {

		@Bean
		public Foo<String> fooBean() {
			return new StringFoo();
		}

	}

	public static interface Foo<T> {

	}

	public static class StringFoo implements Foo<String> {

	}

}

Affects: 4.3.4

Issue Links:

  • #18690 ListableBeanFactory#getBeanNamesForType(ResolvableType) fails to return generic @Bean methods ("is duplicated by")
  • #16761 Add generics / parameterized type support to ListableBeanFactory getBeanNamesForType/getBeansOfType methods
  • #18669 getBeanNamesForType(ResolvableType) does not reliably work for beans with AOP proxies
  • #21261 Inconsistent getTypeForFactoryMethod results for parameterized factory method
  • #22056 getBeanNamesForType(ResolvableType) doesn't work for raw singleton instance from @Bean method with generic return type
  • #18463 Provide registerBean variants based on ResolvableType

1 votes, 3 watchers

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Dec 17, 2016

Juergen Hoeller commented

I've got this addressed through a ResolvableType-based revision of our factory method return type caching in RootBeanDefinition, with corresponding use of that full type (if available) in the AbstractBeanFactory.isTypeMatch algorithm. RootBeanDefinition.setTargetType(ResolvableType) hints get respects there as well now.

I'll have this backported to 4.3.5 by Monday morning. Let's see what our integration tests say then...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.