[SPR-9965] Autowiring of generic types Created: 06/Nov/12  Updated: 15/Jan/19  Resolved: 11/Oct/13

Status: Closed
Project: Spring Framework
Component/s: None
Affects Version/s: None
Fix Version/s: 4.0 RC1

Type: New Feature Priority: Major
Reporter: Oliver Drotbohm Assignee: Juergen Hoeller
Resolution: Complete Votes: 5
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depend
depends on SPR-10973 Introduce ResolvableType Class Closed
Relate
relates to SPR-10993 Generic autowiring should support wir... Closed
is related to SPR-5546 autowiring doesn't match generics Resolved
is related to SPR-12147 Add generics / parameterized type sup... Closed
Days since last comment: 1 year, 6 weeks ago
Last commented by a User: true
Last updater: Spring Issuemaster

 Description   

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.



 Comments   
Comment by Eugene Petrenko [ 14/Nov/12 ]

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 for isAutowireCandidate it checks for generic dependency type and decide which if available candidates is suitable.

Comment by Oliver Drotbohm [ 22/Nov/12 ]

Submitted pull request at https://github.com/SpringSource/spring-framework/pull/186. Have a look at GenericTypeWiringTests for a sample of what the newly introduced functionality allows to do.

Comment by Piotr Findeisen [ 07/Dec/12 ]

Looks quite similar to SPR-5546

Comment by Didier Villevalois [ 21/Feb/13 ]

Fantastic! Could we have Olivier's commit soon in a release ?

Comment by Juergen Hoeller [ 11/Oct/13 ]

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

Comment by Juergen Hoeller [ 11/Oct/13 ]

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.

Comment by Richard Kawala [ 01/May/14 ]

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>.

Comment by Phil Webb [ 09/May/14 ]

Richard Kawala This particular issue has been closed, if you're still interested in getBean(TypeInformation ...) support could you please raise a new issue.

Comment by Vladislav Rassokhin [ 27/May/14 ]

Looks like this feature works only for @annotation-based configurations.
Can it be somehow enabled for xml-based configurations?

Comment by Lari Hotari [ 03/Sep/14 ]

I created a feature request for adding methods to ListableBeanFactory interface that support generics / parameterized types: SPR-12147

Comment by Spring Issuemaster [ 14/Jan/19 ]

The Spring Framework has migrated to GitHub Issues. This issue corresponds to spring-projects/spring-framework#14599.

Generated at Tue Feb 25 19:27:27 UTC 2020 using Jira 7.13.8#713008-sha1:1606a5c1e7006e1ab135aac81f7a9566b2dbc3a6.