Skip to content
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

Autowiring of generic types [SPR-9965] #14599

Closed
spring-projects-issues opened this issue Nov 6, 2012 · 10 comments
Closed

Autowiring of generic types [SPR-9965] #14599

spring-projects-issues opened this issue Nov 6, 2012 · 10 comments
Assignees
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Nov 6, 2012

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:

  • #15601 Introduce ResolvableType Class ("depends on")
  • #15621 Generic autowiring should support wiring of components with unresolved type variables
  • #10217 autowiring doesn't match generics
  • #16761 Add generics / parameterized type support to ListableBeanFactory getBeanNamesForType/getBeansOfType methods

5 votes, 13 watchers

@spring-projects-issues
Copy link
Collaborator Author

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

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Nov 22, 2012

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.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Dec 7, 2012

Piotr Findeisen commented

Looks quite similar to #10217

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 21, 2013

Didier Villevalois commented

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

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented 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
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented 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
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented 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
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented 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
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented May 27, 2014

Vladislav Rassokhin commented

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

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Sep 3, 2014

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
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants