Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring with generics

 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does Spring support injecting beans with generics ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beans with generics ? Where ?
I don't think Spring has problems with generics. For example, you can set values in generic collections.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose there is a class



How do I define a bean in the bean definition XML ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't define generics at runtime. If you want to use a specific type of the generic class, you have to make a subclass.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question was like , how do I define an bean instance of Cache say new Cache<String,Integer>. I guess Spring does not support it without subclassing, is that what you mean ?


 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not that it "does not support" it. Generics types are used at compile-time. I can't see the use of defining generic subclasses at runtime.
Try this :

The cache

The context file

The main class


Why does it work ? Because of type erasure. Generic types A and B both become Object types. There's no point in defining generics at runtime. So, yes, you can instanciate generic classes. But no, you cannot define generic types.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explaination.



This will give runtime error as you have given string value to field value.

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will give runtime error as you have given string value to field value.

Guess what. It doesn't At runtime, because of type erasure, Cache<Integer, Integer> cacheTwo becomes Cache cacheTwo.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:
This will give runtime error as you have given string value to field value.

Guess what. It doesn't At runtime, because of type erasure, Cache<Integer, Integer> cacheTwo becomes Cache cacheTwo.


oh ! So cacheTwo.value will have type of String not Integer ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sheriff. In .net we can specify the generic type in XML itself.


Picked from here
http://www.springframework.net/docs/1.3.0/reference/html/objects.html
 
Peter Mularien
Author
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is because .NET has a fundamentally different (IMO better) generics infrastructure, where information about the generic type signature is retained and can be used at runtime. Although this *can* be done in Java, it's hard. For example, the ApplicationListener interface in Spring 3 works with event publishers to use runtime type reflection about the generic signature of a class implementing ApplicationListener to determine which events it's interested in.

peter
 
Dominik Czerwinski
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what about Java-based configuration?


Isn't it brilliant? Day after day I'm getting bigger Java-based configuration advocate.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic