Hello. Such a situation: I raise through the spring bin, which has a field ArrayList<String>
. Can I somehow tell the spring that the leaf that is defined in the config is typed?
- What do you want to achieve with this? - yozh
- In the class-bin itself, the leaf is declared typed. and in the config the class for this sheet is registered simply as ArrayList. that is, the setter takes a typed sheet as a parameter. Spring himself will be able to resolve this situation? - Vladimir
|
1 answer
It must be remembered that typed types are only at the compilation stage. After that, they are overwritten ("type erasure") and the type cannot be determined at runtime. In other words, despite the fact that you can declare a field as ArrayList<String>
, at run time its class will not be anything different from ArrayList<Integer>
- the JVM will simply safely cast the elements of the collection to the required type (safely - because compiling this has already been verified). Since Spring creates beans dynamically at runtime, there are simply no typed types.
Summarizing, this cannot be done, because it is impossible because of generics implementation in Java.
- yozh ,, thank you very much for the clarification. Uchtu - Vladimir
|