aspose file tools*
The moose likes Spring and the fly likes Spring with generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring with generics" Watch "Spring with generics" New topic
Author

Spring with generics

Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Does Spring support injecting beans with generics ?


Groovy
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Beans with generics ? Where ?
I don't think Spring has problems with generics. For example, you can set values in generic collections.


[My Blog]
All roads lead to JavaRanch
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Suppose there is a class



How do I define a bean in the bean definition XML ?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Feb 27, 2002
Posts: 8919

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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Feb 27, 2002
Posts: 8919

Thanks for the explaination.



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

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Feb 27, 2002
Posts: 8919

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

Joined: Nov 24, 2005
Posts: 14688
    
  16

Yes.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

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

Joined: Sep 06, 2007
Posts: 84
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


Author, Spring Security 3 (the Book), Packt Publishing, 2010
SCJP, OCP
Dominik Czerwinski
Greenhorn

Joined: May 28, 2013
Posts: 1
And what about Java-based configuration?


Isn't it brilliant? Day after day I'm getting bigger Java-based configuration advocate.
 
 
subject: Spring with generics