Skip to content

Autowiring of generic types [SPR-9965] #14599

Closed

Description

@spring-projects-issues

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:

5 votes, 13 watchers

Activity

  1. spring-projects-issues commented on Nov 14, 2012

    @spring-projects-issues
    CollaboratorAuthor

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

  2. spring-projects-issues commented on Nov 22, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Oliver Drotbohm commented

    Submitted pull request at #186. Have a look at GenericTypeWiringTests for a sample of what the newly introduced functionality allows to do.

  3. spring-projects-issues commented on Dec 7, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Piotr Findeisen commented

    Looks quite similar to #10217

  4. spring-projects-issues commented on Feb 21, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Didier Villevalois commented

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

  5. spring-projects-issues commented on Oct 11, 2013

    @spring-projects-issues
    CollaboratorAuthor

    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

  6. spring-projects-issues commented on Oct 11, 2013

    @spring-projects-issues
    CollaboratorAuthor

    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.

  7. spring-projects-issues commented on May 1, 2014

    @spring-projects-issues
    CollaboratorAuthor

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

  8. spring-projects-issues commented on May 9, 2014

    @spring-projects-issues
    CollaboratorAuthor

    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.

  9. spring-projects-issues commented on May 27, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Vladislav Rassokhin commented

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

  10. spring-projects-issues commented on Sep 3, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Lari Hotari commented

    I created a feature request for adding methods to ListableBeanFactory interface that support generics / parameterized types: #16761

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions