-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Autowiring of generic types [SPR-9965] #14599
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
Comments
Eugene Petrenko commented I have a similar issue in my project. Once you declare a non-generic class (i.e. ListenerDispatcher implements EventDispatcher<ListenerClass>) all generic 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 |
Oliver Drotbohm commented Submitted pull request at #186. Have a look at |
Piotr Findeisen commented Looks quite similar to #10217 |
Didier Villevalois commented Fantastic! Could we have Olivier's commit soon in a release ? |
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 |
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. |
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>. |
Phil Webb commented Richard Kawala This particular issue has been closed, if you're still interested in |
Vladislav Rassokhin commented Looks like this feature works only for |
Lari Hotari commented I created a feature request for adding methods to ListableBeanFactory interface that support generics / parameterized types: #16761 |
Uh oh!
There was an error while loading. Please reload this page.
Oliver Drotbohm opened SPR-9965 and commented
Currently the container considers
List
andMap
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:
and two Spring beans:
A client trying to get a special instance of the generic type looking like this:
currently not only gets injected the
IntegerType
bean but theStringType
as well which will cause exceptions at runtime. The core reason this is the case is that theBeanFactory
currently usesClass<?>
based type information that does not carry the generics information on injection time and selectvely inspects the generics for injection of special types likeList
andMap
.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 theBeanFactory
.Issue Links:
5 votes, 13 watchers
The text was updated successfully, but these errors were encountered: