I have a class that looks like this:
class Dao<T>{ ... }
I want to do this:
new Dao<Student>();
from the Spring XML configuration.
Can that be done? How?
Generic types are instantiated to form parameterized types by providing actual type arguments that replace the formal type parameters. A class like LinkedList<E> is a generic type, that has a type parameter E .
To update the Box class to use generics, you create a generic type declaration by changing the code "public class Box" to "public class Box<T>". This introduces the type variable, T, that can be used anywhere inside the class. As you can see, all occurrences of Object are replaced by T.
Reading up about type erasure should help you understand this a bit better.
At runtime, the type parameters for a generic class are erased. Meaning, as cletus said, generics in Java are basically syntactic sugar - they are only a compile-time feature.
Since Spring is instantiate objects at run-time, it is actually free to instantiate a Dao
of any type - and actually, there is nothing stopping it from creating a Dao
and passing in Student
types in some methods and Teacher
types in another.
So basically the answer is, Spring has no idea that the Dao
type is meant to be parameterized and can't do anything with it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With