Repository navigation
Description
Oliver Drotbohm opened SPR-9965 and commented
Currently the container considers List and Map types being injected as special injection points and uses generics inspection to inject the appropriate Spring beans. However, there's no general support for injecting generic types in a type safe way.
Assuming we have a generic interface:
interface MyGenericType<T> { ... }
and two Spring beans:
class IntegerType extends MyGenericType<Integer> { ... }
class StringType extends MyGenericType<String> { ... }
A client trying to get a special instance of the generic type looking like this:
class MyClient {
@Autowired
private List<MyGenericType<Integer>> dependencies;
}
currently not only gets injected the IntegerType bean but the StringType as well which will cause exceptions at runtime. The core reason this is the case is that the BeanFactory currently uses Class<?> based type information that does not carry the generics information on injection time and selectvely inspects the generics for injection of special types like List and Map.
The Spring Data Commons project has a TypeInformation abstraction that allows to lazily resolve generics information as it keeps the generics information around when traversing types, property, method parameter and method return types. It could serve as foundation for an improvement of the BeanFactory.
Issue Links:
- Introduce ResolvableType Class [SPR-10973] #15601 Introduce ResolvableType Class ("depends on")
- Generic autowiring should support wiring of components with unresolved type variables [SPR-10993] #15621 Generic autowiring should support wiring of components with unresolved type variables
- autowiring doesn't match generics [SPR-5546] #10217 autowiring doesn't match generics
- Add generics / parameterized type support to ListableBeanFactory getBeanNamesForType/getBeansOfType methods [SPR-12147] #16761 Add generics / parameterized type support to ListableBeanFactory getBeanNamesForType/getBeansOfType methods
5 votes, 13 watchers
Activity
spring-projects-issues commentedon Nov 14, 2012
Eugene Petrenko commented
I have a similar issue in my project.
We used to use EventDispatcher<ListenerClass> for type-autowiring.
Unfortunately it's not possible to have more than one bean of that time at once.
Once you declare a non-generic class (i.e. ListenerDispatcher implements EventDispatcher<ListenerClass>) all generic
type information gets available via Java 1.5+ reflections API.
This could be used to make Spring to filter out extra beans.
I implemented this logic via SimpleAutowireCandidateResolver. So I replaced autowire candidates resolver of my BeanFactory with a custom resolver that implemented in the following way:
It returns null for
getSuggestedValue, still forisAutowireCandidateit checks for generic dependency type and decide which if available candidates is suitable.spring-projects-issues commentedon Nov 22, 2012
Oliver Drotbohm commented
Submitted pull request at #186. Have a look at
GenericTypeWiringTestsfor a sample of what the newly introduced functionality allows to do.spring-projects-issues commentedon Dec 7, 2012
Piotr Findeisen commented
Looks quite similar to #10217
spring-projects-issues commentedon Feb 21, 2013
Didier Villevalois commented
Fantastic! Could we have Olivier's commit soon in a release ?
spring-projects-issues commentedon Oct 11, 2013
Juergen Hoeller commented
This has been rolled into a master now, in a fresh implementation with minimal impact onto the framework's SPIs. To be released in 4.0 RC1 soon!
Juergen
spring-projects-issues commentedon Oct 11, 2013
Juergen Hoeller commented
Quick note:
It's not actually pushed yet since I had a last-minute test failure in a different corner of the framework that I'm still researching. To be pushed as soon as this is sorted out.
spring-projects-issues commentedon May 1, 2014
Richard Kawala commented
We'd also love a method with a signature like getBean(TypeInformation typeInformation) on ApplicationContext so that I can ask for an object of type Provider<SomeClass>.
spring-projects-issues commentedon May 9, 2014
Phil Webb commented
Richard Kawala This particular issue has been closed, if you're still interested in
getBean(TypeInformation ...)support could you please raise a new issue.spring-projects-issues commentedon May 27, 2014
Vladislav Rassokhin commented
Looks like this feature works only for
@annotation-basedconfigurations.Can it be somehow enabled for xml-based configurations?
spring-projects-issues commentedon Sep 3, 2014
Lari Hotari commented
I created a feature request for adding methods to ListableBeanFactory interface that support generics / parameterized types: #16761