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
Oliver Drotbohm opened SPR-9965 and commented
Currently the container considers
ListandMaptypes 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
IntegerTypebean but theStringTypeas well which will cause exceptions at runtime. The core reason this is the case is that theBeanFactorycurrently usesClass<?>based type information that does not carry the generics information on injection time and selectvely inspects the generics for injection of special types likeListandMap.The Spring Data Commons project has a
TypeInformationabstraction 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