You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add generics / parameterized type support to ListableBeanFactory getBeanNamesForType/getBeansOfType methods [SPR-12147] #16761
Spring currently doesn't have a public API for finding beans based on generics type information. Generic types seem to only be supported in autowiring fields or methods (Spring blog article).
As far as I understand this issue, the intent is to pass the full generic type information to the bean factory there, analogous to introspecting a method parameter declaration. A resolve() call would preserve a substituted type variable but would lose the nested generic types since they can't be represented in the resulting Class.
From my perspective, a getBeanNamesForType(ResolvableType) method should be entirely sufficient, since the getBeansOfType methods are just doing the bean names thing and then getBean for each resulting name. In fact, I'd actively recommend againstgetBeansOfType even in its present form; getBeanNamesForType plus lazy retrieval of the bean instances is usually preferable.
And with respect to the includeNonSingletons and allowEagerInit flags, those are primarily used for limited early checks on bean factory startup... For use later on, I don't see a need to expose them even. Their public availability is to some degree a historic arrangement; we don't expose them for our {{getBeanNamesForAnnotation} variants either. Lari Hotari, do you have a specific need there?
I've introduced getBeanNamesForType(resolvableType and isTypeMatch(name, resolvableType, which should be everything needed to compose generics-based retrieval logic on top. There's also a BeanFactoryUtils.beanNamesForTypeIncludingAncestors(factory, resolvableType convenience method for hierarchical checks.
I'm rather opposed to further convenience variants. I'd only add further variants for retrieval attempts that cannot be expressed any other way.
Activity
spring-projects-issues commentedon Sep 3, 2014
Lari Hotari commented
This issue is related to #14599 .
spring-projects-issues commentedon Sep 3, 2014
Lari Hotari commented
I'm not actually sure if ResolvableType currently support the use case I'm looking to support. I'm working on a pull request for adding Spring Boot support to Ratpack , (exact location. Ratpack uses Guava's com.google.common.reflect.TypeToken (source) for holding generics type information.
Is it possible to create org.springframework.core.ResolvableType from Guava's TypeToken information?
spring-projects-issues commentedon Jan 2, 2015
Phil Webb commented
I think you can use
ResolvableType.forType(typeToken.getType())to do what you want. For example:Will output:
spring-projects-issues commentedon Mar 17, 2015
Rob Winch commented
Perhaps I'm missing something. Is there a reason we need to add additional methods that accept a ResolvableType?
Can you just look up the beans with the following?
For others looking at this issue, you don't even need Guava to resolve the generic type. You can use something like:
spring-projects-issues commentedon Mar 18, 2015
Juergen Hoeller commented
As far as I understand this issue, the intent is to pass the full generic type information to the bean factory there, analogous to introspecting a method parameter declaration. A
resolve()call would preserve a substituted type variable but would lose the nested generic types since they can't be represented in the resultingClass.Juergen
spring-projects-issues commentedon Mar 18, 2015
Juergen Hoeller commented
I'm looking into this at the moment...
From my perspective, a
getBeanNamesForType(ResolvableType)method should be entirely sufficient, since thegetBeansOfTypemethods are just doing the bean names thing and thengetBeanfor each resulting name. In fact, I'd actively recommend againstgetBeansOfTypeeven in its present form;getBeanNamesForTypeplus lazy retrieval of the bean instances is usually preferable.And with respect to the
includeNonSingletonsandallowEagerInitflags, those are primarily used for limited early checks on bean factory startup... For use later on, I don't see a need to expose them even. Their public availability is to some degree a historic arrangement; we don't expose them for our {{getBeanNamesForAnnotation} variants either. Lari Hotari, do you have a specific need there?Juergen
spring-projects-issues commentedon Mar 18, 2015
Juergen Hoeller commented
I've introduced
getBeanNamesForType(resolvableTypeandisTypeMatch(name, resolvableType, which should be everything needed to compose generics-based retrieval logic on top. There's also aBeanFactoryUtils.beanNamesForTypeIncludingAncestors(factory, resolvableTypeconvenience method for hierarchical checks.I'm rather opposed to further convenience variants. I'd only add further variants for retrieval attempts that cannot be expressed any other way.
Juergen